Make first real commit: copy of CaRMetal 4.2.8
This commit is contained in:
parent
002acfc88e
commit
c312811084
1120 changed files with 226843 additions and 1 deletions
86
rene/util/xml/SVGWriter.java
Normal file
86
rene/util/xml/SVGWriter.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class SVGWriter extends XmlWriter {
|
||||
int W, H;
|
||||
|
||||
public SVGWriter(final PrintWriter o, final String enc, final int w,
|
||||
final int h) {
|
||||
super(o);
|
||||
printEncoding(enc);
|
||||
W = w;
|
||||
H = h;
|
||||
startTagStart("svg");
|
||||
printArg("width", "" + w);
|
||||
printArg("height", "" + h);
|
||||
startTagEndNewLine();
|
||||
}
|
||||
|
||||
public SVGWriter(final PrintWriter o) {
|
||||
super(o);
|
||||
}
|
||||
|
||||
public void startSVG(final int w, final int h) {
|
||||
printEncoding("utf-8");
|
||||
Out.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"");
|
||||
Out.println("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
|
||||
startTagStart("svg");
|
||||
printArg("xmlns", "http://www.w3.org/2000/svg");
|
||||
printArg("width", "" + w);
|
||||
printArg("height", "" + h);
|
||||
startTagEndNewLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
endTag("svg");
|
||||
super.close();
|
||||
}
|
||||
|
||||
public void coord(final int x, final int y) {
|
||||
printArg("x", "" + x);
|
||||
printArg("y", "" + y);
|
||||
}
|
||||
|
||||
public void text(final String text, final int x, final int y) {
|
||||
startTagStart("text");
|
||||
coord(x, y);
|
||||
startTagEnd();
|
||||
print(text);
|
||||
endTagNewLine("text");
|
||||
}
|
||||
|
||||
public static void main(final String args[]) throws Exception {
|
||||
final SVGWriter out = new SVGWriter(new PrintWriter(
|
||||
new FileOutputStream("test.svg")), "", 300, 300);
|
||||
out.text("Hallo Welt", 10, 95);
|
||||
out.startTagStart("path");
|
||||
out.printArg("d", "M 150 150 A 50 50 0 1 0 100 200");
|
||||
out.printArg("style", "fill:none;stroke-width:1;stroke:black");
|
||||
out.finishTagNewLine();
|
||||
out.close();
|
||||
}
|
||||
}
|
505
rene/util/xml/XmlReader.java
Normal file
505
rene/util/xml/XmlReader.java
Normal file
|
@ -0,0 +1,505 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import rene.util.SimpleByteBuffer;
|
||||
import rene.util.SimpleStringBuffer;
|
||||
import rene.util.list.ListElement;
|
||||
|
||||
public class XmlReader {
|
||||
BufferedReader In;
|
||||
SimpleStringBuffer buf = new SimpleStringBuffer(10000);
|
||||
|
||||
public XmlReader() {
|
||||
In = null;
|
||||
}
|
||||
|
||||
public XmlReader(final BufferedReader in) {
|
||||
In = in;
|
||||
}
|
||||
|
||||
public XmlReader(final InputStream in) throws XmlReaderException {
|
||||
try { // read the file into a buffer
|
||||
final BufferedInputStream rin = new BufferedInputStream(in);
|
||||
final SimpleByteBuffer bb = new SimpleByteBuffer(10000);
|
||||
while (true) {
|
||||
final int k = rin.read();
|
||||
if (k < 0)
|
||||
break;
|
||||
bb.append((byte) k);
|
||||
}
|
||||
rin.close();
|
||||
final byte b[] = bb.getByteArray();
|
||||
|
||||
// Try to open an ASCII stream, or a default stream
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(b);
|
||||
XmlReader R = null;
|
||||
try {
|
||||
R = new XmlReader(new BufferedReader(new InputStreamReader(bin,
|
||||
"ASCII")));
|
||||
} catch (final UnsupportedEncodingException ex) {
|
||||
R = new XmlReader(
|
||||
new BufferedReader(new InputStreamReader(bin)));
|
||||
}
|
||||
|
||||
// Determine the encoding
|
||||
String Encoding = null;
|
||||
while (true) {
|
||||
while (true) {
|
||||
final int c = R.read();
|
||||
if (c == -1)
|
||||
throw new Exception("<?xml> tag not found");
|
||||
if (c == '<')
|
||||
break;
|
||||
}
|
||||
if (R.found("?xml")) {
|
||||
String s = R.scanFor("?>");
|
||||
if (s == null)
|
||||
throw new Exception("<?xml> tag error");
|
||||
int n = s.indexOf("encoding=\"");
|
||||
if (n >= 0) {
|
||||
n += "encoding=\"".length();
|
||||
s = s.substring(n);
|
||||
final int m = s.indexOf('\"');
|
||||
if (m < 0)
|
||||
throw new Exception("Closing bracket missing");
|
||||
Encoding = s.substring(0, m).toUpperCase();
|
||||
if (Encoding.equals("UTF-8"))
|
||||
Encoding = "UTF8";
|
||||
// for IE5 !
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Open a stream with this encoding
|
||||
bin = new ByteArrayInputStream(b);
|
||||
if (Encoding == null)
|
||||
In = new BufferedReader(new InputStreamReader(bin));
|
||||
else
|
||||
try {
|
||||
In = new BufferedReader(
|
||||
new InputStreamReader(bin, Encoding));
|
||||
} catch (final UnsupportedEncodingException e) {
|
||||
try {
|
||||
In = new BufferedReader(new InputStreamReader(bin,
|
||||
"ASCII"));
|
||||
} catch (final UnsupportedEncodingException ex) {
|
||||
In = new BufferedReader(new InputStreamReader(bin));
|
||||
}
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
throw new XmlReaderException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void init(final InputStream in) throws XmlReaderException {
|
||||
try { // read the file into a buffer
|
||||
|
||||
final BufferedInputStream rin = new BufferedInputStream(in);
|
||||
final SimpleByteBuffer bb = new SimpleByteBuffer(10000);
|
||||
while (true) {
|
||||
final int k = rin.read();
|
||||
if (k < 0)
|
||||
break;
|
||||
bb.append((byte) k);
|
||||
}
|
||||
rin.close();
|
||||
final byte b[] = bb.getByteArray();
|
||||
|
||||
// Try to open an ASCII stream, or a default stream
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(b);
|
||||
XmlReader R = null;
|
||||
try {
|
||||
R = new XmlReader(new BufferedReader(new InputStreamReader(bin,
|
||||
"ASCII")));
|
||||
} catch (final UnsupportedEncodingException ex) {
|
||||
R = new XmlReader(
|
||||
new BufferedReader(new InputStreamReader(bin)));
|
||||
}
|
||||
|
||||
// Determine the encoding
|
||||
String Encoding = null;
|
||||
while (true) {
|
||||
while (true) {
|
||||
final int c = R.read();
|
||||
if (c == -1)
|
||||
throw new Exception("<?xml> tag not found");
|
||||
if (c == '<')
|
||||
break;
|
||||
}
|
||||
if (R.found("?xml")) {
|
||||
String s = R.scanFor("?>");
|
||||
if (s == null)
|
||||
throw new Exception("<?xml> tag error");
|
||||
int n = s.indexOf("encoding=\"");
|
||||
if (n >= 0) {
|
||||
n += "encoding=\"".length();
|
||||
s = s.substring(n);
|
||||
final int m = s.indexOf('\"');
|
||||
if (m < 0)
|
||||
throw new Exception("Closing bracket missing");
|
||||
Encoding = s.substring(0, m).toUpperCase();
|
||||
if (Encoding.equals("UTF-8"))
|
||||
Encoding = "UTF8";
|
||||
// for IE5 !
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Open a stream with this encoding
|
||||
bin = new ByteArrayInputStream(b);
|
||||
if (Encoding == null)
|
||||
In = new BufferedReader(new InputStreamReader(bin));
|
||||
else
|
||||
try {
|
||||
In = new BufferedReader(
|
||||
new InputStreamReader(bin, Encoding));
|
||||
} catch (final UnsupportedEncodingException e) {
|
||||
try {
|
||||
In = new BufferedReader(new InputStreamReader(bin,
|
||||
"ASCII"));
|
||||
} catch (final UnsupportedEncodingException ex) {
|
||||
In = new BufferedReader(new InputStreamReader(bin));
|
||||
}
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
throw new XmlReaderException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan an xml file. This function reads, until <?xml is found. then it
|
||||
* skips this declaration and scans the rest of the items.
|
||||
*/
|
||||
public XmlTree scan() throws XmlReaderException {
|
||||
while (true) {
|
||||
while (true) {
|
||||
final int c = read();
|
||||
if (c == -1)
|
||||
return null;
|
||||
if (c == '<')
|
||||
break;
|
||||
}
|
||||
if (found("?xml")) {
|
||||
final String s = scanFor("?>");
|
||||
if (s == null)
|
||||
return null;
|
||||
final XmlTree t = new XmlTree(new XmlTagRoot());
|
||||
t.addchild(new XmlTree(new XmlTagPI(s)));
|
||||
scanContent(t);
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void scanContent(final XmlTree t) throws XmlReaderException { // System.out.println("Sanning for "+t.getTag().name()+" ("+
|
||||
// t.getTag().countParams()+")");
|
||||
while (true) {
|
||||
String s = scanFor('<');
|
||||
if (s == null) {
|
||||
if (t.getTag() instanceof XmlTagRoot)
|
||||
return;
|
||||
exception("File ended surprisingly");
|
||||
}
|
||||
if (!empty(s)) {
|
||||
t
|
||||
.addchild(new XmlTree(new XmlTagText(XmlTranslator
|
||||
.toText(s))));
|
||||
}
|
||||
if (found("!--")) {
|
||||
s = scanFor("-->");
|
||||
continue;
|
||||
}
|
||||
if (found("!")) {
|
||||
s = scanTagFor('>');
|
||||
continue;
|
||||
}
|
||||
if (found("?")) {
|
||||
s = scanTagFor("?>");
|
||||
t.addchild(new XmlTree(new XmlTagPI(s)));
|
||||
continue;
|
||||
}
|
||||
s = scanTagFor('>');
|
||||
if (s == null)
|
||||
exception("> missing");
|
||||
if (s.startsWith("/")) {
|
||||
if (s.substring(1).equals(t.getTag().name()))
|
||||
return;
|
||||
else
|
||||
exception("End tag without start tag");
|
||||
}
|
||||
if (s.endsWith("/")) {
|
||||
t.addchild(new XmlTree(new XmlTag(s
|
||||
.substring(0, s.length() - 1))));
|
||||
} else {
|
||||
final XmlTree t0 = new XmlTree(new XmlTag(s));
|
||||
scanContent(t0);
|
||||
t.addchild(t0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean empty(final String s) {
|
||||
final int n = s.length();
|
||||
for (int i = 0; i < n; i++) {
|
||||
final char c = s.charAt(i);
|
||||
if (c != ' ' && c != '\n' && c != '\t')
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip Blanks.
|
||||
*
|
||||
* @return Non-blank character or -1 for EOF.
|
||||
*/
|
||||
public int skipBlanks() throws XmlReaderException {
|
||||
while (true) {
|
||||
final int c = read();
|
||||
if (c == ' ' || c == '\t' || c == '\n')
|
||||
continue;
|
||||
else
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan for an end character.
|
||||
*
|
||||
* @return String between the current position and the end character, or
|
||||
* null.
|
||||
*/
|
||||
public String scanFor(final char end) throws XmlReaderException {
|
||||
buf.clear();
|
||||
int c = read();
|
||||
while (c != end) {
|
||||
buf.append((char) c);
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan for a specific string.
|
||||
*
|
||||
* @return String between the current position and the string.
|
||||
*/
|
||||
public String scanFor(final String s) throws XmlReaderException {
|
||||
buf.clear();
|
||||
while (!found(s)) {
|
||||
final int c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
buf.append((char) c);
|
||||
}
|
||||
for (int i = 0; i < s.length(); i++)
|
||||
read();
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan tag for an end character (interpreting " and ')
|
||||
*
|
||||
* @return String between the current position and the end character, or
|
||||
* null.
|
||||
*/
|
||||
public String scanTagFor(final char end) throws XmlReaderException {
|
||||
buf.clear();
|
||||
int c = read();
|
||||
while (c != end) {
|
||||
if (c == '\"') {
|
||||
buf.append((char) c);
|
||||
while (true) {
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
if (c == '\"')
|
||||
break;
|
||||
buf.append((char) c);
|
||||
}
|
||||
buf.append((char) c);
|
||||
} else if (c == '\'') {
|
||||
buf.append((char) c);
|
||||
while (true) {
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
if (c == '\'')
|
||||
break;
|
||||
buf.append((char) c);
|
||||
}
|
||||
buf.append((char) c);
|
||||
} else
|
||||
buf.append((char) c);
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan tag for a specific string (interpreting " and ')
|
||||
*
|
||||
* @return String between the current position and the string.
|
||||
*/
|
||||
public String scanTagFor(final String s) throws XmlReaderException {
|
||||
buf.clear();
|
||||
while (!found(s)) {
|
||||
int c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
if (c == '\"') {
|
||||
buf.append((char) c);
|
||||
while (true) {
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
if (c == '\"')
|
||||
break;
|
||||
buf.append((char) c);
|
||||
}
|
||||
buf.append((char) c);
|
||||
} else if (c == '\'') {
|
||||
buf.append((char) c);
|
||||
while (true) {
|
||||
c = read();
|
||||
if (c < 0)
|
||||
return null;
|
||||
if (c == '\'')
|
||||
break;
|
||||
buf.append((char) c);
|
||||
}
|
||||
buf.append((char) c);
|
||||
} else
|
||||
buf.append((char) c);
|
||||
}
|
||||
for (int i = 0; i < s.length(); i++)
|
||||
read();
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
String Line = null;
|
||||
int LinePos;
|
||||
|
||||
public int read() throws XmlReaderException {
|
||||
try {
|
||||
if (Line == null) {
|
||||
Line = In.readLine();
|
||||
LinePos = 0;
|
||||
// System.out.println("Read --> "+Line);
|
||||
}
|
||||
if (Line == null)
|
||||
return -1;
|
||||
if (LinePos >= Line.length()) {
|
||||
Line = null;
|
||||
return '\n';
|
||||
}
|
||||
return Line.charAt(LinePos++);
|
||||
} catch (final Exception e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return If the string is at the current line position.
|
||||
*/
|
||||
public boolean found(final String s) {
|
||||
final int n = s.length();
|
||||
if (LinePos + n > Line.length())
|
||||
return false;
|
||||
for (int i = 0; i < n; i++)
|
||||
if (s.charAt(i) != Line.charAt(LinePos + i))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void exception(final String s) throws XmlReaderException {
|
||||
throw new XmlReaderException(s, Line, LinePos);
|
||||
}
|
||||
|
||||
/**
|
||||
* A test program.
|
||||
*/
|
||||
public static void main(final String args[]) {
|
||||
try {
|
||||
final BufferedReader in = new BufferedReader(new InputStreamReader(
|
||||
new FileInputStream("rene\\util\\xml\\test.xml"), "UTF8"));
|
||||
final XmlReader reader = new XmlReader(in);
|
||||
final XmlTree tree = reader.scan();
|
||||
in.close();
|
||||
print(tree);
|
||||
} catch (final XmlReaderException e) {
|
||||
System.out.println(e.toString() + "\n" + e.getLine() + "\n"
|
||||
+ "Position : " + e.getPos());
|
||||
} catch (final IOException e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(final XmlTree t) {
|
||||
final XmlTag tag = t.getTag();
|
||||
System.out.print("<" + tag.name());
|
||||
for (int i = 0; i < tag.countParams(); i++) {
|
||||
System.out.print(" " + tag.getParam(i) + "=\"" + tag.getValue(i)
|
||||
+ "\"");
|
||||
}
|
||||
System.out.println(">");
|
||||
ListElement el = t.children().first();
|
||||
while (el != null) {
|
||||
print((XmlTree) (el.content()));
|
||||
el = el.next();
|
||||
}
|
||||
System.out.println("</" + tag.name() + ">");
|
||||
}
|
||||
|
||||
public static boolean testXml(final String s) {
|
||||
int i = 0;
|
||||
while (i < s.length()) {
|
||||
final char c = s.charAt(i);
|
||||
if (c == '<')
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
if (i >= s.length())
|
||||
return false;
|
||||
if (s.substring(i).startsWith("<?xml"))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
55
rene/util/xml/XmlReaderException.java
Normal file
55
rene/util/xml/XmlReaderException.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
public class XmlReaderException extends Exception {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
String Line;
|
||||
int Pos;
|
||||
String S;
|
||||
|
||||
public XmlReaderException(final String s, final String line, final int pos) {
|
||||
super(s);
|
||||
S = s;
|
||||
Line = line;
|
||||
Pos = pos;
|
||||
}
|
||||
|
||||
public XmlReaderException(final String s) {
|
||||
this(s, "", 0);
|
||||
}
|
||||
|
||||
public String getLine() {
|
||||
return Line;
|
||||
}
|
||||
|
||||
public int getPos() {
|
||||
return Pos;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return S;
|
||||
}
|
||||
}
|
149
rene/util/xml/XmlTag.java
Normal file
149
rene/util/xml/XmlTag.java
Normal file
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
public class XmlTag {
|
||||
protected String Tag = "";
|
||||
String Param[];
|
||||
String Value[];
|
||||
int N = 0;
|
||||
|
||||
public XmlTag(final String s) {
|
||||
int n = 0;
|
||||
int k = 0;
|
||||
n = skipBlanks(s, n);
|
||||
while (n < s.length()) {
|
||||
n = endItem(s, n);
|
||||
k++;
|
||||
n = skipBlanks(s, n);
|
||||
}
|
||||
if (k == 0)
|
||||
return;
|
||||
n = 0;
|
||||
n = skipBlanks(s, n);
|
||||
int m = endItem(s, n);
|
||||
Tag = s.substring(n, m);
|
||||
n = m;
|
||||
N = k - 1;
|
||||
Param = new String[N];
|
||||
Value = new String[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
n = skipBlanks(s, n);
|
||||
m = endItem(s, n);
|
||||
final String p = s.substring(n, m);
|
||||
n = m;
|
||||
final int kp = p.indexOf('=');
|
||||
if (kp >= 0) {
|
||||
Param[i] = p.substring(0, kp);
|
||||
Value[i] = XmlTranslator.toText(p.substring(kp + 1));
|
||||
if (Value[i].startsWith("\"") && Value[i].endsWith("\"")) {
|
||||
Value[i] = Value[i].substring(1, Value[i].length() - 1);
|
||||
} else if (Value[i].startsWith("\'") && Value[i].endsWith("\'")) {
|
||||
Value[i] = Value[i].substring(1, Value[i].length() - 1);
|
||||
}
|
||||
} else {
|
||||
Param[i] = p;
|
||||
Value[i] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int skipBlanks(final String s, int n) {
|
||||
while (n < s.length()) {
|
||||
final char c = s.charAt(n);
|
||||
if (c == ' ' || c == '\t' || c == '\n')
|
||||
n++;
|
||||
else
|
||||
break;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int endItem(final String s, int n) {
|
||||
while (n < s.length()) {
|
||||
final char c = s.charAt(n);
|
||||
if (c == ' ' || c == '\t' || c == '\n')
|
||||
break;
|
||||
if (c == '\"') {
|
||||
n++;
|
||||
while (true) {
|
||||
if (n >= s.length())
|
||||
return n;
|
||||
if (s.charAt(n) == '\"')
|
||||
break;
|
||||
n++;
|
||||
}
|
||||
} else if (c == '\'') {
|
||||
n++;
|
||||
while (true) {
|
||||
if (n >= s.length())
|
||||
return n;
|
||||
if (s.charAt(n) == '\'')
|
||||
break;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return Tag;
|
||||
}
|
||||
|
||||
public int countParams() {
|
||||
return N;
|
||||
}
|
||||
|
||||
public String getParam(final int i) {
|
||||
return Param[i];
|
||||
}
|
||||
|
||||
public String getValue(final int i) {
|
||||
return Value[i];
|
||||
}
|
||||
|
||||
public boolean hasParam(final String param) {
|
||||
for (int i = 0; i < N; i++)
|
||||
if (Param[i].equals(param))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasTrueParam(final String param) {
|
||||
for (int i = 0; i < N; i++)
|
||||
if (Param[i].equals(param)) {
|
||||
if (Value[i].equals("true"))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getValue(final String param) {
|
||||
for (int i = 0; i < N; i++)
|
||||
if (Param[i].equals(param))
|
||||
return Value[i];
|
||||
return null;
|
||||
}
|
||||
}
|
30
rene/util/xml/XmlTagPI.java
Normal file
30
rene/util/xml/XmlTagPI.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
public class XmlTagPI extends XmlTag {
|
||||
String Content;
|
||||
|
||||
public XmlTagPI(final String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
30
rene/util/xml/XmlTagRoot.java
Normal file
30
rene/util/xml/XmlTagRoot.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
public class XmlTagRoot extends XmlTag {
|
||||
String Content;
|
||||
|
||||
public XmlTagRoot() {
|
||||
super("#ROOT");
|
||||
}
|
||||
}
|
35
rene/util/xml/XmlTagText.java
Normal file
35
rene/util/xml/XmlTagText.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
public class XmlTagText extends XmlTag {
|
||||
String Content;
|
||||
|
||||
public XmlTagText(final String s) {
|
||||
super("#PCDATA");
|
||||
Content = s;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return Content;
|
||||
}
|
||||
}
|
104
rene/util/xml/XmlTranslator.java
Normal file
104
rene/util/xml/XmlTranslator.java
Normal file
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
import rene.util.SimpleStringBuffer;
|
||||
|
||||
public class XmlTranslator {
|
||||
static SimpleStringBuffer H = new SimpleStringBuffer(10000);
|
||||
|
||||
static String toXml(final String s) {
|
||||
final int m = s.length();
|
||||
H.clear();
|
||||
for (int i = 0; i < m; i++) {
|
||||
final char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '<':
|
||||
toH("<");
|
||||
break;
|
||||
case '>':
|
||||
toH(">");
|
||||
break;
|
||||
case '&':
|
||||
toH("&");
|
||||
break;
|
||||
case '\'':
|
||||
toH("'");
|
||||
break;
|
||||
case '\"':
|
||||
toH(""");
|
||||
break;
|
||||
default:
|
||||
H.append(c);
|
||||
}
|
||||
}
|
||||
return H.toString();
|
||||
}
|
||||
|
||||
static void toH(final String s) {
|
||||
final int m = s.length();
|
||||
for (int i = 0; i < m; i++) {
|
||||
H.append(s.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
static String toText(final String s) {
|
||||
final int m = s.length();
|
||||
H.clear();
|
||||
for (int i = 0; i < m; i++) {
|
||||
final char c = s.charAt(i);
|
||||
if (c == '&') {
|
||||
if (find(s, i, "<")) {
|
||||
H.append('<');
|
||||
i += 3;
|
||||
} else if (find(s, i, ">")) {
|
||||
H.append('>');
|
||||
i += 3;
|
||||
} else if (find(s, i, """)) {
|
||||
H.append('\"');
|
||||
i += 5;
|
||||
} else if (find(s, i, "'")) {
|
||||
H.append('\'');
|
||||
i += 5;
|
||||
} else if (find(s, i, "&")) {
|
||||
H.append('&');
|
||||
i += 4;
|
||||
} else
|
||||
H.append(c);
|
||||
} else
|
||||
H.append(c);
|
||||
}
|
||||
return H.toString();
|
||||
}
|
||||
|
||||
static boolean find(final String s, final int pos, final String t) {
|
||||
try {
|
||||
for (int i = 0; i < t.length(); i++) {
|
||||
if (s.charAt(pos + i) != t.charAt(i))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (final Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
119
rene/util/xml/XmlTree.java
Normal file
119
rene/util/xml/XmlTree.java
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
import rene.util.list.ListElement;
|
||||
import rene.util.list.Tree;
|
||||
import rene.util.parser.StringParser;
|
||||
|
||||
public class XmlTree extends Tree implements Enumeration {
|
||||
public XmlTree(final XmlTag t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
public XmlTag getTag() {
|
||||
return (XmlTag) content();
|
||||
}
|
||||
|
||||
public XmlTree xmlFirstContent() {
|
||||
if (firstchild() != null)
|
||||
return (XmlTree) firstchild();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isText() {
|
||||
if (!haschildren())
|
||||
return true;
|
||||
if (firstchild() != lastchild())
|
||||
return false;
|
||||
final XmlTree t = (XmlTree) firstchild();
|
||||
final XmlTag tag = t.getTag();
|
||||
if (!(tag instanceof XmlTagText))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
if (!haschildren())
|
||||
return "";
|
||||
final XmlTree t = (XmlTree) firstchild();
|
||||
final XmlTag tag = t.getTag();
|
||||
return ((XmlTagText) tag).getContent();
|
||||
}
|
||||
|
||||
ListElement Current;
|
||||
|
||||
public Enumeration getContent() {
|
||||
Current = children().first();
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasMoreElements() {
|
||||
return Current != null;
|
||||
}
|
||||
|
||||
public Object nextElement() {
|
||||
if (Current == null)
|
||||
return null;
|
||||
final XmlTree c = (XmlTree) (Current.content());
|
||||
Current = Current.next();
|
||||
return c;
|
||||
}
|
||||
|
||||
public String parseComment() throws XmlReaderException {
|
||||
final StringBuffer s = new StringBuffer();
|
||||
final Enumeration e = getContent();
|
||||
while (e.hasMoreElements()) {
|
||||
final XmlTree tree = (XmlTree) e.nextElement();
|
||||
final XmlTag tag = tree.getTag();
|
||||
if (tag.name().equals("P")) {
|
||||
if (!tree.haschildren())
|
||||
s.append("\n");
|
||||
else {
|
||||
final XmlTree h = tree.xmlFirstContent();
|
||||
String k = ((XmlTagText) h.getTag()).getContent();
|
||||
k = k.replace('\n', ' ');
|
||||
final StringParser p = new StringParser(k);
|
||||
final Vector v = p.wraplines(1000);
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
s.append((String) v.elementAt(i));
|
||||
s.append("\n");
|
||||
}
|
||||
}
|
||||
} else if (tag instanceof XmlTagText) {
|
||||
final String k = ((XmlTagText) tag).getContent();
|
||||
final StringParser p = new StringParser(k);
|
||||
final Vector v = p.wraplines(1000);
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
s.append((String) v.elementAt(i));
|
||||
s.append("\n");
|
||||
}
|
||||
} else
|
||||
throw new XmlReaderException("<" + tag.name()
|
||||
+ "> not proper here.");
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
}
|
203
rene/util/xml/XmlWriter.java
Normal file
203
rene/util/xml/XmlWriter.java
Normal file
|
@ -0,0 +1,203 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.util.xml;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Vector;
|
||||
|
||||
import rene.util.parser.StringParser;
|
||||
|
||||
public class XmlWriter {
|
||||
PrintWriter Out;
|
||||
|
||||
public XmlWriter(final PrintWriter o) {
|
||||
Out = o;
|
||||
}
|
||||
|
||||
public void printTag(final String tag, final String content) {
|
||||
startTag(tag);
|
||||
print(content);
|
||||
endTag(tag);
|
||||
}
|
||||
|
||||
public void printTagNewLine(final String tag, final String content) {
|
||||
printTag(tag, content);
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void printTag(final String tag, final String arg,
|
||||
final String value, final String content) {
|
||||
startTag(tag, arg, value);
|
||||
print(content);
|
||||
endTag(tag);
|
||||
}
|
||||
|
||||
public void printTagNewLine(final String tag, final String arg,
|
||||
final String value, final String content) {
|
||||
printTag(tag, arg, value, content);
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void startTag(final String tag) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
Out.print(">");
|
||||
}
|
||||
|
||||
public void startTag(final String tag, final String arg, final String value) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
printArg(arg, value);
|
||||
Out.print(">");
|
||||
}
|
||||
|
||||
public void finishTag(final String tag, final String arg, final String value) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
printArg(arg, value);
|
||||
Out.println("/>");
|
||||
}
|
||||
|
||||
public void finishTag(final String tag) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
Out.print("/>");
|
||||
}
|
||||
|
||||
public void finishTagNewLine(final String tag) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
Out.println("/>");
|
||||
}
|
||||
|
||||
public void startTagStart(final String tag) {
|
||||
Out.print("<");
|
||||
Out.print(tag);
|
||||
}
|
||||
|
||||
public void startTagEnd() {
|
||||
Out.print(">");
|
||||
}
|
||||
|
||||
public void finishTag() {
|
||||
Out.print("/>");
|
||||
}
|
||||
|
||||
public void finishTagNewLine() {
|
||||
Out.println("/>");
|
||||
}
|
||||
|
||||
public void startTagEndNewLine() {
|
||||
Out.println(">");
|
||||
}
|
||||
|
||||
public void printArg(final String arg, final String value) {
|
||||
Out.print(" ");
|
||||
print(arg);
|
||||
Out.print("=\"");
|
||||
print(value);
|
||||
Out.print("\"");
|
||||
}
|
||||
|
||||
public void startTagNewLine(final String tag, final String arg,
|
||||
final String value) {
|
||||
startTag(tag, arg, value);
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void startTagNewLine(final String tag) {
|
||||
startTag(tag);
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void endTag(final String tag) {
|
||||
Out.print("</");
|
||||
Out.print(tag);
|
||||
Out.print(">");
|
||||
}
|
||||
|
||||
public void endTagNewLine(final String tag) {
|
||||
endTag(tag);
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void println() {
|
||||
Out.println();
|
||||
}
|
||||
|
||||
public void print(final String s) {
|
||||
Out.print(XmlTranslator.toXml(s));
|
||||
}
|
||||
|
||||
public void println(final String s) {
|
||||
Out.println(XmlTranslator.toXml(s));
|
||||
}
|
||||
|
||||
public void printEncoding(final String s) {
|
||||
if (s.equals(""))
|
||||
Out.println("<?xml version=\"1.0\"?>");
|
||||
else
|
||||
Out.println("<?xml version=\"1.0\" encoding=\"" + s + "\"?>");
|
||||
}
|
||||
|
||||
public void printXml() {
|
||||
printEncoding("");
|
||||
}
|
||||
|
||||
public void printEncoding() {
|
||||
printEncoding("utf-8");
|
||||
}
|
||||
|
||||
public void printXls(final String s) {
|
||||
Out.println("<?xml-stylesheet href=\"" + s + "\" type=\"text/xsl\"?>");
|
||||
}
|
||||
|
||||
public void printParagraphs(String s, final int linelength) {
|
||||
final StringParser p = new StringParser(s);
|
||||
final Vector v = p.wrapwords(linelength);
|
||||
for (int i = 0; i < v.size(); i++) {
|
||||
startTag("P");
|
||||
s = (String) v.elementAt(i);
|
||||
final StringParser q = new StringParser(s);
|
||||
final Vector w = q.wraplines(linelength);
|
||||
for (int j = 0; j < w.size(); j++) {
|
||||
if (j > 0)
|
||||
println();
|
||||
s = (String) w.elementAt(j);
|
||||
print(s);
|
||||
}
|
||||
endTagNewLine("P");
|
||||
}
|
||||
}
|
||||
|
||||
public void printDoctype(final String top, final String dtd) {
|
||||
Out.print("<!DOCTYPE ");
|
||||
Out.print(top);
|
||||
Out.print(" SYSTEM \"");
|
||||
Out.print(dtd);
|
||||
Out.println("\">");
|
||||
}
|
||||
|
||||
public void close() {
|
||||
Out.close();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue