From 74b1e7d3e0e27b710647f80e23df58497079722f Mon Sep 17 00:00:00 2001 From: codistmonk Date: Fri, 2 Jul 2010 22:05:34 +0000 Subject: [PATCH] [Aprog] Updated project properties. Updated XMLTools + tests. git-svn-id: https://aprog.svn.sourceforge.net/svnroot/aprog/trunk@79 7cbf5e2b-b55d-4b93-acdd-c0d7b961df51 --- Aprog/nbproject/build-impl.xml | 246 ++++++++++++++------- Aprog/nbproject/genfiles.properties | 6 +- Aprog/nbproject/project.properties | 2 +- Aprog/src/net/sourceforge/aprog/xml/XMLTools.java | 147 ++++++++++-- .../net/sourceforge/aprog/xml/XMLToolsTest.java | 89 +++++++- 5 files changed, 384 insertions(+), 106 deletions(-) diff --git a/Aprog/nbproject/build-impl.xml b/Aprog/nbproject/build-impl.xml index 96f0359..b572d38 100644 --- a/Aprog/nbproject/build-impl.xml +++ b/Aprog/nbproject/build-impl.xml @@ -55,6 +55,7 @@ is divided into following sections: + @@ -83,23 +84,40 @@ is divided into following sections: - + - - + + - - - - - + + + + - - + + + + + + + + - - + + + + + + + + + + + + + + @@ -156,6 +174,8 @@ is divided into following sections: + + @@ -182,11 +202,53 @@ is divided into following sections: - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -211,6 +273,8 @@ is divided into following sections: + + @@ -358,14 +422,65 @@ is divided into following sections: + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + @@ -447,13 +568,13 @@ is divided into following sections: - + - + - + @@ -468,78 +589,35 @@ is divided into following sections: java -cp "${run.classpath.with.dist.jar}" ${main.class} - - - - - - - - - - - - - - - - - + + + + + + - - - + + + To run this application from the command line without Ant, try: java -jar "${dist.jar.resolved}" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" - + - + @@ -643,7 +721,7 @@ is divided into following sections: - + @@ -660,7 +738,7 @@ is divided into following sections: Must select some files in the IDE or set javac.includes - + diff --git a/Aprog/nbproject/genfiles.properties b/Aprog/nbproject/genfiles.properties index 686b855..23dbd01 100644 --- a/Aprog/nbproject/genfiles.properties +++ b/Aprog/nbproject/genfiles.properties @@ -1,8 +1,8 @@ build.xml.data.CRC32=670924e4 build.xml.script.CRC32=e9cbadeb -build.xml.stylesheet.CRC32=958a1d3e@1.32.1.45 +build.xml.stylesheet.CRC32=28e38971@1.38.1.45 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. nbproject/build-impl.xml.data.CRC32=670924e4 -nbproject/build-impl.xml.script.CRC32=2ba23a6a -nbproject/build-impl.xml.stylesheet.CRC32=576378a2@1.32.1.45 +nbproject/build-impl.xml.script.CRC32=f06f152b +nbproject/build-impl.xml.stylesheet.CRC32=78c6a6ee@1.38.1.45 diff --git a/Aprog/nbproject/project.properties b/Aprog/nbproject/project.properties index 78d46cf..2fffded 100644 --- a/Aprog/nbproject/project.properties +++ b/Aprog/nbproject/project.properties @@ -18,7 +18,7 @@ debug.test.classpath=\ ${run.test.classpath} # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/aprog-1.0.0-M3.jar +dist.jar=${dist.dir}/aprog-1.0.0-M4.jar dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= diff --git a/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java b/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java index d0be576..63bc4b5 100644 --- a/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java +++ b/Aprog/src/net/sourceforge/aprog/xml/XMLTools.java @@ -32,7 +32,9 @@ import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; import javax.xml.XMLConstants; @@ -53,11 +55,14 @@ import javax.xml.transform.stream.StreamSource; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import net.sourceforge.aprog.tools.IllegalInstantiationException; +import org.w3c.dom.Attr; import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -384,20 +389,6 @@ public final class XMLTools { /** * * @param context - *
Not null - * @param xPath - *
Not null - * @return - *
Maybe null - *
Maybe New - */ - public static final Node getOrCreateNode(final Node context, final String xPath) { - return getOrCreateNode((Object) context, xPath); - } - - /** - * - * @param context *
Maybe null * @param xPath *
Not null @@ -457,6 +448,34 @@ public final class XMLTools { /** * + * @param context + *
Maybe null + * @param xPath + *
Not null + * @return + *
Maybe null + *
Not New + */ + public static final Number getNumber(final Object context, final String xPath) { + return (Number) get(context, xPath, XPathConstants.NUMBER); + } + + /** + * + * @param context + *
Maybe null + * @param xPath + *
Not null + * @return + *
Maybe null + *
Not New + */ + public static final String getString(final Object context, final String xPath) { + return (String) get(context, xPath, XPathConstants.STRING); + } + + /** + * * @param The expected return type * @param context *
Maybe null @@ -487,7 +506,6 @@ public final class XMLTools { /** * - * @param The expected node type * @param context *
Not null * @param xPath @@ -496,8 +514,103 @@ public final class XMLTools { *
Maybe null *
Maybe new */ - public static final N getOrCreateNode(final Object context, final String xPath) { - return null; // TODO + public static final Node getOrCreateNode(final Node context, final String xPath) { + final String[] xPathElements = xPath.split("/"); + Node result = context; + + try { + for (int index = 0; index < xPathElements.length && result != null; ++index) { + final String xPathElement = xPathElements[index]; + + if (xPathElement.matches("\\w+\\[\\]")) { + result = addChild(result, xPathElement); + } else { + final Node parent = result; + result = getNode(result, xPathElement); + + if (result == null) { + final Map attributes = getEqualityPredicates(xPathElement); + final Integer nameEnd = xPathElement.indexOf("["); + final String childName = nameEnd < 0 ? xPathElement : xPathElement.substring(0, nameEnd); + + result = addChild(parent, childName); + + for (final Map.Entry entry : attributes.entrySet()) { + addChild(result, entry.getKey()).setNodeValue(entry.getValue()); + } + } + } + } + } catch (final Exception exception) { + throw unchecked(exception); + } + + return result; + } + + /** + * + * @param parent + *
Not null + *
Input-output + * @param xPathElement + *
Not null + * @return + *
Not null + *
New + */ + public static final Node addChild(final Node parent, final String xPathElement) { + final Boolean isAttribute = xPathElement.startsWith("@"); + final String name = isAttribute ? xPathElement.substring(1) : xPathElement.split("\\[")[0]; + final Document document = getOwnerDocument(parent); + final Node result = isAttribute ? document.createAttribute(name) : document.createElement(name); + + if (isAttribute) { + ((Element) parent).setAttributeNode((Attr) result); + } else { + parent.appendChild(result); + } + + return result; + } + + /** + * + * @param node + *
Not null + * @return + *
Maybe null + */ + public static final Document getOwnerDocument(final Node node) { + if (node.getOwnerDocument() == null && node instanceof Document) { + return (Document) node; + } + + return node.getOwnerDocument(); + } + + /** + * + * @param xPathElement + *
Not null + * @return + *
Not null + *
New + */ + private static final Map getEqualityPredicates(final String xPathElement) { + final LinkedHashMap result = new LinkedHashMap(); + + if (xPathElement.trim().endsWith("]")) { + final String[] keyValues = xPathElement + .substring(xPathElement.indexOf("[") + 1, xPathElement.length() - 1) + .split("=|and"); + + for (int i = 0; i < keyValues.length; i += 2) { + result.put(keyValues[i], getString(null, keyValues[i + 1])); + } + } + + return result; } } \ No newline at end of file diff --git a/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java b/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java index f1c6478..dd7dca3 100644 --- a/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java +++ b/Aprog/test/net/sourceforge/aprog/xml/XMLToolsTest.java @@ -30,8 +30,10 @@ import static net.sourceforge.aprog.xml.XMLTools.*; import static org.junit.Assert.*; import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; +import java.io.OutputStreamWriter; import java.util.logging.Level; +import javax.xml.transform.Result; +import javax.xml.transform.stream.StreamResult; import org.junit.Test; import org.w3c.dom.Document; @@ -118,6 +120,91 @@ public final class XMLToolsTest { ); assertEquals("d", getNode(document, "a/b/@c").getNodeValue()); + assertEquals("b", getNode(document, "a/b[@c='d']").getNodeName()); + assertNull(getNode(document, "a/b[@c='e']")); + } + + @Test + public final void testGetOrCreateNode() { + final Document document = newDocument(); + + getOrCreateNode(document, "aa[]"); + + { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final Result result = new StreamResult(new OutputStreamWriter(buffer)); + + write(normalize(document).getDocumentElement(), result, 0); + + assertEquals( + "" + , buffer.toString()); + } + + getOrCreateNode(document, "aa/b"); + + { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final Result result = new StreamResult(new OutputStreamWriter(buffer)); + + write(normalize(document).getDocumentElement(), result, 0); + + assertEquals( + "" + + "" + + "" + , buffer.toString()); + } + + getOrCreateNode(document, "aa/b[@c='d']"); + + { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final Result result = new StreamResult(buffer); + + write(normalize(document).getDocumentElement(), result, 0); + + assertEquals( + "" + + "" + + "" + + "" + , buffer.toString()); + } + + getOrCreateNode(document, "aa/b[]"); + + { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final Result result = new StreamResult(buffer); + + write(normalize(document).getDocumentElement(), result, 0); + + assertEquals( + "" + + "" + + "" + + "" + + "" + , buffer.toString()); + } + + getOrCreateNode(document, "aa/b[position()=2]/e"); + + { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + final Result result = new StreamResult(buffer); + + write(normalize(document).getDocumentElement(), result, 0); + + assertEquals( + "" + + "" + + "" + + "" + + "" + , buffer.toString()); + } } @Test -- 2.11.4.GIT