[Idrs-commit] CVS: Idrs/dev/src/net/sourceforge/idrs/deploy/macro MacroToXML.class,1.5,1.6 MacroToXM
Brought to you by:
bigman921
|
From: Marc B. <big...@us...> - 2002-01-25 14:49:08
|
Update of /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/deploy/macro
In directory usw-pr-cvs1:/tmp/cvs-serv15047/src/net/sourceforge/idrs/deploy/macro
Modified Files:
MacroToXML.class MacroToXML.java MacroToXMLHandler.class
Log Message:
First working version of macro translator created. Also added dev/xml directory containing all xml files from compiler project. As always, re-ran javadoc.
Index: MacroToXML.class
===================================================================
RCS file: /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/deploy/macro/MacroToXML.class,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
Binary files /tmp/cvsPEDHbB and /tmp/cvsGTHvt4 differ
Index: MacroToXML.java
===================================================================
RCS file: /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/deploy/macro/MacroToXML.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** MacroToXML.java 2002/01/23 22:33:04 1.12
--- MacroToXML.java 2002/01/25 14:49:03 1.13
***************
*** 2,14 ****
MacroToXML.java
Copyright (C) 2001 Marc Boorshtein
!
! The contents of this file are subject to the Mozilla Public License Version 1.0 (the License);
! you may not use this file except in compliance with the License. You may obtain a copy of the
License at http://www.mozilla.org/MPL/
!
! Software distributed under the License is distributed on an "AS IS" basis,
! WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.
! */
package net.sourceforge.idrs.deploy.macro;
--- 2,14 ----
MacroToXML.java
Copyright (C) 2001 Marc Boorshtein
!
! The contents of this file are subject to the Mozilla Public License Version 1.0 (the License);
! you may not use this file except in compliance with the License. You may obtain a copy of the
License at http://www.mozilla.org/MPL/
!
! Software distributed under the License is distributed on an "AS IS" basis,
! WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific
language governing rights and limitations under the License.
! */
package net.sourceforge.idrs.deploy.macro;
***************
*** 23,342 ****
*/
public class MacroToXML {
! String rmlSrc;
! Writer out;
! MacroToXMLHandler info;
! boolean inSimple;
! boolean writeText;
! boolean isBody;
! boolean popTag;
! boolean lastTagWrite;
! boolean lookLastTag;
! boolean ownLine;
! boolean inTag;
!
! Stack writeStack;
!
! String currentTagName;
!
! /**
! *Constructor, loads transoformation xml document
! *@param parser Class of parser to be used to parse XML transformation document
! *@param xmlSrc Source of XML document, either a string or a file path
! *@param isFile Identifies if xmlSrc is the xml as a string or a path to the xml file
! */
! public MacroToXML(String parser, String xmlSrc, boolean isFile) throws Exception {
! ownLine = false;
! inTag = false;
! lastTagWrite = false;
! lookLastTag = false;
! writeText = false;
! isBody = false;
! inSimple = false;
! writeStack = new Stack();
!
! XMLReader xmlParser = XMLReaderFactory.createXMLReader(parser);
!
! InputSource in;
! info = new MacroToXMLHandler();
! if (isFile) {
! in = new InputSource(new InputStreamReader(new FileInputStream(new File(xmlSrc))));
! }
! else {
! in = new InputSource(new StringReader(xmlSrc));
! }
! xmlParser.setContentHandler(info);
! xmlParser.parse(in);
! }
!
! /**
! *Sets the rml-macro source as a string
! *@param rml The RML-Macro source
! */
! public void setRML(String rml) {
! this.rmlSrc = rml;
! }
!
! /**
! *Sets the writer where the RML-XML will be written to
! *@param out Writer for output
! */
! public void setWriter(Writer out) throws IOException {
! this.out = out;
! }
!
! /**
! *Transforms the RML-Macro page into an RML-XML page
! */
! public void transformToXML() throws IOException{
! int begin=0,end=0,tagEnd;
! boolean isScript;
!
! String curr = "", tag="";
! end = rmlSrc.indexOf("<",begin);
!
! while (end != -1) {
!
! isScript = isScriptTag(end);
! curr = rmlSrc.substring(begin,end);
!
! //debug
! System.out.println("text : " + curr);
!
!
! if (isScript) {
! outText(curr);
! tagEnd = outScript(end);
! }
! else {
! tagEnd = readTag(end);
!
! tag = rmlSrc.substring(end, tagEnd);
!
! System.out.print("tag : " + tag);
!
! if (isRMLTag(tag)) {
! outText(curr);
! outTag(tag);
! }
! else {
! outText(curr);
! outText(tag);
! }
! }
!
! begin = tagEnd;
! end = rmlSrc.indexOf("<",begin);
! }
!
! }
!
! /**
! *Determines if current tag is a script tag
! *@param begin Begining of tag in rmlSrc
! */
! protected boolean isScriptTag(int begin) {
! return rmlSrc.charAt(begin + 1) == info.getScriptChar();
! }
!
! /**
! *Writes a fully xml compatable version of a script
! *@param begin Beginning of tag in rmlSrc
! */
! protected int outScript(int begin) throws IOException {
! int end = rmlSrc.indexOf(info.getScriptChar() + ">",begin);
! boolean isEcho = rmlSrc.charAt(begin + 2) == '=';
!
! out.write("<" + info.getNameSpace() + ":");
! out.write( isEcho ? info.getEchoScriptTag() : info.getScriptTag());
! out.write(">");
!
! out.write(xmlEncode(rmlSrc.substring((isEcho ? begin + 3 : begin + 2),end)));
!
! out.write("</" + info.getNameSpace() + ":");
! out.write( isEcho ? info.getEchoScriptTag() : info.getScriptTag());
! out.write(">");
!
! return end + 2;
! }
!
! /**
! *Writes text apropriatly, either with text tags or without them
! *@param txt Text to be written
! */
! protected void outText(String txt) throws IOException {
! if (txt.length() == 0) return;
!
! if (shouldWrite())
! out.write("<" + info.getNameSpace() + ":" + info.getTextTag() + ">");
!
!
!
! out.write(xmlEncode(txt));
!
! if (shouldWrite())
! out.write("</" + info.getNameSpace() + ":" + info.getTextTag() + ">");
!
! lookLastTag = false;
! inSimple = false;
! }
!
! /**
! *Reads rmlSrc for position of the end of the current tag
! *@param begin Beginning of tag in rmlSrc
! */
! protected int readTag(int begin) {
! return rmlSrc.indexOf(">", begin)+1;
! }
!
! /**
! *Determines if a tag is an RML tag
! *@param tag Tag to check
! */
! protected boolean isRMLTag(String tag) {
!
! int begin=0, end=0,tmp;
! char c;
! c = tag.charAt(1);
! String tagName;
! boolean inTag, isTag=false;
!
! //writeText = isBody;
!
! if (c == '/') {
! begin = 2;
! inTag = true;
! }
! else {
! begin = 1;
! inTag = false;
! }
!
! tmp = tag.indexOf(" ",begin);
! if (tmp == -1) {
! tmp = tag.indexOf(">",begin);
! }
!
! tagName = tag.substring(begin,tmp);
!
! this.currentTagName=tagName;
!
! isTag = info.isRMLTag(tagName.toLowerCase().trim());
!
! if (isTag) {
! if (inTag) {
! popTag = true;
! }
! else {
! this.inSimple = info.isTagSimple(tagName.toLowerCase().trim());
! if (! writeStack.empty()) {
! this.lastTagWrite = ((Boolean) writeStack.peek()).booleanValue();
! }
! else {
! this.lastTagWrite = false;
! }
!
! this.lookLastTag = true;
! writeStack.push(new Boolean(this.inSimple));
! System.out.println("PUSH " + tagName);
! popTag = false;
! }
!
!
! }
!
! /*
!
! if (isTag && (tagName.toLowerCase().trim().equalsIgnoreCase("body") || tagName.toLowerCase().trim().equalsIgnoreCase("head")))
! if (inTag)
! isBody = false;
! else {
! isBody = true;
! }*/
!
! return isTag;
! }
!
! /**
! *Writes the RML tag as full XML 1.0 tag
! *@param tag Tag to write
! */
! protected void outTag(String tag) throws IOException {
! int begin=0, end=0,tmp;
! char c;
! c = tag.charAt(1);
! String tagName;
! String atts;
! StringTokenizer tok;
! String token;
!
!
! tagName = this.currentTagName;
!
! out.write("<" + ((c=='/') ? "/" : "") + info.getNameSpace() + ":");
!
! begin = end;
! end = tag.indexOf(">");
!
! atts = tag.substring(begin + 1,end);
! tok = new StringTokenizer(atts,"\"",false);
!
! if (tok.countTokens() == 1)
! out.write(info.getCorrectRMLTagName(tagName));
!
! while (tok.hasMoreTokens() && tok.countTokens() != 1) {
! token = tok.nextToken();
! out.write(token);
! out.write("\"");
! out.write(xmlEncode(tok.nextToken()));
! out.write("\"");
! }
!
!
!
! out.write(">");
!
! if (popTag) {
! System.out.println("POP " + tagName);
! writeStack.pop();
! popTag = false;
! }
!
!
!
!
! }
!
! /**
! *Determines if the current block of text should be wrapped in text tags
! */
! protected boolean shouldWrite() {
! if (lookLastTag) {
! return ! this.lastTagWrite;
! }
! else {
! return ! ((! writeStack.empty()) && (((Boolean) writeStack.peek()).booleanValue()));
! }
! }
!
! /**
! *Returns an XML escaped version of the text passed it
! *@param txt Text to encode
! */
! protected String xmlEncode(String txt) {
! StringBuffer buffer = new StringBuffer();
!
! for (int i = 0;i<txt.length();i++) {
! switch (txt.charAt(i)) {
! case '&' : buffer.append("&"); break;
! case '<' : buffer.append("<"); break;
! case '>' : buffer.append(">"); break;
! case '\'' : buffer.append("'"); break;
! case '\"' : buffer.append("""); break;
! default : buffer.append(txt.charAt(i));
! }
! }
!
! return buffer.toString();
! }
}
--- 23,423 ----
*/
public class MacroToXML {
! String rmlSrc;
! Writer out;
! MacroToXMLHandler info;
! boolean inSimple;
! boolean writeText;
! boolean isBody;
! boolean popTag;
! boolean lastTagWrite;
! boolean lookLastTag;
! boolean ownLine;
! boolean lastOwnLine;
! boolean inTag;
!
! Stack writeStack;
!
! String currentTagName;
!
! /**
! *Constructor, loads transoformation xml document
! *@param parser Class of parser to be used to parse XML transformation document
! *@param xmlSrc Source of XML document, either a string or a file path
! *@param isFile Identifies if xmlSrc is the xml as a string or a path to the xml file
! */
! public MacroToXML(String parser, String xmlSrc, boolean isFile) throws Exception {
! ownLine = false;
! inTag = false;
! lastTagWrite = false;
! lookLastTag = false;
! writeText = false;
! isBody = false;
! inSimple = false;
! lastOwnLine = false;
! writeStack = new Stack();
!
! XMLReader xmlParser = XMLReaderFactory.createXMLReader(parser);
!
! InputSource in;
! info = new MacroToXMLHandler();
! if (isFile) {
! in = new InputSource(new InputStreamReader(new FileInputStream(new File(xmlSrc))));
! }
! else {
! in = new InputSource(new StringReader(xmlSrc));
! }
! xmlParser.setContentHandler(info);
! xmlParser.parse(in);
! }
!
! /**
! *Sets the rml-macro source as a string
! *@param rml The RML-Macro source
! */
! public void setRML(String rml) {
! this.rmlSrc = rml;
! }
!
! /**
! *Sets the writer where the RML-XML will be written to
! *@param out Writer for output
! */
! public void setWriter(Writer out) throws IOException {
! this.out = out;
! }
!
! /**
! *Transforms the RML-Macro page into an RML-XML page
! */
! public void transformToXML() throws IOException{
! int begin=0,end=0,tagEnd;
! boolean isScript;
!
! String curr = "", tag="";
! end = rmlSrc.indexOf("<",begin);
!
! while (end != -1) {
!
! isScript = isScriptTag(end);
! curr = rmlSrc.substring(begin,end);
!
! //debug
! System.out.println("text : " + curr);
!
!
! if (isScript) {
! outText(curr);
! tagEnd = outScript(end);
! }
! else {
! tagEnd = readTag(end);
!
! tag = rmlSrc.substring(end, tagEnd);
!
! System.out.print("tag : " + tag);
!
! if (isRMLTag(tag)) {
! outText(curr);
! tagEnd = outTag(tag,tagEnd);
! }
! else {
! outText(curr);
! outText(tag);
! }
! }
!
! begin = tagEnd;
! end = rmlSrc.indexOf("<",begin);
! }
!
! }
!
! /**
! *Determines if current tag is a script tag
! *@param begin Begining of tag in rmlSrc
! */
! protected boolean isScriptTag(int begin) {
! return rmlSrc.charAt(begin + 1) == info.getScriptChar();
! }
!
! /**
! *Writes a fully xml compatable version of a script
! *@param begin Beginning of tag in rmlSrc
! */
! protected int outScript(int begin) throws IOException {
! int end = rmlSrc.indexOf(info.getScriptChar() + ">",begin);
! int spaceBegin;
! int taglen;
! boolean isEcho = rmlSrc.charAt(begin + 2) == '=';
! String tag;
! String script = xmlEncode(rmlSrc.substring((isEcho ? begin + 3 : begin + 2),end));
! String tmpLine,whiteSpace;
! StringTokenizer tok;
!
! tag = "<" + info.getNameSpace() + ":" + (isEcho ? info.getEchoScriptTag() : info.getScriptTag()) + ">";
! out.write(tag);
!
! if (isEcho) {
! out.write(script);
! }
! else {
! tmpLine = script.substring(1,script.indexOf('\n'));
! script = script.substring(script.indexOf('\n') + 1);
! taglen = tag.length() + ((String) "</" + info.getNameSpace() + ":" + info.getTextTag() + ">").length();
! spaceBegin = rmlSrc.lastIndexOf('\n',begin);
! //whiteSpace = rmlSrc.substring(spaceBegin + 1,begin);
! whiteSpace = "";
!
! for (int i=0;i<taglen;i++)
! whiteSpace += " ";
!
! tok = new StringTokenizer(script,"\n",false);
!
! out.write(tmpLine + "\n");
! while (tok.hasMoreTokens()) {
! out.write(whiteSpace + tok.nextToken() + "\n");
! }
! }
!
! out.write("</" + info.getNameSpace() + ":");
! out.write( isEcho ? info.getEchoScriptTag() : info.getScriptTag());
! out.write(">");
!
! return end + 2;
! }
!
! /**
! *Writes text apropriatly, either with text tags or without them
! *@param txt Text to be written
! */
! protected void outText(String txt) throws IOException {
! boolean hasCR = (txt.indexOf("\n") != -1);
! String splitText;
!
! if (txt.length() == 0) return;
!
!
!
!
! if (this.ownLine) {
! if (shouldWrite() && hasCR && !lastOwnLine)
! out.write("<" + info.getNameSpace() + ":" + info.getTextTag() + ">");
!
! if (hasCR) {
! splitText = xmlEncode(txt.substring(0,txt.lastIndexOf('\n')));
! if (splitText.trim().length() != 0)
! out.write(splitText);
! }
! else {
! out.write(xmlEncode("\n" + txt));
! }
!
! if (shouldWrite() && hasCR && !lastOwnLine)
! out.write("</" + info.getNameSpace() + ":" + info.getTextTag() + ">");
!
! if (hasCR)
! out.write(xmlEncode(txt.substring(txt.lastIndexOf('\n'))));
!
! }
! else {
! if (this.lastOwnLine) {
! out.write("\n");
! this.lastOwnLine = false;
! }
!
! if (shouldWrite())
! out.write("<" + info.getNameSpace() + ":" + info.getTextTag() + ">");
!
! out.write(xmlEncode(txt));
!
! if (shouldWrite())
! out.write("</" + info.getNameSpace() + ":" + info.getTextTag() + ">");
! }
!
!
! lookLastTag = false;
! inSimple = false;
! }
!
!
! /**
! *Reads rmlSrc for position of the end of the current tag
! *@param begin Beginning of tag in rmlSrc
! */
! protected int readTag(int begin) {
! return rmlSrc.indexOf(">", begin)+1;
! }
!
! /**
! *Determines if a tag is an RML tag
! *@param tag Tag to check
! */
! protected boolean isRMLTag(String tag) {
!
! int begin=0, end=0,tmp;
! char c;
! c = tag.charAt(1);
! String tagName;
! boolean isTag=false;
!
! //writeText = isBody;
!
! if (c == '/') {
! begin = 2;
! inTag = true;
! }
! else {
! begin = 1;
! inTag = false;
! }
!
! tmp = tag.indexOf(" ",begin);
! if (tmp == -1) {
! tmp = tag.indexOf(">",begin);
! }
!
! tagName = tag.substring(begin,tmp);
!
! this.currentTagName=tagName;
!
! isTag = info.isRMLTag(tagName.toLowerCase().trim());
!
! if (isTag) {
! this.ownLine = info.isTagOwnLine(tagName.toLowerCase().trim());
! if (inTag) {
! popTag = true;
! }
! else {
! this.inSimple = info.isTagSimple(tagName.toLowerCase().trim());
!
!
! if (! writeStack.empty()) {
! this.lastTagWrite = ((Boolean) writeStack.peek()).booleanValue();
! }
! else {
! this.lastTagWrite = false;
! }
!
! this.lookLastTag = true;
! writeStack.push(new Boolean(this.inSimple));
! System.out.println("PUSH " + tagName);
! popTag = false;
! }
!
!
! }
!
! /*
!
! if (isTag && (tagName.toLowerCase().trim().equalsIgnoreCase("body") || tagName.toLowerCase().trim().equalsIgnoreCase("head")))
! if (inTag)
! isBody = false;
! else {
! isBody = true;
! }*/
!
! return isTag;
! }
!
! /**
! *Writes the RML tag as full XML 1.0 tag
! *@param tag Tag to write
! */
! protected int outTag(String tag,int currEnd) throws IOException {
! int begin=0, end=0,tmp,retEnd;
! char c;
! c = tag.charAt(1);
! String tagName;
! String atts;
! StringTokenizer tok;
! String token;
!
!
! tagName = this.currentTagName;
!
! out.write("<" + ((c=='/') ? "/" : "") + info.getNameSpace() + ":");
!
! begin = end;
! end = tag.indexOf(">");
!
! begin = tag.indexOf(tagName) + tagName.length();
! atts = tag.substring(begin,end);
!
! tok = new StringTokenizer(atts,"\"",false);
!
!
! out.write(info.getCorrectRMLTagName(tagName));
!
! while (tok.hasMoreTokens() && tok.countTokens() != 1) {
! token = tok.nextToken();
! out.write(token);
! out.write("\"");
! out.write(xmlEncode(tok.nextToken()));
! out.write("\"");
! }
!
!
!
! out.write(">");
!
! if (popTag) {
! System.out.println("POP " + tagName);
! writeStack.pop();
! popTag = false;
! }
!
!
!
!
! if (this.ownLine) {
! System.out.println("Current End : " + currEnd);
! System.out.println("New End : " + this.rmlSrc.indexOf("\n",currEnd + 1) + 1);
!
! retEnd = (this.rmlSrc.indexOf("\n",currEnd)+1);
! }
! else {
! retEnd = currEnd;
! }
!
! this.lastOwnLine = this.ownLine;
! this.ownLine = false;
!
! return retEnd;
!
!
! }
!
! /**
! *Determines if the current block of text should be wrapped in text tags
! */
! protected boolean shouldWrite() {
! if (lookLastTag) {
! return ! this.lastTagWrite;
! }
! else {
! return ! ((! writeStack.empty()) && (((Boolean) writeStack.peek()).booleanValue()));
! }
! }
!
! /**
! *Returns an XML escaped version of the text passed it
! *@param txt Text to encode
! */
! protected String xmlEncode(String txt) {
! StringBuffer buffer = new StringBuffer();
!
! for (int i = 0;i<txt.length();i++) {
! switch (txt.charAt(i)) {
! case '&' : buffer.append("&"); break;
! case '<' : buffer.append("<"); break;
! case '>' : buffer.append(">"); break;
! case '\'' : buffer.append("'"); break;
! case '\"' : buffer.append("""); break;
! default : buffer.append(txt.charAt(i));
! }
! }
!
! return buffer.toString();
! }
}
Index: MacroToXMLHandler.class
===================================================================
RCS file: /cvsroot/idrs/Idrs/dev/src/net/sourceforge/idrs/deploy/macro/MacroToXMLHandler.class,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
Binary files /tmp/cvsm1qnxD and /tmp/cvsUNjWX6 differ
|