You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by ek...@apache.org on 2005/08/12 17:23:08 UTC
svn commit: r232310 [88/92] - in /beehive/trunk/controls/test: common/
infra/gtlf/ infra/gtlf/xsl/ infra/mantis/ infra/tch/ infra/tch/messages/
infra/tch/runtime/ infra/tch/schema/ perf/ perf/bin/ perf/cases/
perf/ctlsrc/org/apache/beehive/controls/per...
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DomUtils.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DomUtils.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DomUtils.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DomUtils.java Fri Aug 12 08:12:28 2005
@@ -1,637 +1,637 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
-import org.xml.sax.ErrorHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-
-import org.apache.beehive.test.tools.tch.util.TchUtils;
-import org.apache.beehive.test.tools.tch.util.gtlf.GTLFEntityResolver;
-
-/**
- * Useful for working with Doms
- *
- * Provides helper methods for:
- * - instantiating a Dom Document Object.
- * - packaging attributes into a Map.
- * - writing out a Dom tree.
- *
- * GTLFEntityResolver and GenericErrorHandler are defined at the
- * end of this file.
- *
- * @version 1.0, November 17th, 2001
- */
-public class DomUtils
-{
-
- private static final String sep = System.getProperty("line.separator");
- private static final int MAX_ATTS_ON_SAME_LINE_DEFAULT = 2;
- private static final boolean DEFAULT_ESCAPE = true;
-
- /**
- * Creates an empty Dom.
- */
- public static Document instantiateDom() throws ParserConfigurationException
- {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setValidating(false);
- dbf.setNamespaceAware(false);
- dbf.setIgnoringElementContentWhitespace(true);
- dbf.setCoalescing(true);
- return dbf.newDocumentBuilder().newDocument();
- }
-
- // following constructors create Doms from various sources.
-
- public static Document instantiateDom(InputSource is, boolean validate)
- throws NestedXMLProcessingException
- {
- try
- {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setValidating(validate);
- factory.setNamespaceAware(false);
- factory.setIgnoringElementContentWhitespace(true);
- factory.setCoalescing(true);
- DocumentBuilder parser = factory.newDocumentBuilder();
- parser.setErrorHandler(new GenericErrorHandler());
- parser.setEntityResolver(new GTLFEntityResolver());
- return parser.parse(is);
- }
- catch (IOException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- catch (ParserConfigurationException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- catch (SAXException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- }
-
- public static Document instantiateDom(String xml, boolean validate)
- throws NestedXMLProcessingException
- {
- return instantiateDom(new InputSource(new StringReader(xml)), validate);
- }
-
- public static Document instantiateDom(File f, boolean validate)
- throws NestedXMLProcessingException
- {
- try
- {
- return instantiateDom(new InputSource(new FileInputStream(f)), validate);
- }
- catch (FileNotFoundException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- }
-
- public static Document instantiateDom(InputStream is, boolean validate)
- throws NestedXMLProcessingException
- {
- return instantiateDom(new InputSource(is), validate);
- }
-
- public static Document instantiateDom(File f)
- throws NestedXMLProcessingException
- {
- return instantiateDom(f, false);
- }
-
- public static Document instantiateDom(String xml)
- throws NestedXMLProcessingException
- {
- return instantiateDom(xml, false);
- }
-
- public static Document instantiateDom(InputSource is)
- throws NestedXMLProcessingException
- {
- return instantiateDom(is, false);
- }
-
- public static Document instantiateDom(InputStream is)
- throws NestedXMLProcessingException
- {
- return instantiateDom(is, false);
- }
-
- public static Map attributesToMap(Node n)
- {
- Map mapAttributes = new HashMap();
- NamedNodeMap nodeAttributes = n.getAttributes();
- if (nodeAttributes != null)
- {
- for (int i = 0, iL = nodeAttributes.getLength(); i < iL; i++)
- {
- Node current = nodeAttributes.item(i);
- mapAttributes.put(
- current.getNodeName().trim(),
- current.getNodeValue().trim());
- }
- }
- return mapAttributes;
- }
-
- // walks Dom starting at node, throws all attributes in Map
- public static void treeAttributesToMap(Node n, Map attributes)
- {
- attributes.putAll(attributesToMap(n));
- NodeList children = n.getChildNodes();
- for (int i = 0, iL = children.getLength(); i < iL; i++)
- {
- treeAttributesToMap(children.item(i), attributes);
- }
- }
-
- public static List NodeListToList(NodeList in)
- {
- List l = new ArrayList();
- for (int i = 0; i < in.getLength(); i++)
- {
- Node n = in.item(i);
- l.add(n);
- }
- return l;
- }
-
- /**
- * Given a collection of Node Objects, returns the node names, as a
- * Collection of Strings
- *
- * @return Collection
- */
- public static Collection nodeNameToString(Collection nodes)
- {
- Collection rtn = new ArrayList();
- for (Iterator iter = nodes.iterator(); iter.hasNext();)
- {
- Node n = (Node)iter.next();
- rtn.add(n.getNodeName());
- }
- return rtn;
- }
-
- // easy printing of a collection of nodes
- public static String NodeCollectionToString(NodeList nodes)
- {
- return NodeCollectionToString(NodeListToList(nodes));
- }
-
- //easy printing of a collection of nodes
- public static String NodeCollectionToString(Collection nodes)
- {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- pw.print("[");
- for (Iterator iter = nodes.iterator(); iter.hasNext();)
- {
- Node n = (Node)iter.next();
- writeDom(n, pw);
- if (iter.hasNext())
- {
- pw.print(",");
- }
- }
- pw.print("]");
- pw.flush();
- return sw.getBuffer().toString();
- }
-
- public static Collection getNameAttributeValue(Collection nodes)
- {
- Collection rtn = new ArrayList();
- for (Iterator iter = nodes.iterator(); iter.hasNext();)
- {
- Node n = (Node)iter.next();
- rtn.add(getNameAttributeValue(n));
- }
- return rtn;
- }
-
- public static String getNameAttributeValue(Node n)
- {
- return getAttributeValue(n, "name");
- }
-
- public static String getAttributeValue(Node n, String attributeName)
- {
- return getAttributeValue(n, attributeName, null);
- }
-
- public static String getAttributeValue(
- Node n,
- String attributeName,
- String defaultValue)
- {
- NamedNodeMap attributes = n.getAttributes();
- if (attributes == null)
- return defaultValue;
- Node attribute = attributes.getNamedItem(attributeName);
- if (attribute != null)
- return attribute.getNodeValue().trim();
- return defaultValue;
- }
-
- public static void addChildren(Node dest, Node src)
- {
- Collection c = null;
- addChildren(dest, src, c);
- }
-
- public static void addChildren(Node dest, Node src, String attrName)
- {
- Collection c = new ArrayList(1);
- c.add(attrName);
- addChildren(dest, src, c);
- }
-
- /**
- * @param dest
- * @param src
- * @param overrideChildrenWithAttrs Children with matching attributes (name, value) will be overridden
- */
- public static void addChildren(Node dest, Node src, Collection attrs)
- {
- NodeList children = src.getChildNodes();
- int numChildren = children.getLength();
- for (int i = 0; i < numChildren; i++)
- {
- Node childNode = children.item(i);
- Collection nodesToReplace = new HashSet();
- if (attrs != null)
- {
- for (Iterator iter = attrs.iterator(); iter.hasNext();)
- {
- String attrName = (String)iter.next();
- String attrValue = getAttributeValue(childNode, attrName);
- nodesToReplace.addAll(
- getChildNodesWithSameAttribute(dest, attrName, attrValue));
- }
- }
- Node childNodeClone = childNode.cloneNode(true);
- if (!nodesToReplace.isEmpty())
- {
- for (Iterator iter = nodesToReplace.iterator(); iter.hasNext();)
- {
- Node replace = (Node)iter.next();
- dest.replaceChild(childNodeClone, replace);
- }
-
- }
- else
- {
- dest.appendChild(childNodeClone);
- }
- }
- }
-
- private static Collection getChildNodesWithSameAttribute(
- Node node,
- String attrName,
- String attrValue)
- {
- Collection ret = new ArrayList();
- NodeList children = node.getChildNodes();
- int length = children.getLength();
- for (int i = 0; i < length; i++)
- {
- Node child = children.item(i);
- String v = getAttributeValue(child, attrName);
- if (v != null && v.equals(attrValue))
- {
- ret.add(child);
- }
- }
- return ret;
- }
-
- public static boolean hasSameAttributes(Element e1, Element e2)
- {
- if (e1.getAttributes().getLength() != e2.getAttributes().getLength())
- {
- return false;
- }
- NamedNodeMap attributes = e1.getAttributes();
- for (int i = attributes.getLength() - 1; i >= 0; i--)
- {
- Attr attr = (Attr)attributes.item(i);
- String value2 = e2.getAttribute(attr.getName());
- if (value2 == null)
- {
- return false;
- }
- else if (!attr.getValue().equals(value2))
- {
- return false;
- }
- }
- return true;
- }
-
- public static void addAttributes(Node dest, Node src)
- {
- addAttributes(dest, src, new ArrayList());
- }
-
- public static void addAttributes(
- Node dest,
- Node src,
- Collection excludedAttrNames)
- {
- NamedNodeMap attributes = src.getAttributes();
- int numAttr = attributes.getLength();
- for (int i = 0; i < numAttr; i++)
- {
- Attr attr = (Attr)attributes.item(i);
- if (!excludedAttrNames.contains(attr.getName().trim()))
- {
- addAttribute(dest, attr);
- }
- }
- }
-
- public static void addAttribute(Node n, Attr attr)
- {
- NamedNodeMap attributes = n.getAttributes();
- if (attributes.getNamedItem(attr.getName()) != null)
- {
- attributes.getNamedItem(attr.getName()).setNodeValue(attr.getValue());
- }
- else
- {
- Node newAttr = n.getOwnerDocument().importNode(attr, false);
- attributes.setNamedItem(newAttr);
- }
- }
-
- public static void findReplace(Node n, String find, String replace)
- {
- visitDom(n, new FindReplaceNodeVisitor(find, replace));
- }
-
- public static String toString(Node n)
- {
- if (n == null)
- return "null";
- StringBuffer sb = new StringBuffer();
- writeDom(n, sb);
- return sb.toString();
- }
-
- // write to System.out
- public static void writeDom(Node node)
- {
- PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
- writeDom(node, pw);
- pw.flush();
- }
-
- // write to StringBuffer
- public static void writeDom(Node node, StringBuffer sb)
- {
- writeDom(node, sb, MAX_ATTS_ON_SAME_LINE_DEFAULT);
- }
-
- public static void writeDom(Node node, StringBuffer sb, int maxAttrsOnLine)
- {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- writeDom(node, pw, maxAttrsOnLine);
- pw.flush();
- StringBuffer buffer = sw.getBuffer();
- sb.append(buffer);
- }
-
- public static void writeDom(Node node, File f)
- throws NestedXMLProcessingException
- {
- writeDom(node, f, MAX_ATTS_ON_SAME_LINE_DEFAULT);
- }
-
- // write to File
- public static void writeDom(Node node, File f, int maxAttrsOnLine)
- throws NestedXMLProcessingException
- {
- try
- {
- PrintWriter pw =
- new PrintWriter(new BufferedOutputStream(new FileOutputStream((f))));
- writeDom(node, pw, maxAttrsOnLine);
- pw.close();
- }
- catch (FileNotFoundException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- }
-
- // write to pw
- public static void writeDom(Node node, PrintWriter pw)
- {
- writeDom(node, pw, MAX_ATTS_ON_SAME_LINE_DEFAULT);
- pw.flush();
- }
-
- public static void writeDom(Node node, PrintWriter pw, boolean escape)
- {
- writeDom(node, pw, MAX_ATTS_ON_SAME_LINE_DEFAULT, escape);
- pw.flush();
- }
-
- // read xml from String, write to file (to 'beautify').
- public static void writeDom(String xml, File f)
- throws NestedXMLProcessingException
- {
- Document root = instantiateDom(xml);
- try
- {
- PrintWriter pw =
- new PrintWriter(new BufferedOutputStream(new FileOutputStream((f))));
- writeDom(root.getDocumentElement(), pw);
- pw.close();
- }
- catch (FileNotFoundException ex)
- {
- throw new NestedXMLProcessingException(ex);
- }
- }
-
- public static void writeDom(Node node, PrintWriter pw, int maxAttrsOnOneLine)
- {
- visitDom(
- node,
- new NodeVisitorDOMWriter(pw, maxAttrsOnOneLine, DEFAULT_ESCAPE));
- pw.flush();
- }
-
- public static void writeDom(
- Node node,
- PrintWriter pw,
- int maxAttrsOnOneLine,
- boolean escape)
- {
- visitDom(node, new NodeVisitorDOMWriter(pw, maxAttrsOnOneLine, escape));
- pw.flush();
- }
-
- public static void visitDom(Node node, NodeVisitor visitor)
- {
- switch (node.getNodeType())
- {
- case Node.DOCUMENT_NODE :
- Document doc = (Document)node;
- visitor.start(doc);
- visitDom(doc.getDocumentElement(), visitor);
- visitor.end(doc);
- break;
-
- case Node.ELEMENT_NODE :
- visitor.start((Element)node);
- NamedNodeMap attributes = node.getAttributes();
- int attributesLength = attributes.getLength();
- for (int i = 0; i < attributesLength; i++)
- {
- visitor.visit((Attr)attributes.item(i));
- }
- visitor.endAttributes((Element)node);
- NodeList children = node.getChildNodes();
- if (children != null)
- {
- int childrenLength = children.getLength();
- for (int i = 0; i < childrenLength; i++)
- {
- visitDom(children.item(i), visitor);
- }
- }
- visitor.end((Element)node);
- break;
-
- case Node.COMMENT_NODE :
- visitor.visit((Comment)node);
- break;
- case Node.CDATA_SECTION_NODE :
- visitor.visit((CDATASection)node);
- break;
- case Node.TEXT_NODE :
- visitor.visit((Text)node);
- break;
- case Node.PROCESSING_INSTRUCTION_NODE :
- case Node.ENTITY_REFERENCE_NODE :
- case Node.DOCUMENT_TYPE_NODE :
- break;
- }
- }
-
- private static class GenericErrorHandler implements ErrorHandler
- {
- public void warning(SAXParseException e) throws SAXParseException
- {
- throw e;
- }
- public void error(SAXParseException e) throws SAXParseException
- {
- throw e;
- }
- public void fatalError(SAXParseException e) throws SAXParseException
- {
- throw e;
- }
- }
-
- private static class FindReplaceNodeVisitor implements NodeVisitor
- {
- private String find = null;
- private String replace = null;
-
- public FindReplaceNodeVisitor(String inFind, String inReplace)
- {
- find = inFind;
- replace = inReplace;
- }
-
- public void start(Element element)
- {
- handleNode(element);
- }
-
- public void endAttributes(Element element)
- {
- }
-
- public void end(Element element)
- {
- }
-
- public void visit(Attr attribute)
- {
- handleNode(attribute);
- }
-
- public void start(Document document)
- {
- handleNode(document);
- }
-
- public void end(Document document)
- {
- }
-
- public void visit(Comment comment)
- {
- handleNode(comment);
- }
-
- public void visit(CDATASection cdata)
- {
- handleNode(cdata);
- }
-
- public void visit(Text text)
- {
- handleNode(text);
- }
-
- private void handleNode(Node n)
- {
- n.setNodeValue(
- TchUtils.searchAndReplace(n.getNodeValue(), find, replace));
- }
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import org.apache.beehive.test.tools.tch.util.TchUtils;
+import org.apache.beehive.test.tools.tch.util.gtlf.GTLFEntityResolver;
+
+/**
+ * Useful for working with Doms
+ *
+ * Provides helper methods for:
+ * - instantiating a Dom Document Object.
+ * - packaging attributes into a Map.
+ * - writing out a Dom tree.
+ *
+ * GTLFEntityResolver and GenericErrorHandler are defined at the
+ * end of this file.
+ *
+ * @version 1.0, November 17th, 2001
+ */
+public class DomUtils
+{
+
+ private static final String sep = System.getProperty("line.separator");
+ private static final int MAX_ATTS_ON_SAME_LINE_DEFAULT = 2;
+ private static final boolean DEFAULT_ESCAPE = true;
+
+ /**
+ * Creates an empty Dom.
+ */
+ public static Document instantiateDom() throws ParserConfigurationException
+ {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setValidating(false);
+ dbf.setNamespaceAware(false);
+ dbf.setIgnoringElementContentWhitespace(true);
+ dbf.setCoalescing(true);
+ return dbf.newDocumentBuilder().newDocument();
+ }
+
+ // following constructors create Doms from various sources.
+
+ public static Document instantiateDom(InputSource is, boolean validate)
+ throws NestedXMLProcessingException
+ {
+ try
+ {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setValidating(validate);
+ factory.setNamespaceAware(false);
+ factory.setIgnoringElementContentWhitespace(true);
+ factory.setCoalescing(true);
+ DocumentBuilder parser = factory.newDocumentBuilder();
+ parser.setErrorHandler(new GenericErrorHandler());
+ parser.setEntityResolver(new GTLFEntityResolver());
+ return parser.parse(is);
+ }
+ catch (IOException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ catch (ParserConfigurationException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ catch (SAXException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ }
+
+ public static Document instantiateDom(String xml, boolean validate)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(new InputSource(new StringReader(xml)), validate);
+ }
+
+ public static Document instantiateDom(File f, boolean validate)
+ throws NestedXMLProcessingException
+ {
+ try
+ {
+ return instantiateDom(new InputSource(new FileInputStream(f)), validate);
+ }
+ catch (FileNotFoundException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ }
+
+ public static Document instantiateDom(InputStream is, boolean validate)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(new InputSource(is), validate);
+ }
+
+ public static Document instantiateDom(File f)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(f, false);
+ }
+
+ public static Document instantiateDom(String xml)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(xml, false);
+ }
+
+ public static Document instantiateDom(InputSource is)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(is, false);
+ }
+
+ public static Document instantiateDom(InputStream is)
+ throws NestedXMLProcessingException
+ {
+ return instantiateDom(is, false);
+ }
+
+ public static Map attributesToMap(Node n)
+ {
+ Map mapAttributes = new HashMap();
+ NamedNodeMap nodeAttributes = n.getAttributes();
+ if (nodeAttributes != null)
+ {
+ for (int i = 0, iL = nodeAttributes.getLength(); i < iL; i++)
+ {
+ Node current = nodeAttributes.item(i);
+ mapAttributes.put(
+ current.getNodeName().trim(),
+ current.getNodeValue().trim());
+ }
+ }
+ return mapAttributes;
+ }
+
+ // walks Dom starting at node, throws all attributes in Map
+ public static void treeAttributesToMap(Node n, Map attributes)
+ {
+ attributes.putAll(attributesToMap(n));
+ NodeList children = n.getChildNodes();
+ for (int i = 0, iL = children.getLength(); i < iL; i++)
+ {
+ treeAttributesToMap(children.item(i), attributes);
+ }
+ }
+
+ public static List NodeListToList(NodeList in)
+ {
+ List l = new ArrayList();
+ for (int i = 0; i < in.getLength(); i++)
+ {
+ Node n = in.item(i);
+ l.add(n);
+ }
+ return l;
+ }
+
+ /**
+ * Given a collection of Node Objects, returns the node names, as a
+ * Collection of Strings
+ *
+ * @return Collection
+ */
+ public static Collection nodeNameToString(Collection nodes)
+ {
+ Collection rtn = new ArrayList();
+ for (Iterator iter = nodes.iterator(); iter.hasNext();)
+ {
+ Node n = (Node)iter.next();
+ rtn.add(n.getNodeName());
+ }
+ return rtn;
+ }
+
+ // easy printing of a collection of nodes
+ public static String NodeCollectionToString(NodeList nodes)
+ {
+ return NodeCollectionToString(NodeListToList(nodes));
+ }
+
+ //easy printing of a collection of nodes
+ public static String NodeCollectionToString(Collection nodes)
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ pw.print("[");
+ for (Iterator iter = nodes.iterator(); iter.hasNext();)
+ {
+ Node n = (Node)iter.next();
+ writeDom(n, pw);
+ if (iter.hasNext())
+ {
+ pw.print(",");
+ }
+ }
+ pw.print("]");
+ pw.flush();
+ return sw.getBuffer().toString();
+ }
+
+ public static Collection getNameAttributeValue(Collection nodes)
+ {
+ Collection rtn = new ArrayList();
+ for (Iterator iter = nodes.iterator(); iter.hasNext();)
+ {
+ Node n = (Node)iter.next();
+ rtn.add(getNameAttributeValue(n));
+ }
+ return rtn;
+ }
+
+ public static String getNameAttributeValue(Node n)
+ {
+ return getAttributeValue(n, "name");
+ }
+
+ public static String getAttributeValue(Node n, String attributeName)
+ {
+ return getAttributeValue(n, attributeName, null);
+ }
+
+ public static String getAttributeValue(
+ Node n,
+ String attributeName,
+ String defaultValue)
+ {
+ NamedNodeMap attributes = n.getAttributes();
+ if (attributes == null)
+ return defaultValue;
+ Node attribute = attributes.getNamedItem(attributeName);
+ if (attribute != null)
+ return attribute.getNodeValue().trim();
+ return defaultValue;
+ }
+
+ public static void addChildren(Node dest, Node src)
+ {
+ Collection c = null;
+ addChildren(dest, src, c);
+ }
+
+ public static void addChildren(Node dest, Node src, String attrName)
+ {
+ Collection c = new ArrayList(1);
+ c.add(attrName);
+ addChildren(dest, src, c);
+ }
+
+ /**
+ * @param dest
+ * @param src
+ * @param overrideChildrenWithAttrs Children with matching attributes (name, value) will be overridden
+ */
+ public static void addChildren(Node dest, Node src, Collection attrs)
+ {
+ NodeList children = src.getChildNodes();
+ int numChildren = children.getLength();
+ for (int i = 0; i < numChildren; i++)
+ {
+ Node childNode = children.item(i);
+ Collection nodesToReplace = new HashSet();
+ if (attrs != null)
+ {
+ for (Iterator iter = attrs.iterator(); iter.hasNext();)
+ {
+ String attrName = (String)iter.next();
+ String attrValue = getAttributeValue(childNode, attrName);
+ nodesToReplace.addAll(
+ getChildNodesWithSameAttribute(dest, attrName, attrValue));
+ }
+ }
+ Node childNodeClone = childNode.cloneNode(true);
+ if (!nodesToReplace.isEmpty())
+ {
+ for (Iterator iter = nodesToReplace.iterator(); iter.hasNext();)
+ {
+ Node replace = (Node)iter.next();
+ dest.replaceChild(childNodeClone, replace);
+ }
+
+ }
+ else
+ {
+ dest.appendChild(childNodeClone);
+ }
+ }
+ }
+
+ private static Collection getChildNodesWithSameAttribute(
+ Node node,
+ String attrName,
+ String attrValue)
+ {
+ Collection ret = new ArrayList();
+ NodeList children = node.getChildNodes();
+ int length = children.getLength();
+ for (int i = 0; i < length; i++)
+ {
+ Node child = children.item(i);
+ String v = getAttributeValue(child, attrName);
+ if (v != null && v.equals(attrValue))
+ {
+ ret.add(child);
+ }
+ }
+ return ret;
+ }
+
+ public static boolean hasSameAttributes(Element e1, Element e2)
+ {
+ if (e1.getAttributes().getLength() != e2.getAttributes().getLength())
+ {
+ return false;
+ }
+ NamedNodeMap attributes = e1.getAttributes();
+ for (int i = attributes.getLength() - 1; i >= 0; i--)
+ {
+ Attr attr = (Attr)attributes.item(i);
+ String value2 = e2.getAttribute(attr.getName());
+ if (value2 == null)
+ {
+ return false;
+ }
+ else if (!attr.getValue().equals(value2))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static void addAttributes(Node dest, Node src)
+ {
+ addAttributes(dest, src, new ArrayList());
+ }
+
+ public static void addAttributes(
+ Node dest,
+ Node src,
+ Collection excludedAttrNames)
+ {
+ NamedNodeMap attributes = src.getAttributes();
+ int numAttr = attributes.getLength();
+ for (int i = 0; i < numAttr; i++)
+ {
+ Attr attr = (Attr)attributes.item(i);
+ if (!excludedAttrNames.contains(attr.getName().trim()))
+ {
+ addAttribute(dest, attr);
+ }
+ }
+ }
+
+ public static void addAttribute(Node n, Attr attr)
+ {
+ NamedNodeMap attributes = n.getAttributes();
+ if (attributes.getNamedItem(attr.getName()) != null)
+ {
+ attributes.getNamedItem(attr.getName()).setNodeValue(attr.getValue());
+ }
+ else
+ {
+ Node newAttr = n.getOwnerDocument().importNode(attr, false);
+ attributes.setNamedItem(newAttr);
+ }
+ }
+
+ public static void findReplace(Node n, String find, String replace)
+ {
+ visitDom(n, new FindReplaceNodeVisitor(find, replace));
+ }
+
+ public static String toString(Node n)
+ {
+ if (n == null)
+ return "null";
+ StringBuffer sb = new StringBuffer();
+ writeDom(n, sb);
+ return sb.toString();
+ }
+
+ // write to System.out
+ public static void writeDom(Node node)
+ {
+ PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
+ writeDom(node, pw);
+ pw.flush();
+ }
+
+ // write to StringBuffer
+ public static void writeDom(Node node, StringBuffer sb)
+ {
+ writeDom(node, sb, MAX_ATTS_ON_SAME_LINE_DEFAULT);
+ }
+
+ public static void writeDom(Node node, StringBuffer sb, int maxAttrsOnLine)
+ {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ writeDom(node, pw, maxAttrsOnLine);
+ pw.flush();
+ StringBuffer buffer = sw.getBuffer();
+ sb.append(buffer);
+ }
+
+ public static void writeDom(Node node, File f)
+ throws NestedXMLProcessingException
+ {
+ writeDom(node, f, MAX_ATTS_ON_SAME_LINE_DEFAULT);
+ }
+
+ // write to File
+ public static void writeDom(Node node, File f, int maxAttrsOnLine)
+ throws NestedXMLProcessingException
+ {
+ try
+ {
+ PrintWriter pw =
+ new PrintWriter(new BufferedOutputStream(new FileOutputStream((f))));
+ writeDom(node, pw, maxAttrsOnLine);
+ pw.close();
+ }
+ catch (FileNotFoundException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ }
+
+ // write to pw
+ public static void writeDom(Node node, PrintWriter pw)
+ {
+ writeDom(node, pw, MAX_ATTS_ON_SAME_LINE_DEFAULT);
+ pw.flush();
+ }
+
+ public static void writeDom(Node node, PrintWriter pw, boolean escape)
+ {
+ writeDom(node, pw, MAX_ATTS_ON_SAME_LINE_DEFAULT, escape);
+ pw.flush();
+ }
+
+ // read xml from String, write to file (to 'beautify').
+ public static void writeDom(String xml, File f)
+ throws NestedXMLProcessingException
+ {
+ Document root = instantiateDom(xml);
+ try
+ {
+ PrintWriter pw =
+ new PrintWriter(new BufferedOutputStream(new FileOutputStream((f))));
+ writeDom(root.getDocumentElement(), pw);
+ pw.close();
+ }
+ catch (FileNotFoundException ex)
+ {
+ throw new NestedXMLProcessingException(ex);
+ }
+ }
+
+ public static void writeDom(Node node, PrintWriter pw, int maxAttrsOnOneLine)
+ {
+ visitDom(
+ node,
+ new NodeVisitorDOMWriter(pw, maxAttrsOnOneLine, DEFAULT_ESCAPE));
+ pw.flush();
+ }
+
+ public static void writeDom(
+ Node node,
+ PrintWriter pw,
+ int maxAttrsOnOneLine,
+ boolean escape)
+ {
+ visitDom(node, new NodeVisitorDOMWriter(pw, maxAttrsOnOneLine, escape));
+ pw.flush();
+ }
+
+ public static void visitDom(Node node, NodeVisitor visitor)
+ {
+ switch (node.getNodeType())
+ {
+ case Node.DOCUMENT_NODE :
+ Document doc = (Document)node;
+ visitor.start(doc);
+ visitDom(doc.getDocumentElement(), visitor);
+ visitor.end(doc);
+ break;
+
+ case Node.ELEMENT_NODE :
+ visitor.start((Element)node);
+ NamedNodeMap attributes = node.getAttributes();
+ int attributesLength = attributes.getLength();
+ for (int i = 0; i < attributesLength; i++)
+ {
+ visitor.visit((Attr)attributes.item(i));
+ }
+ visitor.endAttributes((Element)node);
+ NodeList children = node.getChildNodes();
+ if (children != null)
+ {
+ int childrenLength = children.getLength();
+ for (int i = 0; i < childrenLength; i++)
+ {
+ visitDom(children.item(i), visitor);
+ }
+ }
+ visitor.end((Element)node);
+ break;
+
+ case Node.COMMENT_NODE :
+ visitor.visit((Comment)node);
+ break;
+ case Node.CDATA_SECTION_NODE :
+ visitor.visit((CDATASection)node);
+ break;
+ case Node.TEXT_NODE :
+ visitor.visit((Text)node);
+ break;
+ case Node.PROCESSING_INSTRUCTION_NODE :
+ case Node.ENTITY_REFERENCE_NODE :
+ case Node.DOCUMENT_TYPE_NODE :
+ break;
+ }
+ }
+
+ private static class GenericErrorHandler implements ErrorHandler
+ {
+ public void warning(SAXParseException e) throws SAXParseException
+ {
+ throw e;
+ }
+ public void error(SAXParseException e) throws SAXParseException
+ {
+ throw e;
+ }
+ public void fatalError(SAXParseException e) throws SAXParseException
+ {
+ throw e;
+ }
+ }
+
+ private static class FindReplaceNodeVisitor implements NodeVisitor
+ {
+ private String find = null;
+ private String replace = null;
+
+ public FindReplaceNodeVisitor(String inFind, String inReplace)
+ {
+ find = inFind;
+ replace = inReplace;
+ }
+
+ public void start(Element element)
+ {
+ handleNode(element);
+ }
+
+ public void endAttributes(Element element)
+ {
+ }
+
+ public void end(Element element)
+ {
+ }
+
+ public void visit(Attr attribute)
+ {
+ handleNode(attribute);
+ }
+
+ public void start(Document document)
+ {
+ handleNode(document);
+ }
+
+ public void end(Document document)
+ {
+ }
+
+ public void visit(Comment comment)
+ {
+ handleNode(comment);
+ }
+
+ public void visit(CDATASection cdata)
+ {
+ handleNode(cdata);
+ }
+
+ public void visit(Text text)
+ {
+ handleNode(text);
+ }
+
+ private void handleNode(Node n)
+ {
+ n.setNodeValue(
+ TchUtils.searchAndReplace(n.getNodeValue(), find, replace));
+ }
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DomUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DynamicPropNodeVisitor.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DynamicPropNodeVisitor.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DynamicPropNodeVisitor.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DynamicPropNodeVisitor.java Fri Aug 12 08:12:28 2005
@@ -1,82 +1,82 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.Text;
-
-import org.apache.beehive.test.tools.tch.util.ValueHandler;
-
-/**
- */
-public class DynamicPropNodeVisitor implements NodeVisitor
-{
- private ValueHandler valueHandler = null;
-
- public DynamicPropNodeVisitor(ValueHandler in)
- {
- valueHandler = in;
- }
-
- public void start(Element element)
- {
- resolveNodeValue(element);
- }
-
- public void endAttributes(Element element)
- {
- // already handled in start
- }
-
- public void end(Element element)
- {
- // already handled in start
- }
-
- public void visit(Attr attribute)
- {
- resolveNodeValue(attribute);
- }
-
- public void start(Document document)
- {
- resolveNodeValue(document);
- }
-
- public void end(Document doccument)
- {
-
- }
-
- public void visit(Comment comment)
- {
- resolveNodeValue(comment);
- }
-
- public void visit(CDATASection cdata)
- {
- resolveNodeValue(cdata);
- }
-
- public void visit(Text text)
- {
- resolveNodeValue(text);
- }
-
- private void resolveNodeValue(Node n)
- {
- String value = n.getNodeValue();
- if (value != null)
- {
- value = value.trim();
- if (value.length() > 0)
- {
- String resolvedValue = valueHandler.handleValue(value);
- n.setNodeValue(resolvedValue);
- }
- }
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.Text;
+
+import org.apache.beehive.test.tools.tch.util.ValueHandler;
+
+/**
+ */
+public class DynamicPropNodeVisitor implements NodeVisitor
+{
+ private ValueHandler valueHandler = null;
+
+ public DynamicPropNodeVisitor(ValueHandler in)
+ {
+ valueHandler = in;
+ }
+
+ public void start(Element element)
+ {
+ resolveNodeValue(element);
+ }
+
+ public void endAttributes(Element element)
+ {
+ // already handled in start
+ }
+
+ public void end(Element element)
+ {
+ // already handled in start
+ }
+
+ public void visit(Attr attribute)
+ {
+ resolveNodeValue(attribute);
+ }
+
+ public void start(Document document)
+ {
+ resolveNodeValue(document);
+ }
+
+ public void end(Document doccument)
+ {
+
+ }
+
+ public void visit(Comment comment)
+ {
+ resolveNodeValue(comment);
+ }
+
+ public void visit(CDATASection cdata)
+ {
+ resolveNodeValue(cdata);
+ }
+
+ public void visit(Text text)
+ {
+ resolveNodeValue(text);
+ }
+
+ private void resolveNodeValue(Node n)
+ {
+ String value = n.getNodeValue();
+ if (value != null)
+ {
+ value = value.trim();
+ if (value.length() > 0)
+ {
+ String resolvedValue = valueHandler.handleValue(value);
+ n.setNodeValue(resolvedValue);
+ }
+ }
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/DynamicPropNodeVisitor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NamedNodeTypeRegistry.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NamedNodeTypeRegistry.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NamedNodeTypeRegistry.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NamedNodeTypeRegistry.java Fri Aug 12 08:12:28 2005
@@ -1,460 +1,460 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-/**
- */
-public class NamedNodeTypeRegistry
-{
- private final static String NAME_NAME = "name";
- private final static String ABSTRACT_NAME = "abstract";
- private final static String INHERIT_NAME = "inherit";
-
- public static boolean THROW_EXCEPTION_IF_DUPLICATE_ENTRY = true;
-
- private static Collection excludedAttrNames = new ArrayList(2);
- static {
- excludedAttrNames.add(ABSTRACT_NAME);
- excludedAttrNames.add(INHERIT_NAME);
- }
-
- private static NamedNodeTypeRegistry instance = null;
- private static boolean active = true;
-
- // NodeKey -> NodeEntry
- private Map mappings = new HashMap();
-
- private NamedNodeTypeRegistry()
- {
- }
-
- public synchronized static void deactivate()
- {
- active = false;
- // so it can get gc'd
- instance = null;
- }
-
- public synchronized static NamedNodeTypeRegistry getInstance()
- {
- if (active)
- {
- if (instance == null)
- {
- instance = new NamedNodeTypeRegistry();
- }
- }
- else
- {
- throw new RuntimeException("The NodeRegistry has been deactivated and can no longer be accessed");
- }
- return instance;
- }
-
- // setters
-
- public void addNodes(Collection nodeTypeNames, Document root, String source)
- throws NodeAlreadyRegisteredException
- {
- for (Iterator iter = nodeTypeNames.iterator(); iter.hasNext();)
- {
- String nodeTypeName = (String)iter.next();
- NodeList nodeList = root.getElementsByTagName(nodeTypeName);
- int numNodes = nodeList.getLength();
- for (int i = 0; i < numNodes; i++)
- {
- Node n = nodeList.item(i);
- String logicalName = DomUtils.getNameAttributeValue(n);
- addNode(logicalName, nodeTypeName, n, source);
- }
- }
- }
-
- private void addNode(String name, String type, Node node, String source)
- throws NodeAlreadyRegisteredException
- {
- addNode(new NodeKey(name, new NodeType(type)), node, source);
- }
-
- private void addNode(String name, Class type, Node node, String source)
- throws NodeAlreadyRegisteredException
- {
- addNode(new NodeKey(name, new NodeType(type)), node, source);
- }
-
- private void addNode(String name, NodeType type, Node node, String source)
- throws NodeAlreadyRegisteredException
- {
- addNode(new NodeKey(name, type), node, source);
- }
-
- private synchronized void addNode(NodeKey key, Node node, String source)
- throws NodeAlreadyRegisteredException
- {
- if (THROW_EXCEPTION_IF_DUPLICATE_ENTRY && getMappings().containsKey(key))
- throw new NodeAlreadyRegisteredException(key + " has already been registered", source);
- NodeEntry entry = new NodeEntry(node, source);
- getMappings().put(key, entry);
- }
-
- // getters
-
- public Node getNode(String name, Class type)
- throws ParentNotFoundException, CircularInheritanceException
- {
- return getNode(new NodeKey(name, new NodeType(type)));
- }
-
- public Node getNode(String name, String type)
- throws ParentNotFoundException, CircularInheritanceException
- {
- return getNode(new NodeKey(name, new NodeType(type)));
- }
-
- public Node getNode(String name, NodeType type)
- throws ParentNotFoundException, CircularInheritanceException
- {
- return getNode(new NodeKey(name, type));
- }
-
- public String getSource(Node n)
- {
- // FIXME need another mapping (Node -> Source)
- for (Iterator iter = getMappings().values().iterator(); iter.hasNext();)
- {
- NodeEntry entry = (NodeEntry)iter.next();
- if (entry.getNode() == n)
- return entry.getSource();
- }
- return null;
- }
-
- public Node getNode(NodeKey key)
- throws ParentNotFoundException, CircularInheritanceException
- {
- if (hasEntry(key))
- {
- Node n = ((NodeEntry)getMappings().get(key)).getNode();
- if (!isAbstract(n))
- {
- n = handleInheritance(n, key.getNodeType());
- return n;
- }
- }
- return null;
- }
-
- public Collection getNodesByType(String type)
- throws ParentNotFoundExceptionContainer, CircularInheritanceException
- {
- return getNodesByType(new NodeType(type));
- }
-
- public Collection getNodesByType(NodeType type)
- throws ParentNotFoundExceptionContainer, CircularInheritanceException
- {
- Collection ret = new ArrayList();
- Collection parentNotFoundExceptions = new ArrayList();
- for (Iterator iter = getMappings().keySet().iterator(); iter.hasNext();)
- {
- NodeKey key = (NodeKey)iter.next();
- if (key.getNodeType().equals(type))
- {
- Node n = null;
- try
- {
- n = getNode(key);
- }
- catch (ParentNotFoundException ex)
- {
- parentNotFoundExceptions.add(ex);
- }
- // might return null if this is abstract node
- // or if we caught ParentNotFoundException
- if (n != null)
- {
- ret.add(n);
- }
- }
- }
- if (!parentNotFoundExceptions.isEmpty())
- {
- throw new ParentNotFoundExceptionContainer(parentNotFoundExceptions);
- }
- return ret;
- }
-
- private boolean isAbstract(Node n)
- {
- return String
- .valueOf(DomUtils.getAttributeValue(n, ABSTRACT_NAME))
- .equalsIgnoreCase("true");
- }
-
- private void getAllSuperNodes(
- Node child,
- NodeType type,
- Collection superNodes)
- throws ParentNotFoundException, CircularInheritanceException
- {
- String superNodeName = getSuperNodeName(child);
- if (superNodeName == null)
- {
- return;
- }
- else
- {
- NodeKey key = new NodeKey(superNodeName, type);
- Node superNode = ((NodeEntry)getMappings().get(key)).getNode();
-
- if (superNode == null)
- {
- throw new ParentNotFoundException(
- type.toString(),
- DomUtils.getNameAttributeValue(child),
- child,
- superNodeName);
- }
-
- if (!superNodes.contains(superNode))
- {
- superNodes.add(superNode);
- getAllSuperNodes(superNode, type, superNodes);
- }
- else
- {
- StringBuffer inheritancePath = new StringBuffer();
- for (Iterator iter = superNodes.iterator(); iter.hasNext();)
- {
- Node n = (Node)iter.next();
- inheritancePath.append(DomUtils.getNameAttributeValue(n));
- if (iter.hasNext())
- {
- inheritancePath.append(" -> ");
- }
- }
- inheritancePath
- .append(". Then inherits again from \"")
- .append(superNodeName)
- .append("\".");
- throw new CircularInheritanceException(
- type.toString(),
- inheritancePath.toString());
- }
- }
- }
-
- private String getSuperNodeName(Node n)
- {
- return DomUtils.getAttributeValue(n, INHERIT_NAME);
- }
-
- private Node handleInheritance(Node child, NodeType type)
- throws ParentNotFoundException, CircularInheritanceException
- {
- List superNodes = new ArrayList();
- superNodes.add(child);
- getAllSuperNodes(child, type, superNodes);
- if (superNodes.size() == 1)
- {
- return child;
- }
- else
- {
- // loop over all super nodes (includes child), and build up the child node
- Node ret =
- ((Node)superNodes.remove(superNodes.size() - 1)).cloneNode(true);
- removeExcludedAttributes(ret);
- for (int i = superNodes.size() - 1; i >= 0; i--)
- {
- Node n = (Node)superNodes.get(i);
- DomUtils.addAttributes(ret, n, excludedAttrNames);
- DomUtils.addChildren(ret, n, NAME_NAME);
- }
- return ret;
- }
- }
-
- private void removeExcludedAttributes(Node n)
- {
- NamedNodeMap map = n.getAttributes();
- for (Iterator iter = excludedAttrNames.iterator(); iter.hasNext();)
- {
- String name = (String)iter.next();
- if (map.getNamedItem(name) != null)
- {
- map.removeNamedItem(name);
- }
- }
- }
-
- private boolean hasEntry(NodeKey key)
- {
- return getMappings().containsKey(key);
- }
-
- public String toString()
- {
- StringBuffer sb = new StringBuffer();
- sb.append("{");
- for (Iterator iter = getMappings().keySet().iterator(); iter.hasNext();)
- {
- NodeKey key = (NodeKey)iter.next();
- Node n = ((NodeEntry)getMappings().get(key)).getNode();
- StringBuffer nodeXML = new StringBuffer();
- DomUtils.writeDom(n, nodeXML);
- sb.append(key).append(" maps to ").append(nodeXML);
- if (iter.hasNext())
- {
- sb.append(",");
- }
- }
- sb.append("}");
- return sb.toString();
- }
-
- private Map getMappings()
- {
- return mappings;
- }
-
- public static class NodeType
- {
- private Class classType = null;
- private String stringType = null;
-
- public NodeType(Class in)
- {
- classType = in;
- }
-
- public NodeType(String inType)
- {
- stringType = inType;
- }
-
- public int hashCode()
- {
- return String.valueOf(stringType).hashCode()
- ^ String.valueOf(classType).hashCode();
- }
-
- public boolean equals(Object in)
- {
- if (!(in instanceof NodeType))
- {
- return false;
- }
- else
- {
- NodeType other = (NodeType)in;
- return (
- String.valueOf(getStringType()).equals(
- String.valueOf(other.getStringType()))
- && String.valueOf(getClassType()).equals(
- String.valueOf(other.getClassType())));
- }
- }
-
- private String getStringType()
- {
- return stringType;
- }
-
- private Class getClassType()
- {
- return classType;
- }
-
- public String toString()
- {
- if (stringType != null)
- {
- return stringType;
- }
- else
- {
- return classType.getName();
- }
- }
- }
-
- public static class NodeKey
- {
- private String name = null;
- private NodeType type = null;
-
- public NodeKey(String inName, NodeType inNodeType)
- {
- name = inName;
- type = inNodeType;
- }
-
- public NodeType getNodeType()
- {
- return type;
- }
-
- public int hashCode()
- {
- return name.hashCode() ^ type.hashCode();
- }
-
- public boolean equals(Object in)
- {
- if (!(in instanceof NodeKey))
- {
- return false;
- }
- else
- {
- NodeKey other = (NodeKey)in;
- return (
- getNodeType().equals(other.getNodeType())
- && getName().equals(other.getName()));
- }
- }
-
- public String toString()
- {
- return "(" + getNodeType().toString() + "," + getName() + ")";
- }
-
- private String getName()
- {
- return name;
- }
- }
-
- public static class NodeEntry
- {
- private Node node = null;
- private String source = null;
-
- public NodeEntry(Node inNode, String inSource)
- {
- node = inNode;
- source = inSource;
- }
-
- public Node getNode()
- {
- return node;
- }
-
- public String getSource()
- {
- return source;
- }
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ */
+public class NamedNodeTypeRegistry
+{
+ private final static String NAME_NAME = "name";
+ private final static String ABSTRACT_NAME = "abstract";
+ private final static String INHERIT_NAME = "inherit";
+
+ public static boolean THROW_EXCEPTION_IF_DUPLICATE_ENTRY = true;
+
+ private static Collection excludedAttrNames = new ArrayList(2);
+ static {
+ excludedAttrNames.add(ABSTRACT_NAME);
+ excludedAttrNames.add(INHERIT_NAME);
+ }
+
+ private static NamedNodeTypeRegistry instance = null;
+ private static boolean active = true;
+
+ // NodeKey -> NodeEntry
+ private Map mappings = new HashMap();
+
+ private NamedNodeTypeRegistry()
+ {
+ }
+
+ public synchronized static void deactivate()
+ {
+ active = false;
+ // so it can get gc'd
+ instance = null;
+ }
+
+ public synchronized static NamedNodeTypeRegistry getInstance()
+ {
+ if (active)
+ {
+ if (instance == null)
+ {
+ instance = new NamedNodeTypeRegistry();
+ }
+ }
+ else
+ {
+ throw new RuntimeException("The NodeRegistry has been deactivated and can no longer be accessed");
+ }
+ return instance;
+ }
+
+ // setters
+
+ public void addNodes(Collection nodeTypeNames, Document root, String source)
+ throws NodeAlreadyRegisteredException
+ {
+ for (Iterator iter = nodeTypeNames.iterator(); iter.hasNext();)
+ {
+ String nodeTypeName = (String)iter.next();
+ NodeList nodeList = root.getElementsByTagName(nodeTypeName);
+ int numNodes = nodeList.getLength();
+ for (int i = 0; i < numNodes; i++)
+ {
+ Node n = nodeList.item(i);
+ String logicalName = DomUtils.getNameAttributeValue(n);
+ addNode(logicalName, nodeTypeName, n, source);
+ }
+ }
+ }
+
+ private void addNode(String name, String type, Node node, String source)
+ throws NodeAlreadyRegisteredException
+ {
+ addNode(new NodeKey(name, new NodeType(type)), node, source);
+ }
+
+ private void addNode(String name, Class type, Node node, String source)
+ throws NodeAlreadyRegisteredException
+ {
+ addNode(new NodeKey(name, new NodeType(type)), node, source);
+ }
+
+ private void addNode(String name, NodeType type, Node node, String source)
+ throws NodeAlreadyRegisteredException
+ {
+ addNode(new NodeKey(name, type), node, source);
+ }
+
+ private synchronized void addNode(NodeKey key, Node node, String source)
+ throws NodeAlreadyRegisteredException
+ {
+ if (THROW_EXCEPTION_IF_DUPLICATE_ENTRY && getMappings().containsKey(key))
+ throw new NodeAlreadyRegisteredException(key + " has already been registered", source);
+ NodeEntry entry = new NodeEntry(node, source);
+ getMappings().put(key, entry);
+ }
+
+ // getters
+
+ public Node getNode(String name, Class type)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ return getNode(new NodeKey(name, new NodeType(type)));
+ }
+
+ public Node getNode(String name, String type)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ return getNode(new NodeKey(name, new NodeType(type)));
+ }
+
+ public Node getNode(String name, NodeType type)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ return getNode(new NodeKey(name, type));
+ }
+
+ public String getSource(Node n)
+ {
+ // FIXME need another mapping (Node -> Source)
+ for (Iterator iter = getMappings().values().iterator(); iter.hasNext();)
+ {
+ NodeEntry entry = (NodeEntry)iter.next();
+ if (entry.getNode() == n)
+ return entry.getSource();
+ }
+ return null;
+ }
+
+ public Node getNode(NodeKey key)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ if (hasEntry(key))
+ {
+ Node n = ((NodeEntry)getMappings().get(key)).getNode();
+ if (!isAbstract(n))
+ {
+ n = handleInheritance(n, key.getNodeType());
+ return n;
+ }
+ }
+ return null;
+ }
+
+ public Collection getNodesByType(String type)
+ throws ParentNotFoundExceptionContainer, CircularInheritanceException
+ {
+ return getNodesByType(new NodeType(type));
+ }
+
+ public Collection getNodesByType(NodeType type)
+ throws ParentNotFoundExceptionContainer, CircularInheritanceException
+ {
+ Collection ret = new ArrayList();
+ Collection parentNotFoundExceptions = new ArrayList();
+ for (Iterator iter = getMappings().keySet().iterator(); iter.hasNext();)
+ {
+ NodeKey key = (NodeKey)iter.next();
+ if (key.getNodeType().equals(type))
+ {
+ Node n = null;
+ try
+ {
+ n = getNode(key);
+ }
+ catch (ParentNotFoundException ex)
+ {
+ parentNotFoundExceptions.add(ex);
+ }
+ // might return null if this is abstract node
+ // or if we caught ParentNotFoundException
+ if (n != null)
+ {
+ ret.add(n);
+ }
+ }
+ }
+ if (!parentNotFoundExceptions.isEmpty())
+ {
+ throw new ParentNotFoundExceptionContainer(parentNotFoundExceptions);
+ }
+ return ret;
+ }
+
+ private boolean isAbstract(Node n)
+ {
+ return String
+ .valueOf(DomUtils.getAttributeValue(n, ABSTRACT_NAME))
+ .equalsIgnoreCase("true");
+ }
+
+ private void getAllSuperNodes(
+ Node child,
+ NodeType type,
+ Collection superNodes)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ String superNodeName = getSuperNodeName(child);
+ if (superNodeName == null)
+ {
+ return;
+ }
+ else
+ {
+ NodeKey key = new NodeKey(superNodeName, type);
+ Node superNode = ((NodeEntry)getMappings().get(key)).getNode();
+
+ if (superNode == null)
+ {
+ throw new ParentNotFoundException(
+ type.toString(),
+ DomUtils.getNameAttributeValue(child),
+ child,
+ superNodeName);
+ }
+
+ if (!superNodes.contains(superNode))
+ {
+ superNodes.add(superNode);
+ getAllSuperNodes(superNode, type, superNodes);
+ }
+ else
+ {
+ StringBuffer inheritancePath = new StringBuffer();
+ for (Iterator iter = superNodes.iterator(); iter.hasNext();)
+ {
+ Node n = (Node)iter.next();
+ inheritancePath.append(DomUtils.getNameAttributeValue(n));
+ if (iter.hasNext())
+ {
+ inheritancePath.append(" -> ");
+ }
+ }
+ inheritancePath
+ .append(". Then inherits again from \"")
+ .append(superNodeName)
+ .append("\".");
+ throw new CircularInheritanceException(
+ type.toString(),
+ inheritancePath.toString());
+ }
+ }
+ }
+
+ private String getSuperNodeName(Node n)
+ {
+ return DomUtils.getAttributeValue(n, INHERIT_NAME);
+ }
+
+ private Node handleInheritance(Node child, NodeType type)
+ throws ParentNotFoundException, CircularInheritanceException
+ {
+ List superNodes = new ArrayList();
+ superNodes.add(child);
+ getAllSuperNodes(child, type, superNodes);
+ if (superNodes.size() == 1)
+ {
+ return child;
+ }
+ else
+ {
+ // loop over all super nodes (includes child), and build up the child node
+ Node ret =
+ ((Node)superNodes.remove(superNodes.size() - 1)).cloneNode(true);
+ removeExcludedAttributes(ret);
+ for (int i = superNodes.size() - 1; i >= 0; i--)
+ {
+ Node n = (Node)superNodes.get(i);
+ DomUtils.addAttributes(ret, n, excludedAttrNames);
+ DomUtils.addChildren(ret, n, NAME_NAME);
+ }
+ return ret;
+ }
+ }
+
+ private void removeExcludedAttributes(Node n)
+ {
+ NamedNodeMap map = n.getAttributes();
+ for (Iterator iter = excludedAttrNames.iterator(); iter.hasNext();)
+ {
+ String name = (String)iter.next();
+ if (map.getNamedItem(name) != null)
+ {
+ map.removeNamedItem(name);
+ }
+ }
+ }
+
+ private boolean hasEntry(NodeKey key)
+ {
+ return getMappings().containsKey(key);
+ }
+
+ public String toString()
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append("{");
+ for (Iterator iter = getMappings().keySet().iterator(); iter.hasNext();)
+ {
+ NodeKey key = (NodeKey)iter.next();
+ Node n = ((NodeEntry)getMappings().get(key)).getNode();
+ StringBuffer nodeXML = new StringBuffer();
+ DomUtils.writeDom(n, nodeXML);
+ sb.append(key).append(" maps to ").append(nodeXML);
+ if (iter.hasNext())
+ {
+ sb.append(",");
+ }
+ }
+ sb.append("}");
+ return sb.toString();
+ }
+
+ private Map getMappings()
+ {
+ return mappings;
+ }
+
+ public static class NodeType
+ {
+ private Class classType = null;
+ private String stringType = null;
+
+ public NodeType(Class in)
+ {
+ classType = in;
+ }
+
+ public NodeType(String inType)
+ {
+ stringType = inType;
+ }
+
+ public int hashCode()
+ {
+ return String.valueOf(stringType).hashCode()
+ ^ String.valueOf(classType).hashCode();
+ }
+
+ public boolean equals(Object in)
+ {
+ if (!(in instanceof NodeType))
+ {
+ return false;
+ }
+ else
+ {
+ NodeType other = (NodeType)in;
+ return (
+ String.valueOf(getStringType()).equals(
+ String.valueOf(other.getStringType()))
+ && String.valueOf(getClassType()).equals(
+ String.valueOf(other.getClassType())));
+ }
+ }
+
+ private String getStringType()
+ {
+ return stringType;
+ }
+
+ private Class getClassType()
+ {
+ return classType;
+ }
+
+ public String toString()
+ {
+ if (stringType != null)
+ {
+ return stringType;
+ }
+ else
+ {
+ return classType.getName();
+ }
+ }
+ }
+
+ public static class NodeKey
+ {
+ private String name = null;
+ private NodeType type = null;
+
+ public NodeKey(String inName, NodeType inNodeType)
+ {
+ name = inName;
+ type = inNodeType;
+ }
+
+ public NodeType getNodeType()
+ {
+ return type;
+ }
+
+ public int hashCode()
+ {
+ return name.hashCode() ^ type.hashCode();
+ }
+
+ public boolean equals(Object in)
+ {
+ if (!(in instanceof NodeKey))
+ {
+ return false;
+ }
+ else
+ {
+ NodeKey other = (NodeKey)in;
+ return (
+ getNodeType().equals(other.getNodeType())
+ && getName().equals(other.getName()));
+ }
+ }
+
+ public String toString()
+ {
+ return "(" + getNodeType().toString() + "," + getName() + ")";
+ }
+
+ private String getName()
+ {
+ return name;
+ }
+ }
+
+ public static class NodeEntry
+ {
+ private Node node = null;
+ private String source = null;
+
+ public NodeEntry(Node inNode, String inSource)
+ {
+ node = inNode;
+ source = inSource;
+ }
+
+ public Node getNode()
+ {
+ return node;
+ }
+
+ public String getSource()
+ {
+ return source;
+ }
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NamedNodeTypeRegistry.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingException.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingException.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingException.java Fri Aug 12 08:12:28 2005
@@ -1,12 +1,12 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.apache.beehive.test.tools.tch.util.NestedException;
-
-public class NestedXMLProcessingException
- extends NestedException
-{
- public NestedXMLProcessingException(Exception nestedException)
- {
- super(nestedException);
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.apache.beehive.test.tools.tch.util.NestedException;
+
+public class NestedXMLProcessingException
+ extends NestedException
+{
+ public NestedXMLProcessingException(Exception nestedException)
+ {
+ super(nestedException);
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingRuntimeException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingRuntimeException.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingRuntimeException.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingRuntimeException.java Fri Aug 12 08:12:28 2005
@@ -1,12 +1,12 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.apache.beehive.test.tools.tch.util.NestedRuntimeException;
-
-public class NestedXMLProcessingRuntimeException
- extends NestedRuntimeException
-{
- public NestedXMLProcessingRuntimeException(Throwable nestedException)
- {
- super(nestedException);
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.apache.beehive.test.tools.tch.util.NestedRuntimeException;
+
+public class NestedXMLProcessingRuntimeException
+ extends NestedRuntimeException
+{
+ public NestedXMLProcessingRuntimeException(Throwable nestedException)
+ {
+ super(nestedException);
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NestedXMLProcessingRuntimeException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NoOpNodeVisitor.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NoOpNodeVisitor.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NoOpNodeVisitor.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NoOpNodeVisitor.java Fri Aug 12 08:12:28 2005
@@ -1,50 +1,50 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Text;
-
-/**
- *
- */
-public class NoOpNodeVisitor implements NodeVisitor
-{
- public void start(Element element)
- {
- }
-
- public void endAttributes(Element element)
- {
- }
-
- public void end(Element element)
- {
- }
-
- public void visit(Attr attribute)
- {
- }
-
- public void start(Document document)
- {
- }
-
- public void end(Document doccument)
- {
- }
-
- public void visit(Comment comment)
- {
- }
-
- public void visit(CDATASection cdata)
- {
- }
-
- public void visit(Text text)
- {
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+/**
+ *
+ */
+public class NoOpNodeVisitor implements NodeVisitor
+{
+ public void start(Element element)
+ {
+ }
+
+ public void endAttributes(Element element)
+ {
+ }
+
+ public void end(Element element)
+ {
+ }
+
+ public void visit(Attr attribute)
+ {
+ }
+
+ public void start(Document document)
+ {
+ }
+
+ public void end(Document doccument)
+ {
+ }
+
+ public void visit(Comment comment)
+ {
+ }
+
+ public void visit(CDATASection cdata)
+ {
+ }
+
+ public void visit(Text text)
+ {
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NoOpNodeVisitor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeAlreadyRegisteredException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeAlreadyRegisteredException.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeAlreadyRegisteredException.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeAlreadyRegisteredException.java Fri Aug 12 08:12:28 2005
@@ -1,18 +1,18 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-/**
- */
-public class NodeAlreadyRegisteredException extends Exception
-{
- private String source = null;
- public NodeAlreadyRegisteredException(String inMessage, String inSource)
- {
- super(inMessage);
- source = inSource;
- }
-
- public String getSource()
- {
- return source;
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+/**
+ */
+public class NodeAlreadyRegisteredException extends Exception
+{
+ private String source = null;
+ public NodeAlreadyRegisteredException(String inMessage, String inSource)
+ {
+ super(inMessage);
+ source = inSource;
+ }
+
+ public String getSource()
+ {
+ return source;
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeAlreadyRegisteredException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitor.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitor.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitor.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitor.java Fri Aug 12 08:12:28 2005
@@ -1,25 +1,25 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Text;
-
-/**
- * Use adapted visitor pattern to visit each Node in a DOM.
- *
- */
-public interface NodeVisitor
-{
- public void start(Element element);
- public void endAttributes(Element element);
- public void end(Element element);
- public void visit(Attr attribute);
- public void start(Document document);
- public void end(Document doccument);
- public void visit(Comment comment);
- public void visit(CDATASection cdata);
- public void visit(Text text);
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+/**
+ * Use adapted visitor pattern to visit each Node in a DOM.
+ *
+ */
+public interface NodeVisitor
+{
+ public void start(Element element);
+ public void endAttributes(Element element);
+ public void end(Element element);
+ public void visit(Attr attribute);
+ public void start(Document document);
+ public void end(Document doccument);
+ public void visit(Comment comment);
+ public void visit(CDATASection cdata);
+ public void visit(Text text);
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitorDOMWriter.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitorDOMWriter.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitorDOMWriter.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitorDOMWriter.java Fri Aug 12 08:12:28 2005
@@ -1,131 +1,131 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import java.io.File;
-import java.io.PrintWriter;
-import java.util.Map;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.CDATASection;
-import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
-import org.w3c.dom.DocumentType;
-import org.w3c.dom.Element;
-import org.w3c.dom.Text;
-
-import org.apache.beehive.test.tools.tch.util.OrderedMap;
-
-/**
- * Print a DOM using the Node Visitor.
- *
- */
-public class NodeVisitorDOMWriter
- extends NoOpNodeVisitor
- implements NodeVisitor
-{
- private static final int NUM_INDENT_SPACES_DEFAULT = 2;
- private static final int MAX_NUM_ATTRS_ON_SAME_LINE_DEFAULT = 2;
-
- private static final boolean DEFAULT_ESCAPE = true;
-
- private XMLWriter writer = null;
-
- private Map currentAttributes = null;
-
-
- public NodeVisitorDOMWriter(PrintWriter inPrintWriter)
- {
- this(inPrintWriter, MAX_NUM_ATTRS_ON_SAME_LINE_DEFAULT);
- }
-
- public NodeVisitorDOMWriter(
- PrintWriter inPrintWriter,
- int inMaxNumAttrsOnSameLine)
- {
- this(
- inPrintWriter,
- inMaxNumAttrsOnSameLine,
- NUM_INDENT_SPACES_DEFAULT,
- DEFAULT_ESCAPE);
- }
-
- public NodeVisitorDOMWriter(
- PrintWriter inPrintWriter,
- int inMaxNumAttrsOnSameLine,
- boolean escape)
- {
- this(
- inPrintWriter,
- inMaxNumAttrsOnSameLine,
- NUM_INDENT_SPACES_DEFAULT,
- escape);
- }
-
- public NodeVisitorDOMWriter(
- PrintWriter inPrintWriter,
- int inMaxNumAttrsOnSameLine,
- int inNumIndentSpaces,
- boolean escape)
- {
- PrintWriterXMLWriter w =
- new PrintWriterXMLWriter(inNumIndentSpaces, inMaxNumAttrsOnSameLine);
- w.setEscape(escape);
- w.setPrintWriter(inPrintWriter);
- writer = w;
- }
-
- public void start(Element element)
- {
- writer.addElement(element.getNodeName());
- currentAttributes = new OrderedMap();
- }
-
- public void end(Element element)
- {
- writer.closeElement();
- }
-
- public void visit(Attr attribute)
- {
- currentAttributes.put(attribute.getName(), attribute.getValue());
- }
-
- public void endAttributes(Element element)
- {
- writer.addAttribute(currentAttributes);
- }
-
- public void start(Document document)
- {
- DocumentType doctype = document.getDoctype();
- if (doctype != null)
- writer.startDocument(
- doctype.getName(),
- doctype.getPublicId(),
- doctype.getSystemId());
- }
-
- public void visit(Comment comment)
- {
- writer.addComment(comment.getData());
- }
-
- public void visit(CDATASection cdata)
- {
- writer.addCDATA(cdata.getData());
- }
-
- public void visit(Text text)
- {
- writer.addText(text.getData());
- }
-
- public static void main(String[] args) throws Exception
- {
- File f = new File(args[0]);
- System.out.println("Reading and writing file " + f);
- Document doc = DomUtils.instantiateDom(f);
- DomUtils.visitDom(
- doc,
- new NodeVisitorDOMWriter(new PrintWriter(System.out)));
- }
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.util.Map;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
+import org.w3c.dom.Comment;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+import org.apache.beehive.test.tools.tch.util.OrderedMap;
+
+/**
+ * Print a DOM using the Node Visitor.
+ *
+ */
+public class NodeVisitorDOMWriter
+ extends NoOpNodeVisitor
+ implements NodeVisitor
+{
+ private static final int NUM_INDENT_SPACES_DEFAULT = 2;
+ private static final int MAX_NUM_ATTRS_ON_SAME_LINE_DEFAULT = 2;
+
+ private static final boolean DEFAULT_ESCAPE = true;
+
+ private XMLWriter writer = null;
+
+ private Map currentAttributes = null;
+
+
+ public NodeVisitorDOMWriter(PrintWriter inPrintWriter)
+ {
+ this(inPrintWriter, MAX_NUM_ATTRS_ON_SAME_LINE_DEFAULT);
+ }
+
+ public NodeVisitorDOMWriter(
+ PrintWriter inPrintWriter,
+ int inMaxNumAttrsOnSameLine)
+ {
+ this(
+ inPrintWriter,
+ inMaxNumAttrsOnSameLine,
+ NUM_INDENT_SPACES_DEFAULT,
+ DEFAULT_ESCAPE);
+ }
+
+ public NodeVisitorDOMWriter(
+ PrintWriter inPrintWriter,
+ int inMaxNumAttrsOnSameLine,
+ boolean escape)
+ {
+ this(
+ inPrintWriter,
+ inMaxNumAttrsOnSameLine,
+ NUM_INDENT_SPACES_DEFAULT,
+ escape);
+ }
+
+ public NodeVisitorDOMWriter(
+ PrintWriter inPrintWriter,
+ int inMaxNumAttrsOnSameLine,
+ int inNumIndentSpaces,
+ boolean escape)
+ {
+ PrintWriterXMLWriter w =
+ new PrintWriterXMLWriter(inNumIndentSpaces, inMaxNumAttrsOnSameLine);
+ w.setEscape(escape);
+ w.setPrintWriter(inPrintWriter);
+ writer = w;
+ }
+
+ public void start(Element element)
+ {
+ writer.addElement(element.getNodeName());
+ currentAttributes = new OrderedMap();
+ }
+
+ public void end(Element element)
+ {
+ writer.closeElement();
+ }
+
+ public void visit(Attr attribute)
+ {
+ currentAttributes.put(attribute.getName(), attribute.getValue());
+ }
+
+ public void endAttributes(Element element)
+ {
+ writer.addAttribute(currentAttributes);
+ }
+
+ public void start(Document document)
+ {
+ DocumentType doctype = document.getDoctype();
+ if (doctype != null)
+ writer.startDocument(
+ doctype.getName(),
+ doctype.getPublicId(),
+ doctype.getSystemId());
+ }
+
+ public void visit(Comment comment)
+ {
+ writer.addComment(comment.getData());
+ }
+
+ public void visit(CDATASection cdata)
+ {
+ writer.addCDATA(cdata.getData());
+ }
+
+ public void visit(Text text)
+ {
+ writer.addText(text.getData());
+ }
+
+ public static void main(String[] args) throws Exception
+ {
+ File f = new File(args[0]);
+ System.out.println("Reading and writing file " + f);
+ Document doc = DomUtils.instantiateDom(f);
+ DomUtils.visitDom(
+ doc,
+ new NodeVisitorDOMWriter(new PrintWriter(System.out)));
+ }
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/NodeVisitorDOMWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundException.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundException.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundException.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundException.java Fri Aug 12 08:12:28 2005
@@ -1,63 +1,63 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import org.w3c.dom.Node;
-
-public class ParentNotFoundException extends Exception
-{
- private String referencerNodeType = null,
- referencerNodeName = null,
- referencedNodeName = null;
- private Node referencerNode = null;
-
- public ParentNotFoundException(
- String inRefNodeType,
- String inRefNodeName,
- Node inRefNode,
- String inRefdNodeName)
- {
- super();
- referencerNodeType = inRefNodeType;
- referencerNodeName = inRefNodeName;
- referencerNode = inRefNode;
- referencedNodeName = inRefdNodeName;
- }
- /**
- * @return
- */
- public String getReferencedNodeName()
- {
- return referencedNodeName;
- }
-
- /**
- * @return
- */
- public Node getReferencerNode()
- {
- return referencerNode;
- }
-
- /**
- * @return
- */
- public String getReferencerNodeName()
- {
- return referencerNodeName;
- }
-
- /**
- * @return
- */
- public String getReferencerNodeType()
- {
- return referencerNodeType;
- }
-
- public String getReferencerNodeXMLDump()
- {
- StringBuffer sb = new StringBuffer();
- DomUtils.writeDom(referencerNode, sb);
- return sb.toString();
- }
-
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import org.w3c.dom.Node;
+
+public class ParentNotFoundException extends Exception
+{
+ private String referencerNodeType = null,
+ referencerNodeName = null,
+ referencedNodeName = null;
+ private Node referencerNode = null;
+
+ public ParentNotFoundException(
+ String inRefNodeType,
+ String inRefNodeName,
+ Node inRefNode,
+ String inRefdNodeName)
+ {
+ super();
+ referencerNodeType = inRefNodeType;
+ referencerNodeName = inRefNodeName;
+ referencerNode = inRefNode;
+ referencedNodeName = inRefdNodeName;
+ }
+ /**
+ * @return
+ */
+ public String getReferencedNodeName()
+ {
+ return referencedNodeName;
+ }
+
+ /**
+ * @return
+ */
+ public Node getReferencerNode()
+ {
+ return referencerNode;
+ }
+
+ /**
+ * @return
+ */
+ public String getReferencerNodeName()
+ {
+ return referencerNodeName;
+ }
+
+ /**
+ * @return
+ */
+ public String getReferencerNodeType()
+ {
+ return referencerNodeType;
+ }
+
+ public String getReferencerNodeXMLDump()
+ {
+ StringBuffer sb = new StringBuffer();
+ DomUtils.writeDom(referencerNode, sb);
+ return sb.toString();
+ }
+
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundExceptionContainer.java
URL: http://svn.apache.org/viewcvs/beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundExceptionContainer.java?rev=232310&r1=232309&r2=232310&view=diff
==============================================================================
--- beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundExceptionContainer.java (original)
+++ beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundExceptionContainer.java Fri Aug 12 08:12:28 2005
@@ -1,25 +1,25 @@
-package org.apache.beehive.test.tools.tch.util.xml;
-
-import java.util.ArrayList;
-import java.util.Collection;
-
-/**
- */
-public class ParentNotFoundExceptionContainer extends Exception
-{
- private Collection parentNotFoundExceptions = new ArrayList();
-
- public ParentNotFoundExceptionContainer(Collection in)
- {
- parentNotFoundExceptions = in;
- }
-
- /**
- * @return
- */
- public Collection getParentNotFoundExceptions()
- {
- return parentNotFoundExceptions;
- }
-
-}
+package org.apache.beehive.test.tools.tch.util.xml;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ */
+public class ParentNotFoundExceptionContainer extends Exception
+{
+ private Collection parentNotFoundExceptions = new ArrayList();
+
+ public ParentNotFoundExceptionContainer(Collection in)
+ {
+ parentNotFoundExceptions = in;
+ }
+
+ /**
+ * @return
+ */
+ public Collection getParentNotFoundExceptions()
+ {
+ return parentNotFoundExceptions;
+ }
+
+}
Propchange: beehive/trunk/controls/test/tools/tch/src/java/org/apache/beehive/test/tools/tch/util/xml/ParentNotFoundExceptionContainer.java
------------------------------------------------------------------------------
svn:eol-style = native