NO_LINE
if the line number is unknown.
49 | *
50 | * lineNr > 0 || lineNr == NO_LINE
52 | * message != null
65 | * message != null
91 | * lineNr > 0
92 | * NO_LINE
if the line number is unknown.
112 | * @see nanoxml.XMLParseException#NO_LINE
113 | * @uml.property name="lineNr"
114 | */
115 | public int getLineNr()
116 | {
117 | return this.lineNr;
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/CDATAReader.java:
--------------------------------------------------------------------------------
1 | /* CDATAReader.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 |
35 |
36 | /**
37 | * @author kriesen
38 | */
39 | class CDATAReader
40 | extends Reader
41 | {
42 |
43 | /**
44 | * The encapsulated reader.
45 | */
46 | private IXMLReader reader;
47 |
48 |
49 | /**
50 | * Saved char.
51 | */
52 | private char savedChar;
53 |
54 |
55 | /**
56 | * True if the end of the stream has been reached.
57 | */
58 | private boolean atEndOfData;
59 |
60 |
61 | /**
62 | * Creates the reader.
63 | *
64 | * @param reader the encapsulated reader
65 | */
66 | CDATAReader(IXMLReader reader)
67 | {
68 | this.reader = reader;
69 | this.savedChar = 0;
70 | this.atEndOfData = false;
71 | }
72 |
73 |
74 | /**
75 | * Cleans up the object when it's destroyed.
76 | */
77 | protected void finalize()
78 | throws Throwable
79 | {
80 | this.reader = null;
81 | super.finalize();
82 | }
83 |
84 |
85 | /**
86 | * Reads a block of data.
87 | *
88 | * @param buffer where to put the read data
89 | * @param offset first position in buffer to put the data
90 | * @param size maximum number of chars to read
91 | *
92 | * @return the number of chars read, or -1 if at EOF
93 | *
94 | * @throws java.io.IOException
95 | * if an error occurred reading the data
96 | */
97 | public int read(char[] buffer,
98 | int offset,
99 | int size)
100 | throws IOException
101 | {
102 | int charsRead = 0;
103 |
104 | if (this.atEndOfData) {
105 | return -1;
106 | }
107 |
108 | if ((offset + size) > buffer.length) {
109 | size = buffer.length - offset;
110 | }
111 |
112 | while (charsRead < size) {
113 | char ch = this.savedChar;
114 |
115 | if (ch == 0) {
116 | ch = this.reader.read();
117 | } else {
118 | this.savedChar = 0;
119 | }
120 |
121 | if (ch == ']') {
122 | char ch2 = this.reader.read();
123 |
124 | if (ch2 == ']') {
125 | char ch3 = this.reader.read();
126 |
127 | if (ch3 == '>') {
128 | this.atEndOfData = true;
129 | break;
130 | }
131 |
132 | this.savedChar = ch2;
133 | this.reader.unread(ch3);
134 | } else {
135 | this.reader.unread(ch2);
136 | }
137 | }
138 | buffer[charsRead] = ch;
139 | charsRead++;
140 | }
141 |
142 | if (charsRead == 0) {
143 | charsRead = -1;
144 | }
145 |
146 | return charsRead;
147 | }
148 |
149 |
150 | /**
151 | * Skips remaining data and closes the stream.
152 | *
153 | * @throws java.io.IOException
154 | * if an error occurred reading the data
155 | */
156 | public void close()
157 | throws IOException
158 | {
159 | while (! this.atEndOfData) {
160 | char ch = this.savedChar;
161 |
162 | if (ch == 0) {
163 | ch = this.reader.read();
164 | } else {
165 | this.savedChar = 0;
166 | }
167 |
168 | if (ch == ']') {
169 | char ch2 = this.reader.read();
170 |
171 | if (ch2 == ']') {
172 | char ch3 = this.reader.read();
173 |
174 | if (ch3 == '>') {
175 | break;
176 | }
177 |
178 | this.savedChar = ch2;
179 | this.reader.unread(ch3);
180 | } else {
181 | this.reader.unread(ch2);
182 | }
183 | }
184 | }
185 |
186 | this.atEndOfData = true;
187 | }
188 |
189 | }
190 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/ContentReader.java:
--------------------------------------------------------------------------------
1 | /* ContentReader.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 |
35 |
36 | /**
37 | * @author kriesen
38 | */
39 | class ContentReader
40 | extends Reader
41 | {
42 |
43 | /**
44 | * The encapsulated reader.
45 | */
46 | private IXMLReader reader;
47 |
48 |
49 | /**
50 | * Buffer.
51 | */
52 | private String buffer;
53 |
54 |
55 | /**
56 | * Pointer into the buffer.
57 | */
58 | private int bufferIndex;
59 |
60 |
61 | /**
62 | * The entity resolver.
63 | */
64 | private IXMLEntityResolver resolver;
65 |
66 |
67 | /**
68 | * Creates the reader.
69 | *
70 | * @param reader the encapsulated reader
71 | * @param resolver the entity resolver
72 | * @param buffer data that has already been read from reader
73 | */
74 | ContentReader(IXMLReader reader,
75 | IXMLEntityResolver resolver,
76 | String buffer)
77 | {
78 | this.reader = reader;
79 | this.resolver = resolver;
80 | this.buffer = buffer;
81 | this.bufferIndex = 0;
82 | }
83 |
84 |
85 | /**
86 | * Cleans up the object when it's destroyed.
87 | */
88 | protected void finalize()
89 | throws Throwable
90 | {
91 | this.reader = null;
92 | this.resolver = null;
93 | this.buffer = null;
94 | super.finalize();
95 | }
96 |
97 |
98 | /**
99 | * Reads a block of data.
100 | *
101 | * @param outputBuffer where to put the read data
102 | * @param offset first position in buffer to put the data
103 | * @param size maximum number of chars to read
104 | *
105 | * @return the number of chars read, or -1 if at EOF
106 | *
107 | * @throws java.io.IOException
108 | * if an error occurred reading the data
109 | */
110 | public int read(char[] outputBuffer,
111 | int offset,
112 | int size)
113 | throws IOException
114 | {
115 | try {
116 | int charsRead = 0;
117 | int bufferLength = this.buffer.length();
118 |
119 | if ((offset + size) > outputBuffer.length) {
120 | size = outputBuffer.length - offset;
121 | }
122 |
123 | while (charsRead < size) {
124 | String str = "";
125 | char ch;
126 |
127 | if (this.bufferIndex >= bufferLength) {
128 | str = XMLUtil.read(this.reader, '&');
129 | ch = str.charAt(0);
130 | } else {
131 | ch = this.buffer.charAt(this.bufferIndex);
132 | this.bufferIndex++;
133 | outputBuffer[charsRead] = ch;
134 | charsRead++;
135 | continue; // don't interprete chars in the buffer
136 | }
137 |
138 | if (ch == '<') {
139 | this.reader.unread(ch);
140 | break;
141 | }
142 |
143 | if ((ch == '&') && (str.length() > 1)) {
144 | if (str.charAt(1) == '#') {
145 | ch = XMLUtil.processCharLiteral(str);
146 | } else {
147 | XMLUtil.processEntity(str, this.reader, this.resolver);
148 | continue;
149 | }
150 | }
151 |
152 | outputBuffer[charsRead] = ch;
153 | charsRead++;
154 | }
155 |
156 | if (charsRead == 0) {
157 | charsRead = -1;
158 | }
159 |
160 | return charsRead;
161 | } catch (XMLParseException e) {
162 | throw new IOException(e.getMessage());
163 | }
164 | }
165 |
166 |
167 | /**
168 | * Skips remaining data and closes the stream.
169 | *
170 | * @throws java.io.IOException
171 | * if an error occurred reading the data
172 | */
173 | public void close()
174 | throws IOException
175 | {
176 | try {
177 | int bufferLength = this.buffer.length();
178 |
179 | for (;;) {
180 | String str = "";
181 | char ch;
182 |
183 | if (this.bufferIndex >= bufferLength) {
184 | str = XMLUtil.read(this.reader, '&');
185 | ch = str.charAt(0);
186 | } else {
187 | ch = this.buffer.charAt(this.bufferIndex);
188 | this.bufferIndex++;
189 | continue; // don't interprete chars in the buffer
190 | }
191 |
192 | if (ch == '<') {
193 | this.reader.unread(ch);
194 | break;
195 | }
196 |
197 | if ((ch == '&') && (str.length() > 1)) {
198 | if (str.charAt(1) != '#') {
199 | XMLUtil.processEntity(str, this.reader, this.resolver);
200 | }
201 | }
202 | }
203 | } catch (XMLParseException e) {
204 | throw new IOException(e.getMessage());
205 | }
206 | }
207 |
208 | }
209 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/IXMLBuilder.java:
--------------------------------------------------------------------------------
1 | /* IXMLBuilder.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 |
35 |
36 | /**
37 | * NanoXML uses IXMLBuilder to construct the XML data structure it retrieved
38 | * from its data source. You can supply your own builder or you can use the
39 | * default builder of NanoXML.
40 | *
41 | * If a method of the builder throws an exception, the parsing is aborted and
42 | * {@link net.n3.nanoxml.IXMLParser#parse} throws an
43 | * {@link net.n3.nanoxml.XMLException} which encasulates the original
44 | * exception.
45 | *
46 | * @see net.n3.nanoxml.IXMLParser
47 | *
48 | * @author Marc De Scheemaecker
49 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.3 $
50 | */
51 | public interface IXMLBuilder
52 | {
53 |
54 | /**
55 | * This method is called before the parser starts processing its input.
56 | *
57 | * @param systemID the system ID of the XML data source.
58 | * @param lineNr the line on which the parsing starts.
59 | *
60 | * @throws java.lang.Exception
61 | * If an exception occurred while processing the event.
62 | */
63 | public void startBuilding(String systemID,
64 | int lineNr)
65 | throws Exception;
66 |
67 |
68 | /**
69 | * This method is called when a processing instruction is encountered.
70 | * A PI with a reserved target ("xml" with any case) is never reported.
71 | *
72 | * @param target the processing instruction target.
73 | * @param reader the method can retrieve the parameter of the PI from this
74 | * reader. You may close the reader before reading all its
75 | * data and you cannot read too much data.
76 | *
77 | * @throws java.lang.Exception
78 | * If an exception occurred while processing the event.
79 | */
80 | public void newProcessingInstruction(String target,
81 | Reader reader)
82 | throws Exception;
83 |
84 |
85 | /**
86 | * This method is called when a new XML element is encountered.
87 | *
88 | * @see #endElement
89 | *
90 | * @param name the name of the element.
91 | * @param nsPrefix the prefix used to identify the namespace. If no
92 | * namespace has been specified, this parameter is null.
93 | * @param nsURI the URI associated with the namespace. If no
94 | * namespace has been specified, or no URI is
95 | * associated with nsPrefix, this parameter is null.
96 | * @param systemID the system ID of the XML data source.
97 | * @param lineNr the line in the source where the element starts.
98 | *
99 | * @throws java.lang.Exception
100 | * If an exception occurred while processing the event.
101 | */
102 | public void startElement(String name,
103 | String nsPrefix,
104 | String nsURI,
105 | String systemID,
106 | int lineNr)
107 | throws Exception;
108 |
109 |
110 | /**
111 | * This method is called when a new attribute of an XML element is
112 | * encountered.
113 | *
114 | * @param key the key (name) of the attribute.
115 | * @param nsPrefix the prefix used to identify the namespace. If no
116 | * namespace has been specified, this parameter is null.
117 | * @param nsURI the URI associated with the namespace. If no
118 | * namespace has been specified, or no URI is
119 | * associated with nsPrefix, this parameter is null.
120 | * @param value the value of the attribute.
121 | * @param type the type of the attribute. If no type is known,
122 | * "CDATA" is returned.
123 | *
124 | * @throws java.lang.Exception
125 | * If an exception occurred while processing the event.
126 | */
127 | public void addAttribute(String key,
128 | String nsPrefix,
129 | String nsURI,
130 | String value,
131 | String type)
132 | throws Exception;
133 |
134 |
135 | /**
136 | * This method is called when the attributes of an XML element have been
137 | * processed.
138 | *
139 | * @see #startElement
140 | * @see #addAttribute
141 | *
142 | * @param name the name of the element.
143 | * @param nsPrefix the prefix used to identify the namespace. If no
144 | * namespace has been specified, this parameter is null.
145 | * @param nsURI the URI associated with the namespace. If no
146 | * namespace has been specified, or no URI is
147 | * associated with nsPrefix, this parameter is null.
148 | *
149 | * @throws java.lang.Exception
150 | * If an exception occurred while processing the event.
151 | */
152 | public void elementAttributesProcessed(String name,
153 | String nsPrefix,
154 | String nsURI)
155 | throws Exception;
156 |
157 |
158 | /**
159 | * This method is called when the end of an XML elemnt is encountered.
160 | *
161 | * @see #startElement
162 | *
163 | * @param name the name of the element.
164 | * @param nsPrefix the prefix used to identify the namespace. If no
165 | * namespace has been specified, this parameter is null.
166 | * @param nsURI the URI associated with the namespace. If no
167 | * namespace has been specified, or no URI is
168 | * associated with nsPrefix, this parameter is null.
169 | *
170 | * @throws java.lang.Exception
171 | * If an exception occurred while processing the event.
172 | */
173 | public void endElement(String name,
174 | String nsPrefix,
175 | String nsURI)
176 | throws Exception;
177 |
178 |
179 | /**
180 | * This method is called when a PCDATA element is encountered. A Java
181 | * reader is supplied from which you can read the data. The reader will
182 | * only read the data of the element. You don't need to check for
183 | * boundaries. If you don't read the full element, the rest of the data
184 | * is skipped. You also don't have to care about entities: they are
185 | * resolved by the parser.
186 | *
187 | * @param reader the method can retrieve the data from this reader. You
188 | * may close the reader before reading all its data and you
189 | * cannot read too much data.
190 | * @param systemID the system ID of the XML data source.
191 | * @param lineNr the line in the source where the element starts.
192 | *
193 | * @throws java.lang.Exception
194 | * If an exception occurred while processing the event.
195 | */
196 | public void addPCData(Reader reader,
197 | String systemID,
198 | int lineNr)
199 | throws Exception;
200 |
201 |
202 | /**
203 | * Returns the result of the building process. This method is called just
204 | * before the parse method of IXMLParser returns.
205 | *
206 | * @see net.n3.nanoxml.IXMLParser#parse
207 | *
208 | * @return the result of the building process.
209 | *
210 | * @throws java.lang.Exception
211 | * If an exception occurred while processing the event.
212 | */
213 | public Object getResult()
214 | throws Exception;
215 |
216 | }
217 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/IXMLEntityResolver.java:
--------------------------------------------------------------------------------
1 | /* IXMLEntityResolver.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.util.Hashtable;
33 | import java.io.Reader;
34 | import java.io.StringReader;
35 |
36 |
37 | /**
38 | * An IXMLEntityResolver resolves entities.
39 | *
40 | * @author Marc De Scheemaecker
41 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
42 | */
43 | public interface IXMLEntityResolver
44 | {
45 |
46 | /**
47 | * Adds an internal entity.
48 | *
49 | * @param name the name of the entity.
50 | * @param value the value of the entity.
51 | */
52 | public void addInternalEntity(String name,
53 | String value);
54 |
55 |
56 | /**
57 | * Adds an external entity.
58 | *
59 | * @param name the name of the entity.
60 | * @param publicID the public ID of the entity, which may be null.
61 | * @param systemID the system ID of the entity.
62 | */
63 | public void addExternalEntity(String name,
64 | String publicID,
65 | String systemID);
66 |
67 |
68 | /**
69 | * Returns a Java reader containing the value of an entity.
70 | *
71 | * @param xmlReader the current NanoXML reader.
72 | * @param name the name of the entity.
73 | *
74 | * @return the reader, or null if the entity could not be resolved.
75 | *
76 | * @throws net.n3.nanoxml.XMLParseException
77 | * If an exception occurred while resolving the entity.
78 | */
79 | public Reader getEntity(IXMLReader xmlReader,
80 | String name)
81 | throws XMLParseException;
82 |
83 |
84 | /**
85 | * Returns true if an entity is external.
86 | *
87 | * @param name the name of the entity.
88 | */
89 | public boolean isExternalEntity(String name);
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/IXMLParser.java:
--------------------------------------------------------------------------------
1 | /* IXMLParser.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.IOException;
33 |
34 |
35 | /**
36 | * @author kriesen
37 | */
38 | public interface IXMLParser
39 | {
40 |
41 | /**
42 | * Sets the reader from which the parser retrieves its data.
43 | * @param reader the reader.
44 | * @uml.property name="reader"
45 | */
46 | public void setReader(IXMLReader reader);
47 |
48 |
49 | /**
50 | * Returns the reader from which the parser retrieves its data.
51 | *
52 | * @return the reader.
53 | */
54 | public IXMLReader getReader();
55 |
56 |
57 | /**
58 | * Sets the builder which creates the logical structure of the XML data.
59 | * @param builder the builder.
60 | * @uml.property name="builder"
61 | */
62 | public void setBuilder(IXMLBuilder builder);
63 |
64 |
65 | /**
66 | * Returns the builder which creates the logical structure of the XML data.
67 | *
68 | * @return the builder.
69 | */
70 | public IXMLBuilder getBuilder();
71 |
72 |
73 | /**
74 | * Sets the validator that validates the XML data.
75 | * @param validator the validator.
76 | * @uml.property name="validator"
77 | */
78 | public void setValidator(IXMLValidator validator);
79 |
80 |
81 | /**
82 | * Returns the validator that validates the XML data.
83 | *
84 | * @return the validator.
85 | */
86 | public IXMLValidator getValidator();
87 |
88 |
89 | /**
90 | * Sets the entity resolver.
91 | * @param resolver the non-null resolver.
92 | * @uml.property name="resolver"
93 | */
94 | public void setResolver(IXMLEntityResolver resolver);
95 |
96 |
97 | /**
98 | * Returns the entity resolver.
99 | *
100 | * @return the non-null resolver.
101 | */
102 | public IXMLEntityResolver getResolver();
103 |
104 |
105 | /**
106 | * Parses the data and lets the builder create the logical data structure.
107 | * The method returns the result of getResult of the builder. if an
108 | * error occurred while reading or parsing the data, the method may throw
109 | * an XMLException.
110 | *
111 | * @see net.n3.nanoxml.IXMLBuilder#getResult
112 | *
113 | * @return the logical structure built by the builder.
114 | *
115 | * @throws net.n3.nanoxml.XMLException
116 | * if an error occurred reading or parsing the data
117 | */
118 | public Object parse()
119 | throws XMLException;
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/IXMLReader.java:
--------------------------------------------------------------------------------
1 | /* IXMLReader.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 | import java.io.FileNotFoundException;
35 | import java.net.MalformedURLException;
36 |
37 |
38 | /**
39 | * @author kriesen
40 | */
41 | public interface IXMLReader
42 | {
43 |
44 | /**
45 | * Reads a character.
46 | *
47 | * @return the character
48 | *
49 | * @throws java.io.IOException
50 | * If no character could be read.
51 | */
52 | public char read()
53 | throws IOException;
54 |
55 |
56 | /**
57 | * Returns true if the current stream has no more characters left to be
58 | * read.
59 | *
60 | * @throws java.io.IOException
61 | * If an I/O error occurred.
62 | */
63 | public boolean atEOFOfCurrentStream()
64 | throws IOException;
65 |
66 |
67 | /**
68 | * Returns true if there are no more characters left to be read.
69 | *
70 | * @throws java.io.IOException
71 | * If an I/O error occurred.
72 | */
73 | public boolean atEOF()
74 | throws IOException;
75 |
76 |
77 | /**
78 | * Pushes the last character read back to the stream.
79 | *
80 | * @param ch the character to push back.
81 | *
82 | * @throws java.io.IOException
83 | * If an I/O error occurred.
84 | */
85 | public void unread(char ch)
86 | throws IOException;
87 |
88 |
89 | /**
90 | * Returns the line number of the data in the current stream.
91 | */
92 | public int getLineNr();
93 |
94 |
95 | /**
96 | * Opens a stream from a public and system ID.
97 | *
98 | * @param publicID the public ID, which may be null.
99 | * @param systemID the system ID, which is never null.
100 | *
101 | * @throws java.net.MalformedURLException
102 | * If the system ID does not contain a valid URL.
103 | * @throws java.io.FileNotFoundException
104 | * If the system ID refers to a local file which does not exist.
105 | * @throws java.io.IOException
106 | * If an error occurred opening the stream.
107 | */
108 | public Reader openStream(String publicID,
109 | String systemID)
110 | throws MalformedURLException,
111 | FileNotFoundException,
112 | IOException;
113 |
114 |
115 | /**
116 | * Starts a new stream from a Java reader. The new stream is used
117 | * temporary to read data from. If that stream is exhausted, control
118 | * returns to the "parent" stream.
119 | *
120 | * @param reader the reader to read the new data from.
121 | */
122 | public void startNewStream(Reader reader);
123 |
124 |
125 | /**
126 | * Starts a new stream from a Java reader. The new stream is used
127 | * temporary to read data from. If that stream is exhausted, control
128 | * returns to the parent stream.
129 | *
130 | * @param reader the non-null reader to read the new data from
131 | * @param isInternalEntity true if the reader is produced by resolving
132 | * an internal entity
133 | */
134 | public void startNewStream(Reader reader,
135 | boolean isInternalEntity);
136 |
137 |
138 | /**
139 | * Returns the current "level" of the stream on the stack of streams.
140 | */
141 | public int getStreamLevel();
142 |
143 |
144 | /**
145 | * Sets the system ID of the current stream.
146 | * @param systemID the system ID.
147 | * @throws java.net.MalformedURLException If the system ID does not contain a valid URL.
148 | * @uml.property name="systemID"
149 | */
150 | public void setSystemID(String systemID)
151 | throws MalformedURLException;
152 |
153 |
154 | /**
155 | * Sets the public ID of the current stream.
156 | * @param publicID the public ID.
157 | * @uml.property name="publicID"
158 | */
159 | public void setPublicID(String publicID);
160 |
161 |
162 | /**
163 | * Returns the current system ID.
164 | * @uml.property name="systemID"
165 | */
166 | public String getSystemID();
167 |
168 |
169 | /**
170 | * Returns the current public ID.
171 | * @uml.property name="publicID"
172 | */
173 | public String getPublicID();
174 |
175 | }
176 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/IXMLValidator.java:
--------------------------------------------------------------------------------
1 | /* IXMLValidator.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 | import java.util.Properties;
35 |
36 |
37 | /**
38 | * @author kriesen
39 | */
40 | public interface IXMLValidator
41 | {
42 |
43 | /**
44 | * Sets the parameter entity resolver.
45 | * @param resolver the entity resolver.
46 | * @uml.property name="parameterEntityResolver"
47 | */
48 | public void setParameterEntityResolver(IXMLEntityResolver resolver);
49 |
50 |
51 | /**
52 | * Returns the parameter entity resolver.
53 | *
54 | * @return the entity resolver.
55 | */
56 | public IXMLEntityResolver getParameterEntityResolver();
57 |
58 |
59 | /**
60 | * Parses the DTD. The validator object is responsible for reading the
61 | * full DTD.
62 | *
63 | * @param publicID the public ID, which may be null.
64 | * @param reader the reader to read the DTD from.
65 | * @param entityResolver the entity resolver.
66 | * @param external true if the DTD is external.
67 | *
68 | * @throws java.lang.Exception
69 | * If something went wrong.
70 | */
71 | public void parseDTD(String publicID,
72 | IXMLReader reader,
73 | IXMLEntityResolver entityResolver,
74 | boolean external)
75 | throws Exception;
76 |
77 |
78 | /**
79 | * Indicates that an element has been started.
80 | *
81 | * @param name the name of the element.
82 | * @param systemId the system ID of the XML data of the element.
83 | * @param lineNr the line number in the XML data of the element.
84 | *
85 | * @throws java.lang.Exception
86 | * If the element could not be validated.
87 | */
88 | public void elementStarted(String name,
89 | String systemId,
90 | int lineNr)
91 | throws Exception;
92 |
93 |
94 | /**
95 | * Indicates that the current element has ended.
96 | *
97 | * @param name the name of the element.
98 | * @param systemId the system ID of the XML data of the element.
99 | * @param lineNr the line number in the XML data of the element.
100 | *
101 | * @throws java.lang.Exception
102 | * If the element could not be validated.
103 | */
104 | public void elementEnded(String name,
105 | String systemId,
106 | int lineNr)
107 | throws Exception;
108 |
109 |
110 | /**
111 | * Indicates that an attribute has been added to the current element.
112 | *
113 | * @param key the name of the attribute.
114 | * @param value the value of the attribute.
115 | * @param systemId the system ID of the XML data of the element.
116 | * @param lineNr the line number in the XML data of the element.
117 | *
118 | * @throws java.lang.Exception
119 | * If the attribute could not be validated.
120 | */
121 | public void attributeAdded(String key,
122 | String value,
123 | String systemId,
124 | int lineNr)
125 | throws Exception;
126 |
127 |
128 | /**
129 | * This method is called when the attributes of an XML element have been
130 | * processed.
131 | * If there are attributes with a default value which have not been
132 | * specified yet, they have to be put into extraAttributes.
133 | *
134 | * @param name the name of the element.
135 | * @param extraAttributes where to put extra attributes.
136 | * @param systemId the system ID of the XML data of the element.
137 | * @param lineNr the line number in the XML data of the element.
138 | *
139 | * @throws java.lang.Exception
140 | * if the element could not be validated.
141 | */
142 | public void elementAttributesProcessed(String name,
143 | Properties extraAttributes,
144 | String systemId,
145 | int lineNr)
146 | throws Exception;
147 |
148 |
149 | /**
150 | * Indicates that a new #PCDATA element has been encountered.
151 | *
152 | * @param systemId the system ID of the XML data of the element.
153 | * @param lineNr the line number in the XML data of the element.
154 | *
155 | * @throws java.lang.Exception
156 | * if the element could not be validated.
157 | */
158 | public void PCDataAdded(String systemId,
159 | int lineNr)
160 | throws Exception;
161 |
162 | }
163 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/PIReader.java:
--------------------------------------------------------------------------------
1 | /* PIReader.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.Reader;
33 | import java.io.IOException;
34 |
35 |
36 | /**
37 | * @author kriesen
38 | */
39 | class PIReader
40 | extends Reader
41 | {
42 |
43 | /**
44 | * The encapsulated reader.
45 | */
46 | private IXMLReader reader;
47 |
48 |
49 | /**
50 | * True if the end of the stream has been reached.
51 | */
52 | private boolean atEndOfData;
53 |
54 |
55 | /**
56 | * Creates the reader.
57 | *
58 | * @param reader the encapsulated reader
59 | */
60 | PIReader(IXMLReader reader)
61 | {
62 | this.reader = reader;
63 | this.atEndOfData = false;
64 | }
65 |
66 |
67 | /**
68 | * Cleans up the object when it's destroyed.
69 | */
70 | protected void finalize()
71 | throws Throwable
72 | {
73 | this.reader = null;
74 | super.finalize();
75 | }
76 |
77 |
78 | /**
79 | * Reads a block of data.
80 | *
81 | * @param buffer where to put the read data
82 | * @param offset first position in buffer to put the data
83 | * @param size maximum number of chars to read
84 | *
85 | * @return the number of chars read, or -1 if at EOF
86 | *
87 | * @throws java.io.IOException
88 | * if an error occurred reading the data
89 | */
90 | public int read(char[] buffer,
91 | int offset,
92 | int size)
93 | throws IOException
94 | {
95 | if (this.atEndOfData) {
96 | return -1;
97 | }
98 |
99 | int charsRead = 0;
100 |
101 | if ((offset + size) > buffer.length) {
102 | size = buffer.length - offset;
103 | }
104 |
105 | while (charsRead < size) {
106 | char ch = this.reader.read();
107 |
108 | if (ch == '?') {
109 | char ch2 = this.reader.read();
110 |
111 | if (ch2 == '>') {
112 | this.atEndOfData = true;
113 | break;
114 | }
115 |
116 | this.reader.unread(ch2);
117 | }
118 |
119 | buffer[charsRead] = ch;
120 | charsRead++;
121 | }
122 |
123 | if (charsRead == 0) {
124 | charsRead = -1;
125 | }
126 |
127 | return charsRead;
128 | }
129 |
130 |
131 | /**
132 | * Skips remaining data and closes the stream.
133 | *
134 | * @throws java.io.IOException
135 | * if an error occurred reading the data
136 | */
137 | public void close()
138 | throws IOException
139 | {
140 | while (! this.atEndOfData) {
141 | char ch = this.reader.read();
142 |
143 | if (ch == '?') {
144 | char ch2 = this.reader.read();
145 |
146 | if (ch2 == '>') {
147 | this.atEndOfData = true;
148 | }
149 | }
150 | }
151 | }
152 |
153 | }
154 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLAttribute.java:
--------------------------------------------------------------------------------
1 | /* XMLAttribute.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | /**
33 | * @author kriesen
34 | */
35 | class XMLAttribute
36 | {
37 |
38 | /**
39 | * The full name of the attribute.
40 | */
41 | private String fullName;
42 |
43 |
44 | /**
45 | * The short name of the attribute.
46 | */
47 | private String name;
48 |
49 |
50 | /**
51 | * The namespace URI of the attribute.
52 | */
53 | private String namespace;
54 |
55 |
56 | /**
57 | * The value of the attribute.
58 | */
59 | private String value;
60 |
61 |
62 | /**
63 | * The type of the attribute.
64 | */
65 | private String type;
66 |
67 |
68 | /**
69 | * Creates a new attribute.
70 | *
71 | * @param fullName the non-null full name
72 | * @param name the non-null short name
73 | * @param namespace the namespace URI, which may be null
74 | * @param value the value of the attribute
75 | * @param type the type of the attribute
76 | */
77 | XMLAttribute(String fullName,
78 | String name,
79 | String namespace,
80 | String value,
81 | String type)
82 | {
83 | this.fullName = fullName;
84 | this.name = name;
85 | this.namespace = namespace;
86 | this.value = value;
87 | this.type = type;
88 | }
89 |
90 |
91 | /**
92 | * Returns the full name of the attribute.
93 | * @uml.property name="fullName"
94 | */
95 | String getFullName()
96 | {
97 | return this.fullName;
98 | }
99 |
100 |
101 | /**
102 | * Returns the short name of the attribute.
103 | * @uml.property name="name"
104 | */
105 | String getName()
106 | {
107 | return this.name;
108 | }
109 |
110 |
111 | /**
112 | * Returns the namespace of the attribute.
113 | * @uml.property name="namespace"
114 | */
115 | String getNamespace()
116 | {
117 | return this.namespace;
118 | }
119 |
120 |
121 | /**
122 | * Returns the value of the attribute.
123 | * @uml.property name="value"
124 | */
125 | String getValue()
126 | {
127 | return this.value;
128 | }
129 |
130 |
131 | /**
132 | * Sets the value of the attribute.
133 | * @param value the new value.
134 | * @uml.property name="value"
135 | */
136 | void setValue(String value)
137 | {
138 | this.value = value;
139 | }
140 |
141 |
142 | /**
143 | * Returns the type of the attribute.
144 | * @param type the new type.
145 | * @uml.property name="type"
146 | */
147 | String getType()
148 | {
149 | return this.type;
150 | }
151 |
152 | }
153 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLEntityResolver.java:
--------------------------------------------------------------------------------
1 | /* XMLEntityResolver.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.util.Hashtable;
33 | import java.io.Reader;
34 | import java.io.StringReader;
35 |
36 |
37 | /**
38 | * An XMLEntityResolver resolves entities.
39 | *
40 | * @author Marc De Scheemaecker
41 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
42 | */
43 | public class XMLEntityResolver
44 | implements IXMLEntityResolver
45 | {
46 |
47 | /**
48 | * The entities.
49 | */
50 | private Hashtable entities;
51 |
52 |
53 | /**
54 | * Initializes the resolver.
55 | */
56 | public XMLEntityResolver()
57 | {
58 | this.entities = new Hashtable();
59 | this.entities.put("amp", "&");
60 | this.entities.put("quot", """);
61 | this.entities.put("apos", "'");
62 | this.entities.put("lt", "<");
63 | this.entities.put("gt", ">");
64 | }
65 |
66 |
67 | /**
68 | * Cleans up the object when it's destroyed.
69 | */
70 | protected void finalize()
71 | throws Throwable
72 | {
73 | this.entities.clear();
74 | this.entities = null;
75 | super.finalize();
76 | }
77 |
78 |
79 | /**
80 | * Adds an internal entity.
81 | *
82 | * @param name the name of the entity.
83 | * @param value the value of the entity.
84 | */
85 | public void addInternalEntity(String name,
86 | String value)
87 | {
88 | if (! this.entities.containsKey(name)) {
89 | this.entities.put(name, value);
90 | }
91 | }
92 |
93 |
94 | /**
95 | * Adds an external entity.
96 | *
97 | * @param name the name of the entity.
98 | * @param publicID the public ID of the entity, which may be null.
99 | * @param systemID the system ID of the entity.
100 | */
101 | public void addExternalEntity(String name,
102 | String publicID,
103 | String systemID)
104 | {
105 | if (! this.entities.containsKey(name)) {
106 | this.entities.put(name, new String[] { publicID, systemID } );
107 | }
108 | }
109 |
110 |
111 | /**
112 | * Returns a Java reader containing the value of an entity.
113 | *
114 | * @param xmlReader the current XML reader
115 | * @param name the name of the entity.
116 | *
117 | * @return the reader, or null if the entity could not be resolved.
118 | */
119 | public Reader getEntity(IXMLReader xmlReader,
120 | String name)
121 | throws XMLParseException
122 | {
123 | Object obj = this.entities.get(name);
124 |
125 | if (obj == null) {
126 | return null;
127 | } else if (obj instanceof java.lang.String) {
128 | return new StringReader((String)obj);
129 | } else {
130 | String[] id = (String[]) obj;
131 | return this.openExternalEntity(xmlReader, id[0], id[1]);
132 | }
133 | }
134 |
135 |
136 | /**
137 | * Returns true if an entity is external.
138 | *
139 | * @param name the name of the entity.
140 | */
141 | public boolean isExternalEntity(String name)
142 | {
143 | Object obj = this.entities.get(name);
144 | return ! (obj instanceof java.lang.String);
145 | }
146 |
147 |
148 | /**
149 | * Opens an external entity.
150 | *
151 | * @param xmlReader the current XML reader
152 | * @param publicID the public ID, which may be null
153 | * @param systemID the system ID
154 | *
155 | * @return the reader, or null if the reader could not be created/opened
156 | */
157 | protected Reader openExternalEntity(IXMLReader xmlReader,
158 | String publicID,
159 | String systemID)
160 | throws XMLParseException
161 | {
162 | String parentSystemID = xmlReader.getSystemID();
163 |
164 | try {
165 | return xmlReader.openStream(publicID, systemID);
166 | } catch (Exception e) {
167 | throw new XMLParseException(parentSystemID,
168 | xmlReader.getLineNr(),
169 | "Could not open external entity "
170 | + "at system ID: " + systemID);
171 | }
172 | }
173 |
174 | }
175 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLException.java:
--------------------------------------------------------------------------------
1 | /* XMLException.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 | import java.io.PrintStream;
32 | import java.io.PrintWriter;
33 |
34 |
35 | /**
36 | * @author kriesen
37 | */
38 | public class XMLException
39 | extends Exception
40 | {
41 |
42 | /**
43 | * The message of the exception.
44 | */
45 | private String msg;
46 |
47 |
48 | /**
49 | * The system ID of the XML data where the exception occurred.
50 | */
51 | private String systemID;
52 |
53 |
54 | /**
55 | * The line number in the XML data where the exception occurred.
56 | */
57 | private int lineNr;
58 |
59 |
60 | /**
61 | * Encapsulated exception.
62 | */
63 | private Exception encapsulatedException;
64 |
65 |
66 | /**
67 | * Creates a new exception.
68 | *
69 | * @param msg the message of the exception.
70 | */
71 | public XMLException(String msg)
72 | {
73 | this(null, -1, null, msg, false);
74 | }
75 |
76 |
77 | /**
78 | * Creates a new exception.
79 | *
80 | * @param e the encapsulated exception.
81 | */
82 | public XMLException(Exception e)
83 | {
84 | this(null, -1, e, "Nested Exception", false);
85 | }
86 |
87 |
88 | /**
89 | * Creates a new exception.
90 | *
91 | * @param systemID the system ID of the XML data where the exception
92 | * occurred
93 | * @param lineNr the line number in the XML data where the exception
94 | * occurred.
95 | * @param e the encapsulated exception.
96 | */
97 | public XMLException(String systemID,
98 | int lineNr,
99 | Exception e)
100 | {
101 | this(systemID, lineNr, e, "Nested Exception", true);
102 | }
103 |
104 |
105 | /**
106 | * Creates a new exception.
107 | *
108 | * @param systemID the system ID of the XML data where the exception
109 | * occurred
110 | * @param lineNr the line number in the XML data where the exception
111 | * occurred.
112 | * @param msg the message of the exception.
113 | */
114 | public XMLException(String systemID,
115 | int lineNr,
116 | String msg)
117 | {
118 | this(systemID, lineNr, null, msg, true);
119 | }
120 |
121 |
122 | /**
123 | * Creates a new exception.
124 | *
125 | * @param systemID the system ID from where the data came
126 | * @param lineNr the line number in the XML data where the exception
127 | * occurred.
128 | * @param e the encapsulated exception.
129 | * @param msg the message of the exception.
130 | * @param reportParams true if the systemID, lineNr and e params need to be
131 | * appended to the message
132 | */
133 | public XMLException(String systemID,
134 | int lineNr,
135 | Exception e,
136 | String msg,
137 | boolean reportParams)
138 | {
139 | super(XMLException.buildMessage(systemID, lineNr, e, msg,
140 | reportParams));
141 | this.systemID = systemID;
142 | this.lineNr = lineNr;
143 | this.encapsulatedException = e;
144 | this.msg = XMLException.buildMessage(systemID, lineNr, e, msg,
145 | reportParams);
146 | }
147 |
148 |
149 | /**
150 | * Builds the exception message
151 | *
152 | * @param systemID the system ID from where the data came
153 | * @param lineNr the line number in the XML data where the exception
154 | * occurred.
155 | * @param e the encapsulated exception.
156 | * @param msg the message of the exception.
157 | * @param reportParams true if the systemID, lineNr and e params need to be
158 | * appended to the message
159 | */
160 | private static String buildMessage(String systemID,
161 | int lineNr,
162 | Exception e,
163 | String msg,
164 | boolean reportParams)
165 | {
166 | String str = msg;
167 |
168 | if (reportParams) {
169 | if (systemID != null) {
170 | str += ", SystemID='" + systemID + "'";
171 | }
172 |
173 | if (lineNr >= 0) {
174 | str += ", Line=" + lineNr;
175 | }
176 |
177 | if (e != null) {
178 | str += ", Exception: " + e;
179 | }
180 | }
181 |
182 | return str;
183 | }
184 |
185 |
186 | /**
187 | * Cleans up the object when it's destroyed.
188 | */
189 | protected void finalize()
190 | throws Throwable
191 | {
192 | this.systemID = null;
193 | this.encapsulatedException = null;
194 | super.finalize();
195 | }
196 |
197 |
198 | /**
199 | * Returns the system ID of the XML data where the exception occurred. If there is no system ID known, null is returned.
200 | * @uml.property name="systemID"
201 | */
202 | public String getSystemID()
203 | {
204 | return this.systemID;
205 | }
206 |
207 |
208 | /**
209 | * Returns the line number in the XML data where the exception occurred. If there is no line number known, -1 is returned.
210 | * @uml.property name="lineNr"
211 | */
212 | public int getLineNr()
213 | {
214 | return this.lineNr;
215 | }
216 |
217 |
218 | /**
219 | * Returns the encapsulated exception, or null if no exception is
220 | * encapsulated.
221 | */
222 | public Exception getException()
223 | {
224 | return this.encapsulatedException;
225 | }
226 |
227 |
228 | /**
229 | * Dumps the exception stack to a print writer.
230 | *
231 | * @param writer the print writer
232 | */
233 | public void printStackTrace(PrintWriter writer)
234 | {
235 | super.printStackTrace(writer);
236 |
237 | if (this.encapsulatedException != null) {
238 | writer.println("*** Nested Exception:");
239 | this.encapsulatedException.printStackTrace(writer);
240 | }
241 | }
242 |
243 |
244 | /**
245 | * Dumps the exception stack to an output stream.
246 | *
247 | * @param stream the output stream
248 | */
249 | public void printStackTrace(PrintStream stream)
250 | {
251 | super.printStackTrace(stream);
252 |
253 | if (this.encapsulatedException != null) {
254 | stream.println("*** Nested Exception:");
255 | this.encapsulatedException.printStackTrace(stream);
256 | }
257 | }
258 |
259 |
260 | /**
261 | * Dumps the exception stack to System.err.
262 | */
263 | public void printStackTrace()
264 | {
265 | super.printStackTrace();
266 |
267 | if (this.encapsulatedException != null) {
268 | System.err.println("*** Nested Exception:");
269 | this.encapsulatedException.printStackTrace();
270 | }
271 | }
272 |
273 |
274 | /**
275 | * Returns a string representation of the exception.
276 | */
277 | public String toString()
278 | {
279 | return this.msg;
280 | }
281 |
282 | }
283 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLParseException.java:
--------------------------------------------------------------------------------
1 | /* XMLParseException.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | /**
33 | * An XMLParseException is thrown when the XML passed to the XML parser is not
34 | * well-formed.
35 | *
36 | * @author Marc De Scheemaecker
37 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.3 $
38 | */
39 | public class XMLParseException
40 | extends XMLException
41 | {
42 |
43 | /**
44 | * Creates a new exception.
45 | *
46 | * @param msg the message of the exception.
47 | */
48 | public XMLParseException(String msg)
49 | {
50 | super(msg);
51 | }
52 |
53 |
54 | /**
55 | * Creates a new exception.
56 | *
57 | * @param systemID the system ID from where the data came
58 | * @param lineNr the line number in the XML data where the exception
59 | * occurred.
60 | * @param msg the message of the exception.
61 | */
62 | public XMLParseException(String systemID,
63 | int lineNr,
64 | String msg)
65 | {
66 | super(systemID, lineNr, null, msg, true);
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLParserFactory.java:
--------------------------------------------------------------------------------
1 | /* XMLParserFactory.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.IOException;
33 |
34 |
35 | /**
36 | * Creates an XML parser.
37 | *
38 | * @author Marc De Scheemaecker
39 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.3 $
40 | */
41 | public class XMLParserFactory
42 | {
43 |
44 | /**
45 | * The class name of the default XML parser.
46 | */
47 | public static final String DEFAULT_CLASS = "net.n3.nanoxml.StdXMLParser";
48 |
49 |
50 | /**
51 | * The Java properties key of the XML parser class name.
52 | */
53 | public static final String CLASS_KEY = "net.n3.nanoxml.XMLParser";
54 |
55 |
56 | /**
57 | * Creates a default parser.
58 | *
59 | * @see #DEFAULT_CLASS
60 | * @see #CLASS_KEY
61 | *
62 | * @return the non-null parser.
63 | *
64 | * @throws java.lang.ClassNotFoundException
65 | * if the class of the parser or validator could not be found.
66 | * @throws java.lang.InstantiationException
67 | * if the parser could not be created
68 | * @throws java.lang.IllegalAccessException
69 | * if the parser could not be created
70 | */
71 | public static IXMLParser createDefaultXMLParser()
72 | throws ClassNotFoundException,
73 | InstantiationException,
74 | IllegalAccessException
75 | {
76 | String className = System.getProperty(XMLParserFactory.CLASS_KEY,
77 | XMLParserFactory.DEFAULT_CLASS);
78 | return XMLParserFactory.createXMLParser(className,
79 | new StdXMLBuilder());
80 | }
81 |
82 |
83 | /**
84 | * Creates a default parser.
85 | *
86 | * @see #DEFAULT_CLASS
87 | * @see #CLASS_KEY
88 | *
89 | * @param builder the XML builder.
90 | *
91 | * @return the non-null parser.
92 | *
93 | * @throws java.lang.ClassNotFoundException
94 | * if the class of the parser could not be found.
95 | * @throws java.lang.InstantiationException
96 | * if the parser could not be created
97 | * @throws java.lang.IllegalAccessException
98 | * if the parser could not be created
99 | */
100 | public static IXMLParser createDefaultXMLParser(IXMLBuilder builder)
101 | throws ClassNotFoundException,
102 | InstantiationException,
103 | IllegalAccessException
104 | {
105 | String className = System.getProperty(XMLParserFactory.CLASS_KEY,
106 | XMLParserFactory.DEFAULT_CLASS);
107 | return XMLParserFactory.createXMLParser(className, builder);
108 | }
109 |
110 |
111 | /**
112 | * Creates a parser.
113 | *
114 | * @param className the name of the class of the XML parser
115 | * @param builder the XML builder.
116 | *
117 | * @return the non-null parser.
118 | *
119 | * @throws java.lang.ClassNotFoundException
120 | * if the class of the parser could not be found.
121 | * @throws java.lang.InstantiationException
122 | * if the parser could not be created
123 | * @throws java.lang.IllegalAccessException
124 | * if the parser could not be created
125 | */
126 | public static IXMLParser createXMLParser(String className,
127 | IXMLBuilder builder)
128 | throws ClassNotFoundException,
129 | InstantiationException,
130 | IllegalAccessException
131 | {
132 | Class cls = Class.forName(className);
133 | IXMLParser parser = (IXMLParser) cls.newInstance();
134 | parser.setBuilder(builder);
135 | parser.setValidator(new NonValidator());
136 | return parser;
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLValidationException.java:
--------------------------------------------------------------------------------
1 | /* XMLValidationException.java NanoXML/Java
2 | *
3 | * $Revision: 1.3 $
4 | * $Date: 2002/01/04 21:03:29 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | /**
33 | * @author kriesen
34 | */
35 | public class XMLValidationException
36 | extends XMLException
37 | {
38 |
39 | /**
40 | * An element was missing.
41 | */
42 | public static final int MISSING_ELEMENT = 1;
43 |
44 |
45 | /**
46 | * An unexpected element was encountered.
47 | */
48 | public static final int UNEXPECTED_ELEMENT = 2;
49 |
50 |
51 | /**
52 | * An attribute was missing.
53 | */
54 | public static final int MISSING_ATTRIBUTE = 3;
55 |
56 |
57 | /**
58 | * An unexpected attribute was encountered.
59 | */
60 | public static final int UNEXPECTED_ATTRIBUTE = 4;
61 |
62 |
63 | /**
64 | * An attribute has an invalid value.
65 | */
66 | public static final int ATTRIBUTE_WITH_INVALID_VALUE = 5;
67 |
68 |
69 | /**
70 | * A PCDATA element was missing.
71 | */
72 | public static final int MISSING_PCDATA = 6;
73 |
74 |
75 | /**
76 | * An unexpected PCDATA element was encountered.
77 | */
78 | public static final int UNEXPECTED_PCDATA = 7;
79 |
80 |
81 | /**
82 | * Another error than those specified in this class was encountered.
83 | */
84 | public static final int MISC_ERROR = 0;
85 |
86 |
87 | /**
88 | * Which error occurred.
89 | */
90 | private int errorType;
91 |
92 |
93 | /**
94 | * The name of the element where the exception occurred.
95 | */
96 | private String elementName;
97 |
98 |
99 | /**
100 | * The name of the attribute where the exception occurred.
101 | */
102 | private String attributeName;
103 |
104 |
105 | /**
106 | * The value of the attribute where the exception occurred.
107 | */
108 | private String attributeValue;
109 |
110 |
111 | /**
112 | * Creates a new exception.
113 | *
114 | * @param errorType the type of validity error
115 | * @param systemID the system ID from where the data came
116 | * @param lineNr the line number in the XML data where the
117 | * exception occurred.
118 | * @param elementName the name of the offending element
119 | * @param attributeName the name of the offending attribute
120 | * @param attributeValue the value of the offending attribute
121 | * @param msg the message of the exception.
122 | */
123 | public XMLValidationException(int errorType,
124 | String systemID,
125 | int lineNr,
126 | String elementName,
127 | String attributeName,
128 | String attributeValue,
129 | String msg)
130 | {
131 | super(systemID, lineNr, null,
132 | msg + ((elementName == null) ? "" : (", element=" + elementName))
133 | + ((attributeName == null) ? ""
134 | : (", attribute=" + attributeName))
135 | + ((attributeValue == null) ? ""
136 | : (", value='" + attributeValue + "'")),
137 | false);
138 | this.elementName = elementName;
139 | this.attributeName = attributeName;
140 | this.attributeValue = attributeValue;
141 | }
142 |
143 |
144 | /**
145 | * Cleans up the object when it's destroyed.
146 | */
147 | protected void finalize()
148 | throws Throwable
149 | {
150 | this.elementName = null;
151 | this.attributeName = null;
152 | this.attributeValue = null;
153 | super.finalize();
154 | }
155 |
156 |
157 | /**
158 | * Returns the name of the element in which the validation is violated. If there is no current element, null is returned.
159 | * @uml.property name="elementName"
160 | */
161 | public String getElementName()
162 | {
163 | return this.elementName;
164 | }
165 |
166 |
167 | /**
168 | * Returns the name of the attribute in which the validation is violated. If there is no current attribute, null is returned.
169 | * @uml.property name="attributeName"
170 | */
171 | public String getAttributeName()
172 | {
173 | return this.attributeName;
174 | }
175 |
176 |
177 | /**
178 | * Returns the value of the attribute in which the validation is violated. If there is no current attribute, null is returned.
179 | * @uml.property name="attributeValue"
180 | */
181 | public String getAttributeValue()
182 | {
183 | return this.attributeValue;
184 | }
185 |
186 | }
187 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/XMLWriter.java:
--------------------------------------------------------------------------------
1 | /* XMLWriter.java NanoXML/Java
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/03/24 11:37:51 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml;
30 |
31 |
32 | import java.io.IOException;
33 | import java.io.OutputStream;
34 | import java.io.PrintWriter;
35 | import java.io.Writer;
36 | import java.util.Enumeration;
37 | import java.util.Vector;
38 |
39 |
40 | /**
41 | * An XMLWriter writes XML data to a stream.
42 | *
43 | * @see net.n3.nanoxml.IXMLElement
44 | * @see java.io.Writer
45 | *
46 | * @author Marc De Scheemaecker
47 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
48 | */
49 | public class XMLWriter
50 | {
51 |
52 | /**
53 | * Where to write the output to.
54 | */
55 | private PrintWriter writer;
56 |
57 |
58 | /**
59 | * Creates a new XML writer.
60 | *
61 | * @param writer where to write the output to.
62 | */
63 | public XMLWriter(Writer writer)
64 | {
65 | if (writer instanceof PrintWriter) {
66 | this.writer = (PrintWriter) writer;
67 | } else {
68 | this.writer = new PrintWriter(writer);
69 | }
70 | }
71 |
72 |
73 | /**
74 | * Creates a new XML writer.
75 | *
76 | * @param stream where to write the output to.
77 | */
78 | public XMLWriter(OutputStream stream)
79 | {
80 | this.writer = new PrintWriter(stream);
81 | }
82 |
83 |
84 | /**
85 | * Cleans up the object when it's destroyed.
86 | */
87 | protected void finalize()
88 | throws Throwable
89 | {
90 | this.writer = null;
91 | super.finalize();
92 | }
93 |
94 |
95 | /**
96 | * Writes an XML element.
97 | *
98 | * @param xml the non-null XML element to write.
99 | */
100 | public void write(IXMLElement xml)
101 | throws IOException
102 | {
103 | this.write(xml, false, 0, true);
104 | }
105 |
106 |
107 | /**
108 | * Writes an XML element.
109 | *
110 | * @param xml the non-null XML element to write.
111 | * @param prettyPrint if spaces need to be inserted to make the output more
112 | * readable
113 | */
114 | public void write(IXMLElement xml,
115 | boolean prettyPrint)
116 | throws IOException
117 | {
118 | this.write(xml, prettyPrint, 0, true);
119 | }
120 |
121 |
122 | /**
123 | * Writes an XML element.
124 | *
125 | * @param xml the non-null XML element to write.
126 | * @param prettyPrint if spaces need to be inserted to make the output more
127 | * readable
128 | * @param indent how many spaces to indent the element.
129 | */
130 | public void write(IXMLElement xml,
131 | boolean prettyPrint,
132 | int indent)
133 | throws IOException
134 | {
135 | this.write(xml, prettyPrint, indent, true);
136 | }
137 |
138 |
139 | /**
140 | * Writes an XML element.
141 | *
142 | * @param xml the non-null XML element to write.
143 | * @param prettyPrint if spaces need to be inserted to make the output more
144 | * readable
145 | * @param indent how many spaces to indent the element.
146 | */
147 | public void write(IXMLElement xml,
148 | boolean prettyPrint,
149 | int indent,
150 | boolean collapseEmptyElements)
151 | throws IOException
152 | {
153 | if (prettyPrint) {
154 | for (int i = 0; i < indent; i++) {
155 | this.writer.print(' ');
156 | }
157 | }
158 |
159 | if (xml.getName() == null) {
160 | if (xml.getContent() != null) {
161 | if (prettyPrint) {
162 | this.writeEncoded(xml.getContent().trim());
163 | writer.println();
164 | } else {
165 | this.writeEncoded(xml.getContent());
166 | }
167 | }
168 | } else {
169 | this.writer.print('<');
170 | this.writer.print(xml.getFullName());
171 | Vector nsprefixes = new Vector();
172 |
173 | if (xml.getNamespace() != null) {
174 | if (xml.getName().equals(xml.getFullName())) {
175 | this.writer.print(" xmlns=\"" + xml.getNamespace() + '"');
176 | } else {
177 | String prefix = xml.getFullName();
178 | prefix = prefix.substring(0, prefix.indexOf(':'));
179 | nsprefixes.addElement(prefix);
180 | this.writer.print(" xmlns:" + prefix);
181 | this.writer.print("=\"" + xml.getNamespace() + "\"");
182 | }
183 | }
184 |
185 | Enumeration enume = xml.enumerateAttributeNames();
186 |
187 | while (enume.hasMoreElements()) {
188 | String key = (String) enume.nextElement();
189 | int index = key.indexOf(':');
190 |
191 | if (index >= 0) {
192 | String namespace = xml.getAttributeNamespace(key);
193 |
194 | if (namespace != null) {
195 | String prefix = key.substring(0, index);
196 |
197 | if (! nsprefixes.contains(prefix)) {
198 | this.writer.print(" xmlns:" + prefix);
199 | this.writer.print("=\"" + namespace + '"');
200 | nsprefixes.addElement(prefix);
201 | }
202 | }
203 | }
204 | }
205 |
206 | enume = xml.enumerateAttributeNames();
207 |
208 | while (enume.hasMoreElements()) {
209 | String key = (String) enume.nextElement();
210 | String value = xml.getAttribute(key, null);
211 | this.writer.print(" " + key + "=\"");
212 | this.writeEncoded(value);
213 | this.writer.print('"');
214 | }
215 |
216 | if ((xml.getContent() != null)
217 | && (xml.getContent().length() > 0)) {
218 | writer.print('>');
219 | this.writeEncoded(xml.getContent());
220 | writer.print("" + xml.getFullName() + '>');
221 |
222 | if (prettyPrint) {
223 | writer.println();
224 | }
225 | } else if (xml.hasChildren() || (! collapseEmptyElements)) {
226 | writer.print('>');
227 |
228 | if (prettyPrint) {
229 | writer.println();
230 | }
231 |
232 | enume = xml.enumerateChildren();
233 |
234 | while (enume.hasMoreElements()) {
235 | IXMLElement child = (IXMLElement) enume.nextElement();
236 | this.write(child, prettyPrint, indent + 4,
237 | collapseEmptyElements);
238 | }
239 |
240 | if (prettyPrint) {
241 | for (int i = 0; i < indent; i++) {
242 | this.writer.print(' ');
243 | }
244 | }
245 |
246 | this.writer.print("" + xml.getFullName() + ">");
247 |
248 | if (prettyPrint) {
249 | writer.println();
250 | }
251 | } else {
252 | this.writer.print("/>");
253 |
254 | if (prettyPrint) {
255 | writer.println();
256 | }
257 | }
258 | }
259 |
260 | this.writer.flush();
261 | }
262 |
263 |
264 | /**
265 | * Writes a string encoding reserved characters.
266 | *
267 | * @param str the string to write.
268 | */
269 | private void writeEncoded(String str)
270 | {
271 | for (int i = 0; i < str.length(); i++) {
272 | char c = str.charAt(i);
273 |
274 | switch (c) {
275 | case 0x0A:
276 | this.writer.print(c);
277 | break;
278 |
279 | case '<':
280 | this.writer.print("<");
281 | break;
282 |
283 | case '>':
284 | this.writer.print(">");
285 | break;
286 |
287 | case '&':
288 | this.writer.print("&");
289 | break;
290 |
291 | case '\'':
292 | this.writer.print("'");
293 | break;
294 |
295 | case '"':
296 | this.writer.print(""");
297 | break;
298 |
299 | default:
300 | if ((c < ' ') || (c > 0x7E)) {
301 | this.writer.print("");
302 | this.writer.print(Integer.toString(c, 16));
303 | this.writer.print(';');
304 | } else {
305 | this.writer.print(c);
306 | }
307 | }
308 | }
309 | }
310 |
311 | }
312 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/sax/SAXEntityResolver.java:
--------------------------------------------------------------------------------
1 | /* SAXEntityResolver.java NanoXML/SAX
2 | *
3 | * $Revision: 1.4 $
4 | * $Date: 2002/01/04 21:03:28 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of the SAX adapter for NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml.sax;
30 |
31 |
32 | import java.io.InputStream;
33 | import java.io.InputStreamReader;
34 | import java.io.Reader;
35 | import java.net.URL;
36 | import net.n3.nanoxml.XMLEntityResolver;
37 | import net.n3.nanoxml.IXMLReader;
38 | import org.xml.sax.EntityResolver;
39 | import org.xml.sax.InputSource;
40 |
41 |
42 | /**
43 | * SAXEntityResolver is a subclass of XMLEntityResolver that supports the
44 | * SAX EntityResolver listener.
45 | *
46 | * @see net.n3.nanoxml.IXMLEntityResolver
47 | *
48 | * @author Marc De Scheemaecker
49 | * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
50 | */
51 | public class SAXEntityResolver
52 | extends XMLEntityResolver
53 | {
54 |
55 | /**
56 | * The SAX EntityResolver listener.
57 | */
58 | private EntityResolver saxEntityResolver;
59 |
60 |
61 | /**
62 | * Creates the resolver.
63 | */
64 | public SAXEntityResolver()
65 | {
66 | this.saxEntityResolver = null;
67 | }
68 |
69 |
70 | /**
71 | * Cleans up the object when it's destroyed.
72 | */
73 | protected void finalize()
74 | throws Throwable
75 | {
76 | this.saxEntityResolver = null;
77 | super.finalize();
78 | }
79 |
80 |
81 | /**
82 | * Sets the SAX EntityResolver listener.
83 | *
84 | * @param resolver the entity resolver
85 | */
86 | public void setEntityResolver(EntityResolver resolver)
87 | {
88 | this.saxEntityResolver = resolver;
89 | }
90 |
91 |
92 | /**
93 | * Opens an external entity.
94 | *
95 | * @param xmlReader the current XML reader
96 | * @param publicID the public ID, which may be null
97 | * @param systemID the system ID
98 | *
99 | * @return the reader, or null if the reader could not be created/opened
100 | */
101 | protected Reader openExternalEntity(IXMLReader xmlReader,
102 | String publicID,
103 | String systemID)
104 | {
105 | try {
106 | URL url = new URL(xmlReader.getSystemID());
107 | url = new URL(url, systemID);
108 |
109 | if (this.saxEntityResolver != null) {
110 | InputSource source
111 | = this.saxEntityResolver
112 | .resolveEntity(publicID, url.toString());
113 |
114 | if (source != null) {
115 | Reader reader = source.getCharacterStream();
116 |
117 | if (reader != null) {
118 | return reader;
119 | }
120 |
121 | InputStream stream = source.getByteStream();
122 |
123 | if (stream == null) {
124 | publicID = source.getPublicId();
125 | systemID = source.getSystemId();
126 | } else {
127 | String encoding = source.getEncoding();
128 |
129 | if (encoding != null) {
130 | return new InputStreamReader(stream, encoding);
131 | } else { // if encoding == null
132 | return new InputStreamReader(stream);
133 | }
134 | }
135 | }
136 | }
137 |
138 | return super.openExternalEntity(xmlReader, publicID, systemID);
139 | } catch (Exception e) {
140 | return null;
141 | }
142 | }
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/src/net/n3/nanoxml/sax/SAXParser.java:
--------------------------------------------------------------------------------
1 | /* SAXParser.java NanoXML/SAX
2 | *
3 | * $Revision: 1.5 $
4 | * $Date: 2002/03/24 11:39:20 $
5 | * $Name: RELEASE_2_2_1 $
6 | *
7 | * This file is part of the SAX adapter for NanoXML 2 for Java.
8 | * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
9 | *
10 | * This software is provided 'as-is', without any express or implied warranty.
11 | * In no event will the authors be held liable for any damages arising from the
12 | * use of this software.
13 | *
14 | * Permission is granted to anyone to use this software for any purpose,
15 | * including commercial applications, and to alter it and redistribute it
16 | * freely, subject to the following restrictions:
17 | *
18 | * 1. The origin of this software must not be misrepresented; you must not
19 | * claim that you wrote the original software. If you use this software in
20 | * a product, an acknowledgment in the product documentation would be
21 | * appreciated but is not required.
22 | *
23 | * 2. Altered source versions must be plainly marked as such, and must not be
24 | * misrepresented as being the original software.
25 | *
26 | * 3. This notice may not be removed or altered from any source distribution.
27 | */
28 |
29 | package net.n3.nanoxml.sax;
30 |
31 |
32 | import java.io.FileNotFoundException;
33 | import java.io.InputStream;
34 | import java.io.InputStreamReader;
35 | import java.io.IOException;
36 | import java.io.Reader;
37 | import java.io.UnsupportedEncodingException;
38 | import java.net.MalformedURLException;
39 | import java.util.Locale;
40 | import net.n3.nanoxml.IXMLBuilder;
41 | import net.n3.nanoxml.IXMLParser;
42 | import net.n3.nanoxml.IXMLReader;
43 | import net.n3.nanoxml.StdXMLReader;
44 | import net.n3.nanoxml.XMLParserFactory;
45 | import org.xml.sax.Parser;
46 | import org.xml.sax.DocumentHandler;
47 | import org.xml.sax.DTDHandler;
48 | import org.xml.sax.EntityResolver;
49 | import org.xml.sax.ErrorHandler;
50 | import org.xml.sax.HandlerBase;
51 | import org.xml.sax.InputSource;
52 | import org.xml.sax.Locator;
53 | import org.xml.sax.SAXException;
54 | import org.xml.sax.helpers.AttributeListImpl;
55 | import org.xml.sax.helpers.LocatorImpl;
56 |
57 |
58 | /**
59 | * @author kriesen
60 | */
61 | public class SAXParser
62 | implements Parser
63 | {
64 |
65 | /**
66 | * The SAX adapter.
67 | */
68 | private SAXAdapter adapter;
69 |
70 |
71 | /**
72 | * The client error handler.
73 | */
74 | private ErrorHandler errorHandler;
75 |
76 |
77 | /**
78 | * The entity resolver.
79 | */
80 | private SAXEntityResolver entityResolver;
81 |
82 |
83 | /**
84 | * Creates the SAX parser.
85 | */
86 | public SAXParser()
87 | {
88 | this.adapter = new SAXAdapter();
89 | this.errorHandler = new HandlerBase();
90 | this.entityResolver = new SAXEntityResolver();
91 | }
92 |
93 |
94 | /**
95 | * Cleans up the object when it's destroyed.
96 | */
97 | protected void finalize()
98 | throws Throwable
99 | {
100 | this.adapter = null;
101 | this.errorHandler = null;
102 | this.entityResolver = null;
103 | super.finalize();
104 | }
105 |
106 |
107 | /**
108 | * Sets the locale. Only locales using the language english are accepted.
109 | *
110 | * @param locale the locale
111 | *
112 | * @exception org.xml.sax.SAXException
113 | * if locale
is null
or the associated
114 | * language is not english.
115 | */
116 | public void setLocale(Locale locale)
117 | throws SAXException
118 | {
119 | if ((locale == null) || (! locale.getLanguage().equals("en"))) {
120 | throw new SAXException("NanoXML/SAX doesn't support locale: "
121 | + locale);
122 | }
123 | }
124 |
125 |
126 | /**
127 | * Sets the entity resolver.
128 | *
129 | * @param resolver the entity resolver
130 | */
131 | public void setEntityResolver(EntityResolver resolver)
132 | {
133 | this.entityResolver.setEntityResolver(resolver);
134 | }
135 |
136 |
137 | /**
138 | * Sets the DTD handler. As the parser is non-validating, this handler is
139 | * never called.
140 | *
141 | * @param handler the DTD handler
142 | */
143 | public void setDTDHandler(DTDHandler handler)
144 | {
145 | // nothing to do
146 | }
147 |
148 |
149 | /**
150 | * Allows an application to register a document event handler.
151 | *
152 | * @param handler the document handler
153 | */
154 | public void setDocumentHandler(DocumentHandler handler)
155 | {
156 | this.adapter.setDocumentHandler(handler);
157 | }
158 |
159 |
160 | /**
161 | * Allow an application to register an error event handler.
162 | * @param handler the error handler
163 | * @uml.property name="errorHandler"
164 | */
165 | public void setErrorHandler(ErrorHandler handler)
166 | {
167 | this.errorHandler = handler;
168 | }
169 |
170 |
171 | /**
172 | * Creates the XML parser.
173 | */
174 | private IXMLParser createParser()
175 | throws SAXException
176 | {
177 | try {
178 | return XMLParserFactory.createDefaultXMLParser();
179 | } catch (Exception exception) {
180 | throw new SAXException(exception);
181 | }
182 | }
183 |
184 |
185 | /**
186 | * Parse an XML document.
187 | *
188 | * @param source the input source
189 | */
190 | public void parse(InputSource source)
191 | throws SAXException,
192 | IOException
193 | {
194 | IXMLParser parser = this.createParser();
195 | parser.setBuilder(this.adapter);
196 | parser.setResolver(this.entityResolver);
197 | Reader reader = source.getCharacterStream();
198 |
199 | if (reader != null) {
200 | parser.setReader(new StdXMLReader(reader));
201 | } else {
202 | InputStream stream = source.getByteStream();
203 |
204 | if (stream != null) {
205 | String encoding = source.getEncoding();
206 |
207 | if (encoding != null) {
208 | try {
209 | reader = new InputStreamReader(stream, encoding);
210 | parser.setReader(new StdXMLReader(reader));
211 | } catch (UnsupportedEncodingException exception) {
212 | throw new SAXException(exception);
213 | }
214 | } else { // if encoding == null
215 | parser.setReader(new StdXMLReader(stream));
216 | }
217 | } else { // if stream == null
218 | parser.setReader(new StdXMLReader(source.getPublicId(),
219 | source.getSystemId()));
220 | }
221 | }
222 |
223 | try {
224 | parser.parse();
225 | this.adapter.endDocument();
226 | } catch (IOException exception) {
227 | throw exception;
228 | } catch (Exception exception) {
229 | throw new SAXException(exception);
230 | } finally {
231 | reader.close();
232 | }
233 | }
234 |
235 |
236 | /**
237 | * Parse an XML document from a system identifier (URI).
238 | *
239 | * @param systemId the system ID
240 | */
241 | public void parse(String systemId)
242 | throws SAXException,
243 | IOException
244 | {
245 | IXMLParser parser = this.createParser();
246 | parser.setBuilder(this.adapter);
247 | parser.setReader(new StdXMLReader(null, systemId));
248 |
249 | try {
250 | parser.parse();
251 | this.adapter.endDocument();
252 | } catch (IOException exception) {
253 | throw exception;
254 | } catch (Exception exception) {
255 | throw new SAXException(exception);
256 | }
257 | }
258 |
259 | }
260 |
--------------------------------------------------------------------------------
/src/util/AssignmentAndCost.java:
--------------------------------------------------------------------------------
1 | package util;
2 |
3 | public class AssignmentAndCost implements Comparable