Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue #64 - contrib to core: Location-information for SAX Parser.

Add support for location information to all the JDOMFactories. This
creates a new compatibility issue with JDOM 1.x (you need to add 'int line, int col, ' to all the JDMFcatory methods that create JDOM Content instances)
  • Loading branch information...
commit 6bead51829d5c54a4f97d3ed55459c736fc9fd36 1 parent b7db151
@rolfl rolfl authored
Showing with 1,989 additions and 255 deletions.
  1. +4 −4 contrib/src/java/org/jdom2/contrib/ids/IdFactory.java
  2. +4 −4 contrib/src/java/org/jdom2/contrib/input/LineNumberSAXHandler.java
  3. +128 −37 core/src/java/org/jdom2/DefaultJDOMFactory.java
  4. +207 −0 core/src/java/org/jdom2/JDOMFactory.java
  5. +42 −42 core/src/java/org/jdom2/SlimJDOMFactory.java
  6. +17 −17 core/src/java/org/jdom2/UncheckedJDOMFactory.java
  7. +171 −143 core/src/java/org/jdom2/input/sax/SAXHandler.java
  8. +113 −0 core/src/java/org/jdom2/located/LCDATA.java
  9. +104 −0 core/src/java/org/jdom2/located/LComment.java
  10. +147 −0 core/src/java/org/jdom2/located/LDocType.java
  11. +152 −0 core/src/java/org/jdom2/located/LElement.java
  12. +139 −0 core/src/java/org/jdom2/located/LEntityRef.java
  13. +140 −0 core/src/java/org/jdom2/located/LProcessingInstruction.java
  14. +110 −0 core/src/java/org/jdom2/located/LText.java
  15. +86 −0 core/src/java/org/jdom2/located/Located.java
  16. +220 −0 core/src/java/org/jdom2/located/LocatedJDOMFactory.java
  17. +12 −0 core/src/java/org/jdom2/located/package.html
  18. +83 −0 test/src/java/org/jdom2/test/cases/AbstractTestJDOMFactory.java
  19. +7 −0 test/src/java/org/jdom2/test/cases/TestDefaultJDOMFactory.java
  20. +7 −0 test/src/java/org/jdom2/test/cases/TestSlimJDOMFactory.java
  21. +7 −0 test/src/java/org/jdom2/test/cases/TestSlimJDOMFactoryNoText.java
  22. +7 −0 test/src/java/org/jdom2/test/cases/TestUncheckedJDOMFactory.java
  23. +74 −0 test/src/java/org/jdom2/test/cases/located/TestLocatedJDOMFactory.java
  24. +8 −8 test/src/java/org/jdom2/test/cases/transform/TestXSLTransformer.java
