You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2004/02/29 10:21:33 UTC
cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util DomHelper.java
antonio 2004/02/29 01:21:33
Modified: src/blocks/woody/java/org/apache/cocoon/woody/util
DomHelper.java
Log:
Formatting code
Revision Changes Path
1.13 +115 -55 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java
Index: DomHelper.java
===================================================================
RCS file: /home/cvs//cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- DomHelper.java 19 Feb 2004 22:13:28 -0000 1.12
+++ DomHelper.java 29 Feb 2004 09:21:33 -0000 1.13
@@ -76,20 +76,23 @@
/**
* Helper class to create and retrieve information from DOM-trees. It provides
- * some functionality comparable to what's found in Avalon's Configuration objects. These
- * lasts one could however not be used by Woody because they don't provide an
- * accurate model of an XML file (no mixed content, no namespaced attributes, no namespace declarations, ...).
+ * some functionality comparable to what's found in Avalon's Configuration
+ * objects. These lasts one could however not be used by Woody because they
+ * don't provide an accurate model of an XML file (no mixed content,
+ * no namespaced attributes, no namespace declarations, ...).
*
- * <p>This class depends specifically on the Xerces DOM implementation to be able to provide
- * information about the location of elements in their source XML file. See the {@link #getLocation(Element)} method.
+ * <p>This class depends specifically on the Xerces DOM implementation to be
+ * able to provide information about the location of elements in their source
+ * XML file. See the {@link #getLocation(Element)} method.
*
* @version CVS $Id$
*/
public class DomHelper {
/**
- * Retrieves the location of an element node in the source file from which the Document was created.
- * This will only work for Document's created with the method {@link #parse(InputSource)} of this class.
+ * Retrieves the location of an element node in the source file from which
+ * the Document was created. This will only work for Document's created
+ * with the method {@link #parse(InputSource)} of this class.
*/
public static String getLocation(Element element) {
String location = null;
@@ -143,29 +146,37 @@
}
/**
- * Returns all Element children of an Element that belong to the given namespace.
+ * Returns all Element children of an Element that belong to the given
+ * namespace.
*/
- public static Element[] getChildElements(Element element, String namespace) {
+ public static Element[] getChildElements(Element element,
+ String namespace) {
ArrayList elements = new ArrayList();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
- if (node instanceof Element && namespace.equals(node.getNamespaceURI()))
+ if (node instanceof Element
+ && namespace.equals(node.getNamespaceURI()))
elements.add(node);
}
return (Element[])elements.toArray(new Element[0]);
}
/**
- * Returns all Element children of an Element that belong to the given namespace and have the given local name.
+ * Returns all Element children of an Element that belong to the given
+ * namespace and have the given local name.
*/
- public static Element[] getChildElements(Element element, String namespace, String localName) {
+ public static Element[] getChildElements(Element element,
+ String namespace, String localName) {
ArrayList elements = new ArrayList();
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
- if (node instanceof Element && namespace.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
+ if (node instanceof Element
+ && namespace.equals(node.getNamespaceURI())
+ && localName.equals(node.getLocalName())) {
elements.add(node);
+ }
}
return (Element[])elements.toArray(new Element[0]);
}
@@ -174,7 +185,8 @@
* Returns the first child element with the given namespace and localName,
* or null if there is no such element.
*/
- public static Element getChildElement(Element element, String namespace, String localName) {
+ public static Element getChildElement(Element element, String namespace,
+ String localName) {
Element node = null;
try {
node = getChildElement(element, namespace, localName, false);
@@ -184,35 +196,47 @@
return node;
}
- public static Element getChildElement(Element element, String namespace, String localName, boolean required) throws Exception {
+ public static Element getChildElement(Element element, String namespace,
+ String localName, boolean required) throws Exception {
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
- if (node instanceof Element && namespace.equals(node.getNamespaceURI()) && localName.equals(node.getLocalName()))
+ if (node instanceof Element
+ && namespace.equals(node.getNamespaceURI())
+ && localName.equals(node.getLocalName())) {
return (Element)node;
+ }
}
- if (required)
- throw new Exception("Missing element \"" + localName + "\" as child of element \"" + element.getTagName() + "\" at " + DomHelper.getLocation(element));
- else
+ if (required) {
+ throw new Exception("Missing element \"" + localName +
+ "\" as child of element \"" + element.getTagName() +
+ "\" at " + DomHelper.getLocation(element));
+ } else {
return null;
+ }
}
/**
* Returns the value of an element's attribute, but throws an exception
* if the element has no such attribute.
*/
- public static String getAttribute(Element element, String attributeName) throws Exception {
+ public static String getAttribute(Element element, String attributeName)
+ throws Exception {
String attrValue = element.getAttribute(attributeName);
if (attrValue.equals("")) {
- throw new Exception("Missing attribute \"" + attributeName + "\" on element \"" + element.getTagName() + "\" at " + getLocation(element));
+ throw new Exception("Missing attribute \"" + attributeName +
+ "\" on element \"" + element.getTagName() +
+ "\" at " + getLocation(element));
}
return attrValue;
}
/**
- * Returns the value of an element's attribute, or a default value if the element has no such attribute.
+ * Returns the value of an element's attribute, or a default value if the
+ * element has no such attribute.
*/
- public static String getAttribute(Element element, String attributeName, String defaultValue) throws Exception {
+ public static String getAttribute(Element element, String attributeName,
+ String defaultValue) throws Exception {
String attrValue = element.getAttribute(attributeName);
if (attrValue.equals("")) {
return defaultValue;
@@ -220,16 +244,21 @@
return attrValue;
}
- public static int getAttributeAsInteger(Element element, String attributeName) throws Exception {
+ public static int getAttributeAsInteger(Element element,
+ String attributeName) throws Exception {
String attrValue = getAttribute(element, attributeName);
try {
return Integer.parseInt(attrValue);
} catch (NumberFormatException e) {
- throw new Exception("Cannot parse the value \"" + attrValue + "\" as an integer in the attribute \"" + attributeName + "\" on the element \"" + element.getTagName() + "\" at " + getLocation(element));
+ throw new Exception("Cannot parse the value \"" + attrValue +
+ "\" as an integer in the attribute \"" + attributeName +
+ "\" on the element \"" + element.getTagName() +
+ "\" at " + getLocation(element));
}
}
- public static int getAttributeAsInteger(Element element, String attributeName, int defaultValue) throws Exception {
+ public static int getAttributeAsInteger(Element element,
+ String attributeName, int defaultValue) throws Exception {
String attrValue = element.getAttribute(attributeName);
if (attrValue.equals("")) {
return defaultValue;
@@ -237,21 +266,31 @@
try {
return Integer.parseInt(attrValue);
} catch (NumberFormatException e) {
- throw new Exception("Cannot parse the value \"" + attrValue + "\" as an integer in the attribute \"" + attributeName + "\" on the element \"" + element.getTagName() + "\" at " + getLocation(element));
+ throw new Exception("Cannot parse the value \"" + attrValue +
+ "\" as an integer in the attribute \"" +
+ attributeName + "\" on the element \"" +
+ element.getTagName() + "\" at " +
+ getLocation(element));
}
}
}
- public static boolean getAttributeAsBoolean(Element element, String attributeName, boolean defaultValue) throws Exception {
+ public static boolean getAttributeAsBoolean(Element element,
+ String attributeName, boolean defaultValue) throws Exception {
String attrValue = element.getAttribute(attributeName);
if (attrValue.equals("")) {
return defaultValue;
- } else if (attrValue.equalsIgnoreCase("true") || attrValue.equalsIgnoreCase("yes")) {
+ } else if (attrValue.equalsIgnoreCase("true")
+ || attrValue.equalsIgnoreCase("yes")) {
return true;
- } else if (attrValue.equalsIgnoreCase("false") || attrValue.equalsIgnoreCase("no")) {
+ } else if (attrValue.equalsIgnoreCase("false")
+ || attrValue.equalsIgnoreCase("no")) {
return false;
} else {
- throw new Exception("Cannot parse the value \"" + attrValue + "\" as a boolean in the attribute \"" + attributeName + "\" on the element \"" + element.getTagName() + "\" at " + getLocation(element));
+ throw new Exception("Cannot parse the value \"" + attrValue +
+ "\" as a boolean in the attribute \"" + attributeName +
+ "\" on the element \"" + element.getTagName() +
+ "\" at " + getLocation(element));
}
}
@@ -268,10 +307,11 @@
}
/**
- * Returns the content of the given Element as an object implementing the XMLizable
- * interface. Practically speaking, the implementation uses the {@link SaxBuffer} class.
- * The XMLizable object will be a standalone blurb of SAX events, not producing
- * start/endDocument calls and containing all necessary namespace declarations.
+ * Returns the content of the given Element as an object implementing the
+ * XMLizable interface. Practically speaking, the implementation uses the
+ * {@link SaxBuffer} class. The XMLizable object will be a standalone blurb
+ * of SAX events, not producing start/endDocument calls and containing all
+ * necessary namespace declarations.
*/
public static XMLizable compileElementContent(Element element) {
SaxBuffer saxBuffer = new SaxBuffer();
@@ -283,58 +323,78 @@
try {
domStreamer.stream(childNodes.item(i));
} catch (SAXException e) {
- // It's unlikely that an exception will occur here, so use a runtime exception
- throw new RuntimeException("Error in DomHelper.compileElementContent: " + e.toString());
+ // It's unlikely that an exception will occur here,
+ // so use a runtime exception
+ throw new RuntimeException(
+ "Error in DomHelper.compileElementContent: " +
+ e.toString());
}
}
return saxBuffer;
}
/**
- * Creates a W3C Document that remembers the location of each element in the source file.
- * The location of element nodes can then be retrieved using the {@link #getLocation(Element)} method.
+ * Creates a W3C Document that remembers the location of each element in
+ * the source file. The location of element nodes can then be retrieved
+ * using the {@link #getLocation(Element)} method.
*/
- public static Document parse(InputSource inputSource) throws SAXException, SAXNotSupportedException, IOException {
+ public static Document parse(InputSource inputSource)
+ throws SAXException, SAXNotSupportedException, IOException {
DOMParser domParser = new LocationTrackingDOMParser();
- domParser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
- domParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes", false);
+ domParser.setFeature(
+ "http://apache.org/xml/features/dom/defer-node-expansion",
+ false);
+ domParser.setFeature(
+ "http://apache.org/xml/features/dom/create-entity-ref-nodes",
+ false);
domParser.parse(inputSource);
return domParser.getDocument();
}
/**
- * An extension of the Xerces DOM parser that puts the location of each node in that
- * node's UserData.
+ * An extension of the Xerces DOM parser that puts the location of each
+ * node in that node's UserData.
*/
public static class LocationTrackingDOMParser extends DOMParser {
XMLLocator locator;
- public void startDocument(XMLLocator xmlLocator, String s, NamespaceContext namespaceContext, Augmentations augmentations) throws XNIException {
- super.startDocument(xmlLocator, s, namespaceContext, augmentations);
+ public void startDocument(XMLLocator xmlLocator, String s,
+ NamespaceContext namespaceContext,
+ Augmentations augmentations) throws XNIException {
+ super.startDocument(xmlLocator, s, namespaceContext,
+ augmentations);
this.locator = xmlLocator;
setLocation();
}
- public void startElement(QName qName, XMLAttributes xmlAttributes, Augmentations augmentations) throws XNIException {
+ public void startElement(QName qName, XMLAttributes xmlAttributes,
+ Augmentations augmentations) throws XNIException {
super.startElement(qName, xmlAttributes, augmentations);
setLocation();
}
private final void setLocation() {
- // Older versions of Xerces had a different signature for the startDocument method. If such a
- // version is used, the startDocument method above will not be called and locator will hence be null.
+ // Older versions of Xerces had a different signature for the
+ // startDocument method. If such a version is used, the
+ // startDocument method above will not be called and locator will
+ // hence be null.
// Tell the users this so that they don't get a stupid NPE.
- if (this.locator == null)
- throw new RuntimeException("Error: locator is null. Check that you have the correct version of Xerces (such as the one that comes with Cocoon) in your endorsed library path.");
-
+ if (this.locator == null) {
+ throw new RuntimeException(
+ "Error: locator is null. Check that you have the" +
+ " correct version of Xerces (such as the one that" +
+ " comes with Cocoon) in your endorsed library path.");
+ }
NodeImpl node = null;
try {
- node = (NodeImpl) this.getProperty("http://apache.org/xml/properties/dom/current-element-node");
+ node = (NodeImpl)this.getProperty(
+ "http://apache.org/xml/properties/dom/current-element-node");
} catch (org.xml.sax.SAXException ex) {
System.err.println("except" + ex);
}
if (node != null) {
- String location = locator.getLiteralSystemId() + ":" + locator.getLineNumber() + ":" + locator.getColumnNumber();
+ String location = locator.getLiteralSystemId() + ":" +
+ locator.getLineNumber() + ":" + locator.getColumnNumber();
node.setUserData("location", location, null);
}
}