You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by hi...@apache.org on 2001/02/13 20:45:20 UTC
cvs commit: xml-batik/sources/org/apache/batik/dom/util DocumentDescriptor.java DocumentFactory.java SAXDocumentFactory.java
hillion 01/02/13 11:45:19
Modified: sources/org/apache/batik/dom/svg SAXSVGDocumentFactory.java
SVGDocumentFactory.java
sources/org/apache/batik/dom/util DocumentFactory.java
SAXDocumentFactory.java
Added: sources/org/apache/batik/dom/util DocumentDescriptor.java
Log:
Added a mechanism to track the line number of an element.
Revision Changes Path
1.7 +10 -1 xml-batik/sources/org/apache/batik/dom/svg/SAXSVGDocumentFactory.java
Index: SAXSVGDocumentFactory.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/svg/SAXSVGDocumentFactory.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- SAXSVGDocumentFactory.java 2001/02/11 20:36:42 1.6
+++ SAXSVGDocumentFactory.java 2001/02/13 19:45:10 1.7
@@ -34,7 +34,7 @@
* from an URI using SAX2.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: SAXSVGDocumentFactory.java,v 1.6 2001/02/11 20:36:42 hillion Exp $
+ * @version $Id: SAXSVGDocumentFactory.java,v 1.7 2001/02/13 19:45:10 hillion Exp $
*/
public class SAXSVGDocumentFactory
extends SAXDocumentFactory
@@ -57,6 +57,15 @@
*/
public SAXSVGDocumentFactory(String parser) {
super(SVGDOMImplementation.getDOMImplementation(), parser);
+ }
+
+ /**
+ * Creates a new SVGDocumentFactory object.
+ * @param parser The SAX2 parser classname.
+ * @param dd Whether a document descriptor must be generated.
+ */
+ public SAXSVGDocumentFactory(String parser, boolean dd) {
+ super(SVGDOMImplementation.getDOMImplementation(), parser, dd);
}
/**
1.6 +4 -2 xml-batik/sources/org/apache/batik/dom/svg/SVGDocumentFactory.java
Index: SVGDocumentFactory.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/svg/SVGDocumentFactory.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- SVGDocumentFactory.java 2001/01/08 13:19:52 1.5
+++ SVGDocumentFactory.java 2001/02/13 19:45:11 1.6
@@ -12,13 +12,15 @@
import java.io.IOException;
import java.io.Reader;
+import org.apache.batik.dom.util.DocumentFactory;
+
/**
* This interface represents an object which can build a SVGDocument.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: SVGDocumentFactory.java,v 1.5 2001/01/08 13:19:52 hillion Exp $
+ * @version $Id: SVGDocumentFactory.java,v 1.6 2001/02/13 19:45:11 hillion Exp $
*/
-public interface SVGDocumentFactory {
+public interface SVGDocumentFactory extends DocumentFactory {
/**
* Creates a SVGOMDocument instance.
1.6 +7 -1 xml-batik/sources/org/apache/batik/dom/util/DocumentFactory.java
Index: DocumentFactory.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/util/DocumentFactory.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DocumentFactory.java 2001/01/08 13:19:52 1.5
+++ DocumentFactory.java 2001/02/13 19:45:16 1.6
@@ -18,7 +18,7 @@
* This interface represents an object which can build a Document.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: DocumentFactory.java,v 1.5 2001/01/08 13:19:52 hillion Exp $
+ * @version $Id: DocumentFactory.java,v 1.6 2001/02/13 19:45:16 hillion Exp $
*/
public interface DocumentFactory {
@@ -53,4 +53,10 @@
Document createDocument(String ns, String root, String uri, Reader r)
throws IOException;
+ /**
+ * Returns the document descriptor associated with the latest created
+ * document.
+ * @return null if no document or descriptor was previously generated.
+ */
+ DocumentDescriptor getDocumentDescriptor();
}
1.3 +64 -5 xml-batik/sources/org/apache/batik/dom/util/SAXDocumentFactory.java
Index: SAXDocumentFactory.java
===================================================================
RCS file: /home/cvs/xml-batik/sources/org/apache/batik/dom/util/SAXDocumentFactory.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SAXDocumentFactory.java 2001/02/11 20:36:42 1.2
+++ SAXDocumentFactory.java 2001/02/13 19:45:17 1.3
@@ -25,6 +25,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
@@ -36,7 +37,7 @@
* from an URI using SAX2.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
- * @version $Id: SAXDocumentFactory.java,v 1.2 2001/02/11 20:36:42 hillion Exp $
+ * @version $Id: SAXDocumentFactory.java,v 1.3 2001/02/13 19:45:17 hillion Exp $
*/
public class SAXDocumentFactory
extends DefaultHandler
@@ -59,11 +60,26 @@
protected Document document;
/**
+ * The created document descriptor.
+ */
+ protected DocumentDescriptor documentDescriptor;
+
+ /**
+ * Whether a document descriptor must be generated.
+ */
+ protected boolean createDocumentDescriptor;
+
+ /**
* The current node.
*/
protected Node currentNode;
/**
+ * The locator.
+ */
+ protected Locator locator;
+
+ /**
* Whether the parser currently parses a CDATA section.
*/
protected boolean inCDATA;
@@ -85,15 +101,31 @@
/**
* Creates a new SAXDocumentFactory object.
+ * No document descriptor will be created while generating a document.
* @param impl The DOM implementation to use for building the DOM tree.
* @param parser The SAX2 parser classname.
*/
- public SAXDocumentFactory(DOMImplementation impl, String parser) {
- implementation = impl;
- parserClassName = parser;
+ public SAXDocumentFactory(DOMImplementation impl,
+ String parser) {
+ implementation = impl;
+ parserClassName = parser;
}
/**
+ * Creates a new SAXDocumentFactory object.
+ * @param impl The DOM implementation to use for building the DOM tree.
+ * @param parser The SAX2 parser classname.
+ * @param dd Whether a document descriptor must be generated.
+ */
+ public SAXDocumentFactory(DOMImplementation impl,
+ String parser,
+ boolean dd) {
+ implementation = impl;
+ parserClassName = parser;
+ createDocumentDescriptor = dd;
+ }
+
+ /**
* Creates a Document instance.
* @param ns The namespace URI of the root element of the document.
* @param root The name of the root element of the document.
@@ -176,6 +208,23 @@
}
/**
+ * Returns the document descriptor associated with the latest created
+ * document.
+ * @return null if no document or descriptor was previously generated.
+ */
+ public DocumentDescriptor getDocumentDescriptor() {
+ return documentDescriptor;
+ }
+
+ /**
+ * <b>SAX</b>: Implements {@link
+ * org.xml.sax.ContentHandler#setDocumentLocator(Locator)}.
+ */
+ public void setDocumentLocator(Locator l) {
+ locator = l;
+ }
+
+ /**
* <b>SAX</b>: Implements {@link
* org.xml.sax.ContentHandler#startDocument()}.
*/
@@ -189,6 +238,12 @@
inCDATA = false;
inDTD = false;
currentNode = document;
+
+ if (createDocumentDescriptor) {
+ documentDescriptor = new DocumentDescriptor();
+ } else {
+ documentDescriptor = null;
+ }
}
/**
@@ -256,6 +311,11 @@
}
currentNode = e;
+ // Storage of the line number.
+ if (createDocumentDescriptor && locator != null) {
+ documentDescriptor.setLocationLine(e, locator.getLineNumber());
+ }
+
// Attributes creation
for (int i = 0; i < len; i++) {
String aname = attributes.getQName(i);
@@ -289,7 +349,6 @@
currentNode = currentNode.getParentNode();
namespaces.pop();
}
-
/**
* <b>SAX</b>: Implements {@link
1.1 xml-batik/sources/org/apache/batik/dom/util/DocumentDescriptor.java
Index: DocumentDescriptor.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.batik.dom.util;
import org.w3c.dom.Element;
/**
* This class contains informations about a document.
*
* @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
* @version $Id: DocumentDescriptor.java,v 1.1 2001/02/13 19:45:15 hillion Exp $
*/
public class DocumentDescriptor {
/**
* The table initial capacity
*/
protected final static int INITIAL_CAPACITY = 101;
/**
* The underlying array
*/
protected Entry[] table;
/**
* The number of entries
*/
protected int count;
/**
* Creates a new table.
*/
public DocumentDescriptor() {
table = new Entry[INITIAL_CAPACITY];
}
/**
* Returns the number of elements in the document.
*/
public int getNumberOfElements() {
return count;
}
/**
* Returns the location in the source file of the end element.
* @return zero if the information is unknown.
*/
public int getLocationLine(Element elt) {
int hash = elt.hashCode() & 0x7FFFFFFF;
int index = hash % table.length;
for (Entry e = table[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.element.equals(elt)) {
return e.locationLine;
}
}
return 0;
}
/**
* Sets the location in the source file of the end element.
*/
public void setLocationLine(Element elt, int line) {
int hash = elt.hashCode() & 0x7FFFFFFF;
int index = hash % table.length;
for (Entry e = table[index]; e != null; e = e.next) {
if ((e.hash == hash) && e.element.equals(elt)) {
e.locationLine = line;
}
}
// The key is not in the hash table
int len = table.length;
if (count++ >= (len * 3) >>> 2) {
rehash();
index = hash % table.length;
}
Entry e = new Entry(hash, elt, line, table[index]);
table[index] = e;
}
/**
* Rehash the table
*/
protected void rehash () {
Entry[] oldTable = table;
table = new Entry[oldTable.length * 2 + 1];
for (int i = oldTable.length-1; i >= 0; i--) {
for (Entry old = oldTable[i]; old != null;) {
Entry e = old;
old = old.next;
int index = e.hash % table.length;
e.next = table[index];
table[index] = e;
}
}
}
/**
* To manage collisions
*/
protected static class Entry {
/**
* The hash code
*/
public int hash;
/**
* The element
*/
public Element element;
/**
* The line number.
*/
public int locationLine;
/**
* The next entry
*/
public Entry next;
/**
* Creates a new entry
*/
public Entry(int hash, Element element, int locationLine, Entry next) {
this.hash = hash;
this.element = element;
this.locationLine = locationLine;
this.next = next;
}
}
}