View
8 contrib/src/java/org/jdom2/contrib/ids/IdFactory.java
@@ -133,19 +133,19 @@ public Document document(Element rootElement) {
}
@Override
- public Element element(String name, Namespace namespace) {
+ public Element element(final int line, final int col, String name, Namespace namespace) {
return new IdElement(name, namespace);
}
@Override
- public Element element(String name) {
+ public Element element(final int line, final int col, String name) {
return new IdElement(name);
}
@Override
- public Element element(String name, String uri) {
+ public Element element(final int line, final int col, String name, String uri) {
return new IdElement(name, uri);
}
@Override
- public Element element(String name, String prefix, String uri) {
+ public Element element(final int line, final int col, String name, String prefix, String uri) {
return new IdElement(name, prefix, uri);
}
}
View
8 contrib/src/java/org/jdom2/contrib/input/LineNumberSAXHandler.java
@@ -98,25 +98,25 @@ public SAXHandler createSAXHandler(JDOMFactory factory) {
private static class MyJDOMFactory extends DefaultJDOMFactory {
@Override
- public Element element(String name)
+ public Element element(final int line, final int col, String name)
{
return new LineNumberElement(name);
}
@Override
- public Element element(String name, String prefix, String uri)
+ public Element element(final int line, final int col, String name, String prefix, String uri)
{
return new LineNumberElement(name, prefix, uri);
}
@Override
- public Element element(String name, Namespace namespace)
+ public Element element(final int line, final int col, String name, Namespace namespace)
{
return new LineNumberElement(name, namespace);
}
@Override
- public Element element(String name, String uri)
+ public Element element(final int line, final int col, String name, String uri)
{
return new LineNumberElement(name, uri);
}
View
165 core/src/java/org/jdom2/DefaultJDOMFactory.java
@@ -59,10 +59,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
/**
* Creates the standard top-level JDOM classes (Element, Document, Comment,
* etc). A subclass of this factory might construct custom classes.
- *
- * @author Ken Rune Holland
- * @author Phil Nelson
- * @author Bradley S. Huffman
+ *
+ * @author Ken Rune Holland
+ * @author Phil Nelson
+ * @author Bradley S. Huffman
*/
public class DefaultJDOMFactory implements JDOMFactory {
@@ -82,14 +82,15 @@ public Attribute attribute(String name, String value, Namespace namespace) {
@Override
@Deprecated
- public Attribute attribute(String name, String value,
- int type, Namespace namespace) {
- return new Attribute(name, value, AttributeType.byIndex(type), namespace);
+ public Attribute attribute(String name, String value, int type,
+ Namespace namespace) {
+ return new Attribute(name, value, AttributeType.byIndex(type),
+ namespace);
}
@Override
- public Attribute attribute(String name, String value,
- AttributeType type, Namespace namespace) {
+ public Attribute attribute(String name, String value, AttributeType type,
+ Namespace namespace) {
return new Attribute(name, value, type, namespace);
}
@@ -108,35 +109,67 @@ public Attribute attribute(String name, String value, int type) {
public Attribute attribute(String name, String value, AttributeType type) {
return new Attribute(name, value, type);
}
+
+ @Override
+ public final CDATA cdata(String str) {
+ return cdata(-1, -1, str);
+ }
@Override
- public CDATA cdata(String text) {
+ public CDATA cdata(final int line, final int col, final String text) {
return new CDATA(text);
}
+
+ @Override
+ public final Text text(String str) {
+ return text(-1, -1, str);
+ }
@Override
- public Text text(String text) {
+ public Text text(final int line, final int col, final String text) {
return new Text(text);
}
+
+ @Override
+ public final Comment comment(String text) {
+ return comment(-1, -1, text);
+ }
@Override
- public Comment comment(String text) {
+ public Comment comment(final int line, final int col, final String text) {
return new Comment(text);
}
@Override
- public DocType docType(String elementName,
- String publicID, String systemID) {
+ public final DocType docType(String elementName, String publicID, String systemID) {
+ return docType(-1, -1, elementName, publicID, systemID);
+ }
+
+ @Override
+ public DocType docType(final int line, final int col,
+ final String elementName, String publicID, String systemID) {
return new DocType(elementName, publicID, systemID);
}
@Override
- public DocType docType(String elementName, String systemID) {
+ public final DocType docType(String elementName, String systemID) {
+ return docType(-1, -1, elementName, systemID);
+ }
+
+ @Override
+ public DocType docType(final int line, final int col,
+ final String elementName, String systemID) {
return new DocType(elementName, systemID);
}
+
+ @Override
+ public final DocType docType(String elementName) {
+ return docType(-1, -1, elementName);
+ }
@Override
- public DocType docType(String elementName) {
+ public DocType docType(final int line, final int col,
+ final String elementName) {
return new DocType(elementName);
}
@@ -146,7 +179,8 @@ public Document document(Element rootElement, DocType docType) {
}
@Override
- public Document document(Element rootElement, DocType docType, String baseURI) {
+ public Document document(Element rootElement, DocType docType,
+ String baseURI) {
return new Document(rootElement, docType, baseURI);
}
@@ -156,67 +190,124 @@ public Document document(Element rootElement) {
}
@Override
- public Element element(String name, Namespace namespace) {
+ public final Element element(String name, Namespace namespace) {
+ return element(-1, -1, name, namespace);
+ }
+
+ @Override
+ public Element element(final int line, final int col, final String name,
+ Namespace namespace) {
return new Element(name, namespace);
}
+
+ @Override
+ public final Element element(String name) {
+ return element(-1, -1, name);
+ }
@Override
- public Element element(String name) {
+ public Element element(final int line, final int col, final String name) {
return new Element(name);
}
@Override
- public Element element(String name, String uri) {
- return new Element(name, uri);
+ public final Element element(String name, String uri) {
+ return element(-1, -1, name, uri);
}
@Override
- public Element element(String name, String prefix, String uri) {
+ public Element element(final int line, final int col, final String name,
+ String uri) {
+ return new Element(name, uri);
+ }
+
+ @Override
+ public final Element element(String name, String prefix, String uri) {
+ return element(-1, -1, name, prefix, uri);
+ }
+
+ @Override
+ public Element element(final int line, final int col, final String name,
+ String prefix, String uri) {
return new Element(name, prefix, uri);
}
@Override
- public ProcessingInstruction processingInstruction(String target) {
+ public final ProcessingInstruction processingInstruction(String target) {
+ return processingInstruction(-1, -1, target);
+ }
+
+ @Override
+ public ProcessingInstruction processingInstruction(final int line,
+ final int col, final String target) {
return new ProcessingInstruction(target);
}
@Override
- public ProcessingInstruction processingInstruction(String target,
- Map<String,String> data) {
+ public final ProcessingInstruction processingInstruction(String target,
+ Map<String, String> data) {
+ return processingInstruction(-1, -1, target, data);
+ }
+
+ @Override
+ public ProcessingInstruction processingInstruction(final int line,
+ final int col, final String target, Map<String, String> data) {
return new ProcessingInstruction(target, data);
}
-
+
@Override
- public ProcessingInstruction processingInstruction(String target,
+ public final ProcessingInstruction processingInstruction(String target,
String data) {
+ return processingInstruction(-1, -1, target, data);
+ }
+
+ @Override
+ public ProcessingInstruction processingInstruction(final int line,
+ final int col, final String target, String data) {
return new ProcessingInstruction(target, data);
}
+
+ @Override
+ public final EntityRef entityRef(String name) {
+ return entityRef(-1, -1, name);
+ }
@Override
- public EntityRef entityRef(String name) {
+ public EntityRef entityRef(final int line, final int col, final String name) {
return new EntityRef(name);
}
@Override
- public EntityRef entityRef(String name, String publicID, String systemID) {
+ public final EntityRef entityRef(String name, String publicID, String systemID) {
+ return entityRef(-1, -1, name, publicID, systemID);
+ }
+
+ @Override
+ public EntityRef entityRef(final int line, final int col,
+ final String name, String publicID, String systemID) {
return new EntityRef(name, publicID, systemID);
}
+
+ @Override
+ public final EntityRef entityRef(String name, String systemID) {
+ return entityRef(-1, -1, name, systemID);
+ }
@Override
- public EntityRef entityRef(String name, String systemID) {
+ public EntityRef entityRef(final int line, final int col,
+ final String name, String systemID) {
return new EntityRef(name, systemID);
}
// =====================================================================
- // List manipulation
- // =====================================================================
+ // List manipulation
+ // =====================================================================
- @Override
- public void addContent(Parent parent, Content child) {
+ @Override
+ public void addContent(Parent parent, Content child) {
if (parent instanceof Document) {
((Document) parent).addContent(child);
- }
- else {
+ } else {
((Element) parent).addContent(child);
}
}
@@ -230,7 +321,7 @@ public void setAttribute(Element parent, Attribute a) {
public void addNamespaceDeclaration(Element parent, Namespace additional) {
parent.addNamespaceDeclaration(additional);
}
-
+
@Override
public void setRoot(Document doc, Element root) {
doc.setRootElement(root);
View
207 core/src/java/org/jdom2/JDOMFactory.java
@@ -176,6 +176,28 @@ public Attribute attribute(String name, String value,
*/
public CDATA cdata(String str);
+ /**
+ * This creates the CDATA with the supplied text.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param str <code>String</code> content of CDATA.
+ * @return the created CDATA instance
+ */
+ public CDATA cdata(int line, int col, String str);
+
+ // **** constructing Text ****
+
+ /**
+ * This creates the Text with the supplied text.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param str <code>String</code> content of Text.
+ * @return the created Text instance
+ */
+ public Text text(int line, int col, String str);
+
// **** constructing Text ****
/**
@@ -196,6 +218,16 @@ public Attribute attribute(String name, String value,
*/
public Comment comment(String text);
+ /**
+ * This creates the comment with the supplied text.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param text <code>String</code> content of comment.
+ * @return the created Comment instance
+ */
+ public Comment comment(int line, int col, String text);
+
// **** constructing DocType
/**
@@ -237,6 +269,51 @@ public DocType docType(String elementName,
*/
public DocType docType(String elementName);
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name and a reference to an
+ * external DTD.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @param publicID <code>String</code> public ID of
+ * referenced DTD
+ * @param systemID <code>String</code> system ID of
+ * referenced DTD
+ * @return the created DocType instance
+ */
+ public DocType docType(int line, int col, String elementName,
+ String publicID, String systemID);
+
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name and reference to an
+ * external DTD.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @param systemID <code>String</code> system ID of
+ * referenced DTD
+ * @return the created DocType instance
+ */
+ public DocType docType(int line, int col, String elementName, String systemID);
+
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @return the created DocType instance
+ */
+ public DocType docType(int line, int col, String elementName);
+
// **** constructing Document
/**
@@ -325,6 +402,62 @@ public DocType docType(String elementName,
*/
public Element element(String name, String prefix, String uri);
+ /**
+ * This will create a new <code>Element</code>
+ * with the supplied (local) name, and define
+ * the <code>{@link org.jdom2.Namespace}</code> to be used.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @param namespace <code>Namespace</code> to put element in.
+ * @return the created Element instance
+ */
+ public Element element(int line, int col, String name, Namespace namespace);
+
+ /**
+ * This will create an <code>Element</code> in no
+ * <code>{@link org.jdom2.Namespace}</code>.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @return the created Element instance
+ */
+ public Element element(int line, int col, String name);
+
+ /**
+ * This will create a new <code>Element</code> with
+ * the supplied (local) name, and specifies the URI
+ * of the <code>{@link org.jdom2.Namespace}</code> the <code>Element</code>
+ * should be in, resulting it being unprefixed (in the default
+ * namespace).
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @param uri <code>String</code> URI for <code>Namespace</code> element
+ * should be in.
+ * @return the created Element instance
+ */
+ public Element element(int line, int col, String name, String uri);
+
+ /**
+ * This will create a new <code>Element</code> with
+ * the supplied (local) name, and specifies the prefix and URI
+ * of the <code>{@link org.jdom2.Namespace}</code> the <code>Element</code>
+ * should be in.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @param prefix the NamespacePrefic to use for this Element
+ * @param uri <code>String</code> URI for <code>Namespace</code> element
+ * should be in.
+ * @return the created Element instance
+ */
+ public Element element(int line, int col, String name, String prefix, String uri);
+
// **** constructing ProcessingInstruction ****
/**
@@ -359,6 +492,44 @@ public ProcessingInstruction processingInstruction(String target,
*/
public ProcessingInstruction processingInstruction(String target);
+ /**
+ * This will create a new <code>ProcessingInstruction</code>
+ * with the specified target and data.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param target <code>String</code> target of PI.
+ * @param data <code>Map</code> data for PI, in
+ * name/value pairs
+ * @return the created ProcessingInstruction instance
+ */
+ public ProcessingInstruction processingInstruction(int line, int col, String target,
+ Map<String,String> data);
+
+ /**
+ * This will create a new <code>ProcessingInstruction</code>
+ * with the specified target and data.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param target <code>String</code> target of PI.
+ * @param data <code>String</code> data for PI.
+ * @return the created ProcessingInstruction instance
+ */
+ public ProcessingInstruction processingInstruction(int line, int col, String target,
+ String data);
+
+ /**
+ * This will create a new <code>ProcessingInstruction</code>
+ * with the specified target and no data.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param target <code>String</code> target of PI.
+ * @return the created ProcessingInstruction instance
+ */
+ public ProcessingInstruction processingInstruction(int line, int col, String target);
+
// **** constructing EntityRef ****
/**
@@ -391,6 +562,42 @@ public ProcessingInstruction processingInstruction(String target,
*/
public EntityRef entityRef(String name, String systemID);
+ /**
+ * This will create a new <code>EntityRef</code>
+ * with the supplied name.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @return the created EntityRef instance
+ */
+ public EntityRef entityRef(int line, int col, String name);
+
+ /**
+ * This will create a new <code>EntityRef</code>
+ * with the supplied name, public ID, and system ID.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @param publicID <code>String</code> public ID of element.
+ * @param systemID <code>String</code> system ID of element.
+ * @return the created EntityRef instance
+ */
+ public EntityRef entityRef(int line, int col, String name, String publicID, String systemID);
+
+ /**
+ * This will create a new <code>EntityRef</code>
+ * with the supplied name and system ID.
+ *
+ * @param line The line on which this content begins.
+ * @param col The column on the line at which this content begins.
+ * @param name <code>String</code> name of element.
+ * @param systemID <code>String</code> system ID of element.
+ * @return the created EntityRef instance
+ */
+ public EntityRef entityRef(int line, int col, String name, String systemID);
+
// =====================================================================
// List manipulation
// =====================================================================
View
84 core/src/java/org/jdom2/SlimJDOMFactory.java
@@ -86,7 +86,7 @@ public SlimJDOMFactory() {
* @param cachetext should be true if you want the content of CDATA, Text,
* Comment and Attribute values cached as well.
*/
- public SlimJDOMFactory(boolean cachetext) {
+ public SlimJDOMFactory(final boolean cachetext) {
super();
this.cachetext = cachetext;
}
@@ -100,7 +100,7 @@ public void clearCache() {
}
@Override
- public Attribute attribute(String name, String value, Namespace namespace) {
+ public Attribute attribute(final String name, final String value, final Namespace namespace) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value),
namespace);
@@ -108,15 +108,15 @@ public Attribute attribute(String name, String value, Namespace namespace) {
@Override
@Deprecated
- public Attribute attribute(String name, String value, int type,
- Namespace namespace) {
+ public Attribute attribute(final String name, final String value, final int type,
+ final Namespace namespace) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value),
type, namespace);
}
@Override
- public Attribute attribute(String name, String value, AttributeType type,
+ public Attribute attribute(final String name, final String value, final AttributeType type,
Namespace namespace) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value),
@@ -124,106 +124,106 @@ public Attribute attribute(String name, String value, AttributeType type,
}
@Override
- public Attribute attribute(String name, String value) {
+ public Attribute attribute(final String name, final String value) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value));
}
@Override
@Deprecated
- public Attribute attribute(String name, String value, int type) {
+ public Attribute attribute(final String name, final String value, final int type) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value),
type);
}
@Override
- public Attribute attribute(String name, String value, AttributeType type) {
+ public Attribute attribute(final String name, final String value, final AttributeType type) {
return super.attribute(cache.reuse(name),
(cachetext ? cache.reuse(value) : value),
type);
}
@Override
- public CDATA cdata(String str) {
- return super.cdata((cachetext ? cache.reuse(str) : str));
+ public CDATA cdata(final int line, final int col, final String str) {
+ return super.cdata(line, col, (cachetext ? cache.reuse(str) : str));
}
@Override
- public Text text(String str) {
- return super.text((cachetext ? cache.reuse(str) : str));
+ public Text text(final int line, final int col, final String str) {
+ return super.text(line, col, (cachetext ? cache.reuse(str) : str));
}
@Override
- public Comment comment(String text) {
- return super.comment((cachetext ? cache.reuse(text) : text));
+ public Comment comment(final int line, final int col, final String text) {
+ return super.comment(line, col, (cachetext ? cache.reuse(text) : text));
}
@Override
- public DocType docType(String elementName, String publicID, String systemID) {
- return super.docType(cache.reuse(elementName), publicID, systemID);
+ public DocType docType(final int line, final int col, final String elementName, final String publicID, final String systemID) {
+ return super.docType(line, col, cache.reuse(elementName), publicID, systemID);
}
@Override
- public DocType docType(String elementName, String systemID) {
- return super.docType(cache.reuse(elementName), systemID);
+ public DocType docType(final int line, final int col, final String elementName, final String systemID) {
+ return super.docType(line, col, cache.reuse(elementName), systemID);
}
@Override
- public DocType docType(String elementName) {
- return super.docType(cache.reuse(elementName));
+ public DocType docType(final int line, final int col, final String elementName) {
+ return super.docType(line, col, cache.reuse(elementName));
}
@Override
- public Element element(String name, Namespace namespace) {
- return super.element(cache.reuse(name), namespace);
+ public Element element(final int line, final int col, final String name, final Namespace namespace) {
+ return super.element(line, col, cache.reuse(name), namespace);
}
@Override
- public Element element(String name) {
- return super.element(cache.reuse(name));
+ public Element element(final int line, final int col, final String name) {
+ return super.element(line, col, cache.reuse(name));
}
@Override
- public Element element(String name, String uri) {
- return super.element(cache.reuse(name), uri);
+ public Element element(final int line, final int col, final String name, final String uri) {
+ return super.element(line, col, cache.reuse(name), uri);
}
@Override
- public Element element(String name, String prefix, String uri) {
- return super.element(cache.reuse(name), prefix, uri);
+ public Element element(final int line, final int col, final String name, final String prefix, final String uri) {
+ return super.element(line, col, cache.reuse(name), prefix, uri);
}
@Override
- public ProcessingInstruction processingInstruction(String target,
- Map<String, String> data) {
- return super.processingInstruction(cache.reuse(target), data);
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target,
+ final Map<String, String> data) {
+ return super.processingInstruction(line, col, cache.reuse(target), data);
}
@Override
- public ProcessingInstruction processingInstruction(String target,
- String data) {
- return super.processingInstruction(cache.reuse(target), data);
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target,
+ final String data) {
+ return super.processingInstruction(line, col, cache.reuse(target), data);
}
@Override
- public ProcessingInstruction processingInstruction(String target) {
- return super.processingInstruction(cache.reuse(target));
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target) {
+ return super.processingInstruction(line, col, cache.reuse(target));
}
@Override
- public EntityRef entityRef(String name) {
- return super.entityRef(cache.reuse(name));
+ public EntityRef entityRef(final int line, final int col, final String name) {
+ return super.entityRef(line, col, cache.reuse(name));
}
@Override
- public EntityRef entityRef(String name, String publicID, String systemID) {
- return super.entityRef(cache.reuse(name), publicID, systemID);
+ public EntityRef entityRef(final int line, final int col, final String name, final String publicID, final String systemID) {
+ return super.entityRef(line, col, cache.reuse(name), publicID, systemID);
}
@Override
- public EntityRef entityRef(String name, String systemID) {
- return super.entityRef(cache.reuse(name), systemID);
+ public EntityRef entityRef(final int line, final int col, final String name, final String systemID) {
+ return super.entityRef(line, col, cache.reuse(name), systemID);
}
}
View
34 core/src/java/org/jdom2/UncheckedJDOMFactory.java
@@ -63,14 +63,14 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* problems in the input will be uncaught until later when they could cause
* infinite loops, malformed XML, or worse. Use with extreme caution.
*/
-public class UncheckedJDOMFactory implements JDOMFactory {
+public class UncheckedJDOMFactory extends DefaultJDOMFactory {
// =====================================================================
// Element Factory
// =====================================================================
@Override
- public Element element(String name, Namespace namespace) {
+ public Element element(final int line, final int col, final String name, Namespace namespace) {
Element e = new Element();
e.name = name;
if (namespace == null) {
@@ -81,7 +81,7 @@ public Element element(String name, Namespace namespace) {
}
@Override
- public Element element(String name) {
+ public Element element(final int line, final int col, final String name) {
Element e = new Element();
e.name = name;
e.namespace = Namespace.NO_NAMESPACE;
@@ -89,12 +89,12 @@ public Element element(String name) {
}
@Override
- public Element element(String name, String uri) {
+ public Element element(final int line, final int col, final String name, String uri) {
return element(name, Namespace.getNamespace("", uri));
}
@Override
- public Element element(String name, String prefix, String uri) {
+ public Element element(final int line, final int col, final String name, String prefix, String uri) {
return element(name, Namespace.getNamespace(prefix, uri));
}
@@ -164,7 +164,7 @@ public Attribute attribute(String name, String value, AttributeType type) {
// =====================================================================
@Override
- public Text text(String str) {
+ public Text text(final int line, final int col, final String str) {
Text t = new Text();
t.value = str;
return t;
@@ -175,7 +175,7 @@ public Text text(String str) {
// =====================================================================
@Override
- public CDATA cdata(String str) {
+ public CDATA cdata(final int line, final int col, final String str) {
CDATA c = new CDATA();
c.value = str;
return c;
@@ -186,7 +186,7 @@ public CDATA cdata(String str) {
// =====================================================================
@Override
- public Comment comment(String str) {
+ public Comment comment(final int line, final int col, final String str) {
Comment c = new Comment();
c.text = str;
return c;
@@ -197,7 +197,7 @@ public Comment comment(String str) {
// =====================================================================
@Override
- public ProcessingInstruction processingInstruction(String target, Map<String,String> data) {
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target, Map<String,String> data) {
ProcessingInstruction p = new ProcessingInstruction();
p.target = target;
p.setData(data);
@@ -205,7 +205,7 @@ public ProcessingInstruction processingInstruction(String target, Map<String,Str
}
@Override
- public ProcessingInstruction processingInstruction(String target, String data) {
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target, String data) {
ProcessingInstruction p = new ProcessingInstruction();
p.target = target;
p.setData(data);
@@ -213,7 +213,7 @@ public ProcessingInstruction processingInstruction(String target, String data) {
}
@Override
- public ProcessingInstruction processingInstruction(String target) {
+ public ProcessingInstruction processingInstruction(final int line, final int col, final String target) {
ProcessingInstruction p = new ProcessingInstruction();
p.target = target;
p.rawData = "";
@@ -225,14 +225,14 @@ public ProcessingInstruction processingInstruction(String target) {
// =====================================================================
@Override
- public EntityRef entityRef(String name) {
+ public EntityRef entityRef(final int line, final int col, final String name) {
EntityRef e = new org.jdom2.EntityRef();
e.name = name;
return e;
}
@Override
- public EntityRef entityRef(String name, String systemID) {
+ public EntityRef entityRef(final int line, final int col, final String name, String systemID) {
EntityRef e = new EntityRef();
e.name = name;
e.systemID = systemID;
@@ -240,7 +240,7 @@ public EntityRef entityRef(String name, String systemID) {
}
@Override
- public EntityRef entityRef(String name, String publicID, String systemID) {
+ public EntityRef entityRef(final int line, final int col, final String name, String publicID, String systemID) {
EntityRef e = new EntityRef();
e.name = name;
e.publicID = publicID;
@@ -253,7 +253,7 @@ public EntityRef entityRef(String name, String publicID, String systemID) {
// =====================================================================
@Override
- public DocType docType(String elementName, String publicID, String systemID) {
+ public DocType docType(final int line, final int col, final String elementName, String publicID, String systemID) {
DocType d = new DocType();
d.elementName = elementName;
d.publicID = publicID;
@@ -262,12 +262,12 @@ public DocType docType(String elementName, String publicID, String systemID) {
}
@Override
- public DocType docType(String elementName, String systemID) {
+ public DocType docType(final int line, final int col, final String elementName, String systemID) {
return docType(elementName, null, systemID);
}
@Override
- public DocType docType(String elementName) {
+ public DocType docType(final int line, final int col, final String elementName) {
return docType(elementName, null, null);
}
View
314 core/src/java/org/jdom2/input/sax/SAXHandler.java
@@ -54,17 +54,37 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
package org.jdom2.input.sax;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import javax.xml.XMLConstants;
-import org.jdom2.*;
+import org.xml.sax.Attributes;
+import org.xml.sax.DTDHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.DeclHandler;
+import org.xml.sax.ext.LexicalHandler;
+import org.xml.sax.helpers.DefaultHandler;
+
+import org.jdom2.Attribute;
+import org.jdom2.AttributeType;
+import org.jdom2.CDATA;
+import org.jdom2.Comment;
+import org.jdom2.DefaultJDOMFactory;
+import org.jdom2.DocType;
+import org.jdom2.Document;
+import org.jdom2.Element;
+import org.jdom2.EntityRef;
+import org.jdom2.JDOMFactory;
+import org.jdom2.Namespace;
+import org.jdom2.Parent;
+import org.jdom2.ProcessingInstruction;
+import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
-import org.xml.sax.*;
-import org.xml.sax.ext.*;
-import org.xml.sax.helpers.*;
-
/**
* A support class for {@link SAXBuilder} which listens for SAX events.
* <p>
@@ -79,6 +99,13 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* custom implementation of {@link SAXHandlerFactory} to supply your instances
* to {@link SAXBuilder}
* <p>
+ * If the XMLReader producing the SAX Events supports a document Locator, then
+ * this instance will use the locator to supply the line and column data from
+ * the SAX locator to the JDOMFactory. <strong>Note:</strong> the SAX
+ * specification for the SAX Locator indicates that the line and column
+ * represent the position of the <strong>end</strong> of the SAX Event. For
+ * example, the line and column of the simple XML <code>&lt;root /&gt;</code>
+ * would be line 1, column 9.
*
* @see org.jdom2.input.sax
* @author Brett McLaughlin
@@ -88,8 +115,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* @author phil@triloggroup.com
* @author Rolf Lear
*/
-public class SAXHandler extends DefaultHandler
- implements LexicalHandler, DeclHandler, DTDHandler {
+public class SAXHandler extends DefaultHandler implements LexicalHandler,
+ DeclHandler, DTDHandler {
/** The JDOMFactory used for JDOM object creation */
private final JDOMFactory factory;
@@ -98,8 +125,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* Temporary holder for namespaces that have been declared with
* startPrefixMapping, but are not yet available on the element
*/
- private final List<Namespace> declaredNamespaces =
- new ArrayList<Namespace>(32);
+ private final List<Namespace> declaredNamespaces = new ArrayList<Namespace>(
+ 32);
/** Temporary holder for the internal subset */
private final StringBuilder internalSubset = new StringBuilder();
@@ -108,8 +135,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
private final TextBuffer textBuffer = new TextBuffer();
/** The external entities defined in this document */
- private final Map<String, String[]> externalEntities =
- new HashMap<String, String[]>();
+ private final Map<String, String[]> externalEntities = new HashMap<String, String[]>();
/** <code>Document</code> object being built - must be cleared on reset() */
private Document currentDocument = null;
@@ -156,6 +182,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
/** Whether to ignore text containing all whitespace */
private boolean ignoringBoundaryWhite = false;
+
+ private int lastline = 0, lastcol = 0;
/**
* This will create a new <code>SAXHandler</code> that listens to SAX events
@@ -174,7 +202,7 @@ public SAXHandler() {
* @param factory
* <code>JDOMFactory</code> to be used for constructing objects
*/
- public SAXHandler(JDOMFactory factory) {
+ public SAXHandler(final JDOMFactory factory) {
this.factory = factory != null ? factory : new DefaultJDOMFactory();
reset();
}
@@ -223,13 +251,12 @@ public final void reset() {
* @param element
* root element under which content will be built
*/
- protected void pushElement(Element element) {
+ protected void pushElement(final Element element) {
if (atRoot) {
currentDocument.setRootElement(element); // XXX should we use a
- // factory call?
+ // factory call?
atRoot = false;
- }
- else {
+ } else {
factory.addContent(currentElement, element);
}
currentElement = element;
@@ -265,7 +292,7 @@ public JDOMFactory getFactory() {
* <code>boolean</code> indicating whether entity expansion should
* occur.
*/
- public void setExpandEntities(boolean expand) {
+ public void setExpandEntities(final boolean expand) {
this.expand = expand;
}
@@ -291,7 +318,7 @@ public boolean getExpandEntities() {
* @param ignoringWhite
* Whether to ignore ignorable whitespace
*/
- public void setIgnoringElementContentWhitespace(boolean ignoringWhite) {
+ public void setIgnoringElementContentWhitespace(final boolean ignoringWhite) {
this.ignoringWhite = ignoringWhite;
}
@@ -303,7 +330,8 @@ public void setIgnoringElementContentWhitespace(boolean ignoringWhite) {
* @param ignoringBoundaryWhite
* Whether to ignore only whitespace content
*/
- public void setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) {
+ public void setIgnoringBoundaryWhitespace(
+ final boolean ignoringBoundaryWhite) {
this.ignoringBoundaryWhite = ignoringBoundaryWhite;
}
@@ -352,17 +380,15 @@ public void startDocument() {
* when things go wrong
*/
@Override
- public void externalEntityDecl(String name,
- String publicID, String systemID)
- throws SAXException {
+ public void externalEntityDecl(final String name, final String publicID,
+ final String systemID) throws SAXException {
// Store the public and system ids for the name
externalEntities.put(name, new String[] { publicID, systemID });
if (!inInternalSubset)
return;
- internalSubset.append(" <!ENTITY ")
- .append(name);
+ internalSubset.append(" <!ENTITY ").append(name);
appendExternalId(publicID, systemID);
internalSubset.append(">\n");
}
@@ -382,30 +408,21 @@ public void externalEntityDecl(String name,
* <code>String</code> value of attribute
*/
@Override
- public void attributeDecl(String eName, String aName, String type,
- String valueDefault, String value) {
+ public void attributeDecl(final String eName, final String aName,
+ final String type, final String valueDefault, final String value) {
if (!inInternalSubset)
return;
- internalSubset.append(" <!ATTLIST ")
- .append(eName)
- .append(' ')
- .append(aName)
- .append(' ')
- .append(type)
- .append(' ');
+ internalSubset.append(" <!ATTLIST ").append(eName).append(' ')
+ .append(aName).append(' ').append(type).append(' ');
if (valueDefault != null) {
internalSubset.append(valueDefault);
} else {
- internalSubset.append('\"')
- .append(value)
- .append('\"');
+ internalSubset.append('\"').append(value).append('\"');
}
if ((valueDefault != null) && (valueDefault.equals("#FIXED"))) {
- internalSubset.append(" \"")
- .append(value)
- .append('\"');
+ internalSubset.append(" \"").append(value).append('\"');
}
internalSubset.append(">\n");
}
@@ -419,16 +436,13 @@ public void attributeDecl(String eName, String aName, String type,
* <code>String</code> model of the element in DTD syntax
*/
@Override
- public void elementDecl(String name, String model) {
+ public void elementDecl(final String name, final String model) {
// Skip elements that come from the external subset
if (!inInternalSubset)
return;
- internalSubset.append(" <!ELEMENT ")
- .append(name)
- .append(' ')
- .append(model)
- .append(">\n");
+ internalSubset.append(" <!ELEMENT ").append(name).append(' ')
+ .append(model).append(">\n");
}
/**
@@ -440,7 +454,7 @@ public void elementDecl(String name, String model) {
* <code>String</code> value of the entity
*/
@Override
- public void internalEntityDecl(String name, String value) {
+ public void internalEntityDecl(final String name, final String value) {
// Skip entities that come from the external subset
if (!inInternalSubset)
@@ -452,9 +466,7 @@ public void internalEntityDecl(String name, String value) {
} else {
internalSubset.append(name);
}
- internalSubset.append(" \"")
- .append(value)
- .append("\">\n");
+ internalSubset.append(" \"").append(value).append("\">\n");
}
/**
@@ -471,7 +483,7 @@ public void internalEntityDecl(String name, String value) {
* when things go wrong
*/
@Override
- public void processingInstruction(String target, String data)
+ public void processingInstruction(final String target, final String data)
throws SAXException {
if (suppress)
@@ -479,11 +491,15 @@ public void processingInstruction(String target, String data)
flushCharacters();
+ final ProcessingInstruction pi = (currentLocator == null) ? factory
+ .processingInstruction(target, data) : factory
+ .processingInstruction(currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), target, data);
+
if (atRoot) {
- factory.addContent(currentDocument, factory.processingInstruction(target, data));
+ factory.addContent(currentDocument, pi);
} else {
- factory.addContent(getCurrentElement(),
- factory.processingInstruction(target, data));
+ factory.addContent(getCurrentElement(), pi);
}
}
@@ -497,8 +513,7 @@ public void processingInstruction(String target, String data)
* when things go wrong
*/
@Override
- public void skippedEntity(String name)
- throws SAXException {
+ public void skippedEntity(final String name) throws SAXException {
// We don't handle parameter entity references.
if (name.startsWith("%"))
@@ -506,7 +521,11 @@ public void skippedEntity(String name)
flushCharacters();
- factory.addContent(getCurrentElement(), factory.entityRef(name));
+ final EntityRef er = currentLocator == null ? factory.entityRef(name)
+ : factory.entityRef(currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), name);
+
+ factory.addContent(getCurrentElement(), er);
}
/**
@@ -519,13 +538,13 @@ public void skippedEntity(String name)
* <code>String</code> namespace URI.
*/
@Override
- public void startPrefixMapping(String prefix, String uri)
+ public void startPrefixMapping(final String prefix, final String uri)
throws SAXException {
if (suppress)
return;
- Namespace ns = Namespace.getNamespace(prefix, uri);
+ final Namespace ns = Namespace.getNamespace(prefix, uri);
declaredNamespaces.add(ns);
}
@@ -550,9 +569,8 @@ public void startPrefixMapping(String prefix, String uri)
* when things go wrong
*/
@Override
- public void startElement(String namespaceURI, String localName,
- String qName, Attributes atts)
- throws SAXException {
+ public void startElement(final String namespaceURI, String localName,
+ final String qName, final Attributes atts) throws SAXException {
if (suppress)
return;
@@ -560,7 +578,7 @@ public void startElement(String namespaceURI, String localName,
// If QName is set, then set prefix and local name as necessary
if (!"".equals(qName)) {
- int colon = qName.indexOf(':');
+ final int colon = qName.indexOf(':');
if (colon > 0) {
prefix = qName.substring(0, colon);
@@ -574,8 +592,12 @@ public void startElement(String namespaceURI, String localName,
// At this point either prefix and localName are set correctly or
// there is an error in the parser.
- Namespace namespace = Namespace.getNamespace(prefix, namespaceURI);
- Element element = factory.element(localName, namespace);
+ final Namespace namespace = Namespace
+ .getNamespace(prefix, namespaceURI);
+ final Element element = currentLocator == null ? factory.element(
+ localName, namespace) : factory.element(
+ currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), localName, namespace);
// Take leftover declared namespaces and add them to this element's
// map of namespaces
@@ -587,7 +609,7 @@ public void startElement(String namespaceURI, String localName,
if (atRoot) {
factory.setRoot(currentDocument, element); // Yes, use a factory
- // call...
+ // call...
atRoot = false;
} else {
factory.addContent(getCurrentElement(), element);
@@ -599,7 +621,7 @@ public void startElement(String namespaceURI, String localName,
String attPrefix = "";
String attLocalName = atts.getLocalName(i);
- String attQName = atts.getQName(i);
+ final String attQName = atts.getQName(i);
// If attribute QName is set, then set attribute prefix and
// attribute local name as necessary
@@ -612,7 +634,7 @@ public void startElement(String namespaceURI, String localName,
continue;
}
- int attColon = attQName.indexOf(':');
+ final int attColon = attQName.indexOf(':');
if (attColon > 0) {
attPrefix = attQName.substring(0, attColon);
@@ -624,13 +646,14 @@ public void startElement(String namespaceURI, String localName,
}
}
- AttributeType attType = AttributeType.getAttributeType(atts.getType(i));
- String attValue = atts.getValue(i);
- String attURI = atts.getURI(i);
+ final AttributeType attType = AttributeType.getAttributeType(atts
+ .getType(i));
+ final String attValue = atts.getValue(i);
+ final String attURI = atts.getURI(i);
- if (XMLConstants.XMLNS_ATTRIBUTE.equals(attLocalName) ||
- XMLConstants.XMLNS_ATTRIBUTE.equals(attPrefix) ||
- XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attURI)) {
+ if (XMLConstants.XMLNS_ATTRIBUTE.equals(attLocalName)
+ || XMLConstants.XMLNS_ATTRIBUTE.equals(attPrefix)
+ || XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(attURI)) {
// use the actual Namespace to check too, because, in theory, a
// namespace-aware parser does not need to set the qName unless
// the namespace-prefixes feature is set as well.
@@ -653,9 +676,10 @@ public void startElement(String namespaceURI, String localName,
// is an attribute definition that has form="qualified".
// <xs:attribute name="attname" form="qualified" ... />
// or the schema sets attributeFormDefault="qualified"
- HashMap<String, Namespace> tmpmap = new HashMap<String, Namespace>();
- for (Namespace nss : element.getNamespacesInScope()) {
- if (nss.getPrefix().length() > 0 && nss.getURI().equals(attURI)) {
+ final HashMap<String, Namespace> tmpmap = new HashMap<String, Namespace>();
+ for (final Namespace nss : element.getNamespacesInScope()) {
+ if (nss.getPrefix().length() > 0
+ && nss.getURI().equals(attURI)) {
attPrefix = nss.getPrefix();
break;
}
@@ -675,7 +699,7 @@ public void startElement(String namespaceURI, String localName,
// All of these things are possible.
// Create some sort of default prefix.
int cnt = 0;
- String base = "attns";
+ final String base = "attns";
String pfx = base + cnt;
while (tmpmap.containsKey(pfx)) {
cnt++;
@@ -684,10 +708,10 @@ public void startElement(String namespaceURI, String localName,
attPrefix = pfx;
}
}
- Namespace attNs = Namespace.getNamespace(attPrefix, attURI);
+ final Namespace attNs = Namespace.getNamespace(attPrefix, attURI);
- Attribute attribute = factory.attribute(attLocalName, attValue,
- attType, attNs);
+ final Attribute attribute = factory.attribute(attLocalName,
+ attValue, attType, attNs);
factory.setAttribute(element, attribute);
}
@@ -700,8 +724,8 @@ public void startElement(String namespaceURI, String localName,
* @param element
* <code>Element</code> to read namespaces from.
*/
- private void transferNamespaces(Element element) {
- for (Namespace ns : declaredNamespaces) {
+ private void transferNamespaces(final Element element) {
+ for (final Namespace ns : declaredNamespaces) {
if (ns != element.getNamespace()) {
element.addNamespaceDeclaration(ns);
}
@@ -720,7 +744,7 @@ private void transferNamespaces(Element element) {
* <code>int</code> length of data.
*/
@Override
- public void characters(char[] ch, int start, int length)
+ public void characters(final char[] ch, final int start, final int length)
throws SAXException {
if (suppress || (length == 0 && !inCDATA))
@@ -731,6 +755,11 @@ public void characters(char[] ch, int start, int length)
}
textBuffer.append(ch, start, length);
+
+ if (currentLocator != null) {
+ lastline = currentLocator.getLineNumber();
+ lastcol = currentLocator.getColumnNumber();
+ }
}
/**
@@ -748,8 +777,8 @@ public void characters(char[] ch, int start, int length)
* when things go wrong
*/
@Override
- public void ignorableWhitespace(char[] ch, int start, int length)
- throws SAXException {
+ public void ignorableWhitespace(final char[] ch, final int start,
+ final int length) throws SAXException {
if (!ignoringWhite) {
characters(ch, start, length);
}
@@ -767,8 +796,7 @@ protected void flushCharacters() throws SAXException {
if (!textBuffer.isAllWhitespace()) {
flushCharacters(textBuffer.toString());
}
- }
- else {
+ } else {
flushCharacters(textBuffer.toString());
}
textBuffer.clear();
@@ -784,7 +812,7 @@ protected void flushCharacters() throws SAXException {
* @throws SAXException
* if the state of the handler does not allow this.
*/
- protected void flushCharacters(String data) throws SAXException {
+ protected void flushCharacters(final String data) throws SAXException {
if (data.length() == 0 && !inCDATA) {
previousCDATA = inCDATA;
return;
@@ -798,10 +826,13 @@ protected void flushCharacters(String data) throws SAXException {
*/
if (previousCDATA) {
- factory.addContent(getCurrentElement(), factory.cdata(data));
- }
- else {
- factory.addContent(getCurrentElement(), factory.text(data));
+ final CDATA cdata = currentLocator == null ? factory.cdata(data)
+ : factory.cdata(lastline, lastcol, data);
+ factory.addContent(getCurrentElement(), cdata);
+ } else {
+ final Text text = currentLocator == null ? factory.text(data)
+ : factory.text(lastline, lastcol, data);
+ factory.addContent(getCurrentElement(), text);
}
previousCDATA = inCDATA;
@@ -823,8 +854,8 @@ protected void flushCharacters(String data) throws SAXException {
* when things go wrong
*/
@Override
- public void endElement(String namespaceURI, String localName,
- String qName) throws SAXException {
+ public void endElement(final String namespaceURI, final String localName,
+ final String qName) throws SAXException {
if (suppress)
return;
@@ -832,18 +863,16 @@ public void endElement(String namespaceURI, String localName,
flushCharacters();
if (!atRoot) {
- Parent p = currentElement.getParent();
+ final Parent p = currentElement.getParent();
if (p instanceof Document) {
atRoot = true;
- }
- else {
+ } else {
currentElement = (Element) p;
}
- }
- else {
+ } else {
throw new SAXException(
- "Ill-formed XML document (missing opening tag for " +
- localName + ")");
+ "Ill-formed XML document (missing opening tag for "
+ + localName + ")");
}
}
@@ -860,12 +889,16 @@ public void endElement(String namespaceURI, String localName,
* <code>String</code> system ID of DTD
*/
@Override
- public void startDTD(String name, String publicID, String systemID)
- throws SAXException {
+ public void startDTD(final String name, final String publicID,
+ final String systemID) throws SAXException {
flushCharacters(); // Is this needed here?
- factory.addContent(currentDocument, factory.docType(name, publicID, systemID));
+ final DocType doctype = currentLocator == null ? factory.docType(name,
+ publicID, systemID) : factory.docType(
+ currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), name, publicID, systemID);
+ factory.addContent(currentDocument, doctype);
inDTD = true;
inInternalSubset = true;
}
@@ -876,13 +909,14 @@ public void startDTD(String name, String publicID, String systemID)
@Override
public void endDTD() {
- currentDocument.getDocType().setInternalSubset(internalSubset.toString());
+ currentDocument.getDocType().setInternalSubset(
+ internalSubset.toString());
inDTD = false;
inInternalSubset = false;
}
@Override
- public void startEntity(String name) throws SAXException {
+ public void startEntity(final String name) throws SAXException {
entityDepth++;
if (expand || entityDepth > 1) {
@@ -897,17 +931,14 @@ public void startEntity(String name) throws SAXException {
}
// Ignore DTD references, and translate the standard 5
- if ((!inDTD) &&
- (!name.equals("amp")) &&
- (!name.equals("lt")) &&
- (!name.equals("gt")) &&
- (!name.equals("apos")) &&
- (!name.equals("quot"))) {
+ if ((!inDTD) && (!name.equals("amp")) && (!name.equals("lt"))
+ && (!name.equals("gt")) && (!name.equals("apos"))
+ && (!name.equals("quot"))) {
if (!expand) {
String pub = null;
String sys = null;
- String[] ids = externalEntities.get(name);
+ final String[] ids = externalEntities.get(name);
if (ids != null) {
pub = ids[0]; // may be null, that's OK
sys = ids[1]; // may be null, that's OK
@@ -922,7 +953,10 @@ public void startEntity(String name) throws SAXException {
*/
if (!atRoot) {
flushCharacters();
- EntityRef entity = factory.entityRef(name, pub, sys);
+ final EntityRef entity = currentLocator == null ? factory
+ .entityRef(name, pub, sys) : factory.entityRef(
+ currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), name, pub, sys);
// no way to tell if the entity was from an attribute or
// element so just assume element
@@ -934,7 +968,7 @@ public void startEntity(String name) throws SAXException {
}
@Override
- public void endEntity(String name) throws SAXException {
+ public void endEntity(final String name) throws SAXException {
entityDepth--;
if (entityDepth == 0) {
// No way are we suppressing if not in an entity,
@@ -986,7 +1020,7 @@ public void endCDATA() throws SAXException {
* if the state of the handler disallows this call
*/
@Override
- public void comment(char[] ch, int start, int length)
+ public void comment(final char[] ch, final int start, final int length)
throws SAXException {
if (suppress)
@@ -994,18 +1028,20 @@ public void comment(char[] ch, int start, int length)
flushCharacters();
- String commentText = new String(ch, start, length);
+ final String commentText = new String(ch, start, length);
if (inDTD && inInternalSubset && (expand == false)) {
- internalSubset.append(" <!--")
- .append(commentText)
- .append("-->\n");
+ internalSubset.append(" <!--").append(commentText).append("-->\n");
return;
}
if ((!inDTD) && (!commentText.equals(""))) {
+ final Comment comment = currentLocator == null ? factory
+ .comment(commentText) : factory.comment(
+ currentLocator.getLineNumber(),
+ currentLocator.getColumnNumber(), commentText);
if (atRoot) {
- factory.addContent(currentDocument, factory.comment(commentText));
+ factory.addContent(currentDocument, comment);
} else {
- factory.addContent(getCurrentElement(), factory.comment(commentText));
+ factory.addContent(getCurrentElement(), comment);
}
}
}
@@ -1021,14 +1057,13 @@ public void comment(char[] ch, int start, int length)
* the system ID of the notation
*/
@Override
- public void notationDecl(String name, String publicID, String systemID)
- throws SAXException {
+ public void notationDecl(final String name, final String publicID,
+ final String systemID) throws SAXException {
if (!inInternalSubset)
return;
- internalSubset.append(" <!NOTATION ")
- .append(name);
+ internalSubset.append(" <!NOTATION ").append(name);
appendExternalId(publicID, systemID);
internalSubset.append(">\n");
}
@@ -1046,17 +1081,15 @@ public void notationDecl(String name, String publicID, String systemID)
* <code>String</code> of the unparsed entity decl
*/
@Override
- public void unparsedEntityDecl(String name, String publicID,
- String systemID, String notationName) {
+ public void unparsedEntityDecl(final String name, final String publicID,
+ final String systemID, final String notationName) {
if (!inInternalSubset)
return;
- internalSubset.append(" <!ENTITY ")
- .append(name);
+ internalSubset.append(" <!ENTITY ").append(name);
appendExternalId(publicID, systemID);
- internalSubset.append(" NDATA ")
- .append(notationName);
+ internalSubset.append(" NDATA ").append(notationName);
internalSubset.append(">\n");
}
@@ -1069,22 +1102,17 @@ public void unparsedEntityDecl(String name, String publicID,
* @param systemID
* the system ID
*/
- private void appendExternalId(String publicID, String systemID) {
+ private void appendExternalId(final String publicID, final String systemID) {
if (publicID != null) {
- internalSubset.append(" PUBLIC \"")
- .append(publicID)
- .append('\"');
+ internalSubset.append(" PUBLIC \"").append(publicID).append('\"');
}
if (systemID != null) {
if (publicID == null) {
internalSubset.append(" SYSTEM ");
- }
- else {
+ } else {
internalSubset.append(' ');
}
- internalSubset.append('\"')
- .append(systemID)
- .append('\"');
+ internalSubset.append('\"').append(systemID).append('\"');
}
}
@@ -1117,7 +1145,7 @@ public Element getCurrentElement() throws SAXException {
* SAX document event.
*/
@Override
- public void setDocumentLocator(Locator locator) {
+ public void setDocumentLocator(final Locator locator) {
this.currentLocator = locator;
}
View
113 core/src/java/org/jdom2/located/LCDATA.java
@@ -0,0 +1,113 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.located;
+
+import org.jdom2.CDATA;
+import org.jdom2.IllegalDataException;
+import org.jdom2.Text;
+
+/**
+ * An XML CDATA section. Represents character-based content within an XML
+ * document that should be output within special CDATA tags. Semantically it's
+ * identical to a simple {@link Text} object, but output behavior is different.
+ * CDATA makes no guarantees about the underlying textual representation of
+ * character data, but does expose that data as a Java String.
+ *
+ * @author Rolf Lear
+ */
+public class LCDATA extends CDATA implements Located {
+
+ /**
+ * This constructor creates a new <code>LCDATA</code> node, with the
+ * supplied string value as it's character content.
+ *
+ * @param str the node's character content.
+ * @throws IllegalDataException if <code>str</code> contains an
+ * illegal character such as a vertical tab (as determined
+ * by {@link org.jdom2.Verifier#checkCharacterData})
+ */
+ public LCDATA(String str) {
+ super(str);
+ }
+
+ /**
+ * JDOM2 Serialization. In this case, DocType is simple.
+ */
+ private static final long serialVersionUID = 200L;
+
+ private int line, col;
+
+ @Override
+ public int getLine() {
+ return line;
+ }
+
+ @Override
+ public int getColumn() {
+ return col;
+ }
+
+ @Override
+ public void setLine(int line) {
+ this.line = line;
+ }
+
+ @Override
+ public void setColumn(int col) {
+ this.col = col;
+ }
+
+
+}
View
104 core/src/java/org/jdom2/located/LComment.java
@@ -0,0 +1,104 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.located;
+
+import org.jdom2.Comment;
+
+/**
+ * An XML comment. Methods allow the user to get and set the text of the
+ * comment.
+ *
+ * @author Rolf Lear
+ */
+public class LComment extends Comment implements Located {
+
+ /**
+ * This creates the comment with the supplied text.
+ *
+ * @param text <code>String</code> content of comment.
+ */
+ public LComment(String text) {
+ super(text);
+ }
+
+ /**
+ * JDOM2 Serialization. In this case, DocType is simple.
+ */
+ private static final long serialVersionUID = 200L;
+
+ private int line, col;
+
+ @Override
+ public int getLine() {
+ return line;
+ }
+
+ @Override
+ public int getColumn() {
+ return col;
+ }
+
+ @Override
+ public void setLine(int line) {
+ this.line = line;
+ }
+
+ @Override
+ public void setColumn(int col) {
+ this.col = col;
+ }
+
+
+}
View
147 core/src/java/org/jdom2/located/LDocType.java
@@ -0,0 +1,147 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission
+ from the JDOM Project Management <request_AT_jdom_DOT_org>.
+
+ In addition, we request (but do not require) that you include in the
+ end-user documentation provided with the redistribution and/or in the
+ software itself an acknowledgement equivalent to the following:
+ "This product includes software developed by the
+ JDOM Project (http://www.jdom.org/)."
+ Alternatively, the acknowledgment may be graphical using the logos
+ available at http://www.jdom.org/images/logos.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many
+ individuals on behalf of the JDOM Project and was originally
+ created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
+ Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
+ on the JDOM Project, please see <http://www.jdom.org/>.
+
+ */
+
+package org.jdom2.located;
+
+import org.jdom2.DocType;
+import org.jdom2.IllegalDataException;
+import org.jdom2.IllegalNameException;
+
+/**
+ * An XML DOCTYPE declaration. Method allow the user to get and set the
+ * root element name, public id, and system id.
+ *
+ * @author Rolf Lear
+ */
+public class LDocType extends DocType implements Located {
+
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name and a reference to an
+ * external DTD.
+ *
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @param publicID <code>String</code> public ID of
+ * referenced DTD
+ * @param systemID <code>String</code> system ID of
+ * referenced DTD
+ * @throws IllegalDataException if the given system ID is not a legal
+ * system literal or the public ID is not a legal public ID.
+ * @throws IllegalNameException if the given root element name is not a
+ * legal XML element name.
+ */
+ public LDocType(String elementName, String publicID, String systemID) {
+ super(elementName, publicID, systemID);
+ }
+
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name and reference to an
+ * external DTD.
+ *
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @param systemID <code>String</code> system ID of
+ * referenced DTD
+ * @throws IllegalDataException if the given system ID is not a legal
+ * system literal.
+ * @throws IllegalNameException if the given root element name is not a
+ * legal XML element name.
+ */
+ public LDocType(String elementName, String systemID) {
+ super(elementName, systemID);
+ }
+
+ /**
+ * This will create the <code>DocType</code> with
+ * the specified element name
+ *
+ * @param elementName <code>String</code> name of
+ * element being constrained.
+ * @throws IllegalNameException if the given root element name is not a
+ * legal XML element name.
+ */
+ public LDocType(String elementName) {
+ super(elementName);
+ }
+
+ /**
+ * JDOM2 Serialization. In this case, DocType is simple.
+ */
+ private static final long serialVersionUID = 200L;
+
+ private int line, col;
+
+ @Override
+ public int getLine() {
+ return line;
+ }
+
+ @Override
+ public int getColumn() {
+ return col;
+ }
+
+ @Override
+ public void setLine(int line) {
+ this.line = line;
+ }
+
+ @Override
+ public void setColumn(int col) {
+ this.col = col;
+ }
+
+}
View
152 core/src/java/org/jdom2/located/LElement.java
@@ -0,0 +1,152 @@
+/*--
+
+ Copyright (C) 2012 Jason Hunter & Brett McLaughlin.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions, and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions, and the disclaimer that follows
+ these conditions in the documentation and/or other materials
+ provided with the distribution.
+
+ 3. The name "JDOM" must not be used to endorse or promote products
+ derived from this software without prior written permission. For
+ written permission, please contact <request_AT_jdom_DOT_org>.
+
+ 4. Products derived from this software may not be called "JDOM", nor
+ may "JDOM" appear in their name, without prior written permission