Author: tfennelly
Date: 2010-01-17 04:06:10 -0500 (Sun, 17 Jan 2010)
New Revision: 19786
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/xml/XMLSampleModelBuilder.java
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLSample_XMLFreeMarkerTemplateBuilderTest.java
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XSD_XMLFreeMarkerTemplateBuilderTest.java
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-01.xml
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-02.xml
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml
Removed:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLFreeMarkerTemplateBuilderTest.java
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/ModelBuilder.java
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/template/TemplateBuilder.java
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/TestUtil.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-5245
Templating API parts of Create FreeMarker Template builder for generating XML messages
(from an XML Sample). Need to integrate into editor now.
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/ModelBuilder.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/ModelBuilder.java 2010-01-15
22:44:22 UTC (rev 19785)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/ModelBuilder.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -163,7 +163,7 @@
return builder.newDocument();
}
- protected void setMinMax(Element element, int minOccurs, int maxOccurs) {
+ public static void setMinMax(Element element, int minOccurs, int maxOccurs) {
element.setAttributeNS(NAMESPACE, "smk:minOccurs",
Integer.toString(minOccurs)); //$NON-NLS-1$
element.setAttributeNS(NAMESPACE, "smk:maxOccurs",
Integer.toString(maxOccurs)); //$NON-NLS-1$
}
@@ -234,4 +234,33 @@
}
return false;
}
+
+ /**
+ * Turn on/off enforcement of collection sub-mapping rules.
+ * <p/>
+ * If turned on, the collection mapping must be made on the model collection element
(having maxOccurs > 1)
+ * before mappings can be made on sub elements.
+ *
+ * @param element The model element.
+ * @param enforce True if enforcement is to be turned on, otherwise false.
+ */
+ public static void setEnforceCollectionSubMappingRules(Element element, boolean enforce)
{
+ element.setAttributeNS(NAMESPACE,
"smk:enforceCollectionSubMappingRules", Boolean.toString(enforce));
//$NON-NLS-1$
+ }
+
+ /**
+ * Is collection sub-mapping rules turned on for the supplied model element.
+ * @param element The model element.
+ * @return True if enforcement is turned on, otherwise false.
+ * @see #setEnforceCollectionSubMappingRules(Element, boolean)
+ */
+ public static boolean getEnforceCollectionSubMappingRules(Element element) {
+ String enforce = element.getAttributeNS(NAMESPACE,
"enforceCollectionSubMappingRules"); //$NON-NLS-1$
+
+ if(enforce == null || enforce.length() == 0) {
+ return true;
+ }
+
+ return !enforce.equals("false"); //$NON-NLS-1$
+ }
}
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/xml/XMLSampleModelBuilder.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/xml/XMLSampleModelBuilder.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/xml/XMLSampleModelBuilder.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,207 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.tools.smooks.templating.model.xml;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.eclipse.core.runtime.Assert;
+import org.eclipse.emf.common.util.URI;
+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.xml.sax.SAXException;
+import org.jboss.tools.smooks.templating.model.ModelBuilder;
+import org.jboss.tools.smooks.templating.model.ModelBuilderException;
+import org.jboss.tools.smooks.templating.template.xml.XMLFreeMarkerTemplateBuilder;
+import org.milyn.xml.DomUtils;
+
+/**
+ * XML Model Builder from an XML Sample.
+ * <p/>
+ * The generated model can then be used by the {@link XMLFreeMarkerTemplateBuilder}.
+ *
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class XMLSampleModelBuilder extends ModelBuilder {
+
+ private static DocumentBuilder docBuilder;
+ private Document model;
+
+ static {
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+ docBuilderFactory.setNamespaceAware(true);
+ try {
+ docBuilder = docBuilderFactory.newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ throw new RuntimeException("Unexpectd XML exception. Unable to create
DocumentBuilder instance.", e);
+ }
+ }
+
+ public XMLSampleModelBuilder(URI xmlSampleURI) throws IOException,
ModelBuilderException {
+ Assert.isNotNull(xmlSampleURI, "Null 'xmlSampleURI' arg in method
call.");
+
+ File xmlSampleFile = new File(xmlSampleURI.toFileString());
+
+ if(!xmlSampleFile.exists()) {
+ throw new IOException("XML Sample '" +
xmlSampleFile.getAbsolutePath() + "' not found."); //$NON-NLS-1$
+ } else if(!xmlSampleFile.isFile()) {
+ throw new IOException("XML Sample '" +
xmlSampleFile.getAbsolutePath() + "' is not a normal file. Might be a directory
etc."); //$NON-NLS-1$
+ }
+
+ try {
+ model = docBuilder.parse(xmlSampleFile);
+ } catch (SAXException e) {
+ throw new ModelBuilderException("Error parsing XML Sample file.", e);
//$NON-NLS-1$
+ }
+
+ Element documentElement = model.getDocumentElement();
+
+ trimNonModelNodes(documentElement);
+ configureModelElementTypes(documentElement);
+ configureModelElementCardinality(documentElement);
+ registerNamepsaces(documentElement);
+ }
+
+ private void trimNonModelNodes(Element element) {
+ NodeList children = element.getChildNodes();
+ List<Node> removeableChildren = new ArrayList<Node>();
+
+ for(int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+
+ if(child.getNodeType() == Node.ELEMENT_NODE) {
+ trimNonModelNodes((Element) child);
+ } else {
+ removeableChildren.add(child);
+ }
+ }
+
+ for(Node child : removeableChildren) {
+ element.removeChild(child);
+ }
+ }
+
+ private void configureModelElementTypes(Element element) {
+ NodeList children = element.getChildNodes();
+ int childCount = children.getLength();
+
+ if(childCount > 0) {
+ // Has child elements, therefore it's a "complex" element type...
+ ModelBuilder.setElementType(element, ElementType.complex);
+
+ for(int i = 0; i < childCount; i++) {
+ Node child = children.item(i);
+
+ if(child.getNodeType() == Node.ELEMENT_NODE) {
+ configureModelElementTypes((Element) child);
+ } else {
+ throw new IllegalStateException("The configureModelElementTypes method can only
be called after the model has been trimed of non-model Nodes. Call trimNonModelNodes()
before calling configureModelElementTypes().");
+ }
+ }
+ } else {
+ // Has no child elements, therefore it's a "simple" element type...
+ ModelBuilder.setElementType(element, ElementType.simple);
+ }
+ }
+
+ private void configureModelElementCardinality(Element element) {
+ NodeList children = element.getChildNodes();
+ List<Node> removeableChildren = new ArrayList<Node>();
+ int childCount = children.getLength();
+
+ Map<String, Element> childElementByNames = new HashMap<String, Element>();
+ for(int i = 0; i < childCount; i++) {
+ Node child = children.item(i);
+
+ if(child.getNodeType() == Node.ELEMENT_NODE) {
+ Element childElement = (Element) child;
+ String elementName = DomUtils.getName(childElement) + ":" +
childElement.getNamespaceURI(); // Yes, namespace can be null, but that's OK.
+ Element earlierOccurance = childElementByNames.get(elementName);
+
+ // Mark every element as being optional and possibly being multiple...
+ ModelBuilder.setMinMax(childElement, 0, -1);
+
+ if(earlierOccurance != null) {
+ // According to the sample XML, this element is definitely a
+ // collection item because it exists more than once, so lets mark it
+ // such that sub mappings on this element require this collection to be mapped
beforehand...
+ ModelBuilder.setEnforceCollectionSubMappingRules(earlierOccurance, true);
+ // And remove the duplicates...
+ removeableChildren.add(childElement);
+ } else {
+ // We've no way of knowing whether or not this element is a collection
+ // item or not, so lets not enforce the collection sub mapping rules...
+ ModelBuilder.setEnforceCollectionSubMappingRules(childElement, false);
+ }
+
+ configureModelElementCardinality(childElement);
+ } else {
+ throw new IllegalStateException("The configureModelElementTypes method can only
be called after the model has been trimed of non-model Nodes. Call trimNonModelNodes()
before calling configureModelElementTypes().");
+ }
+ }
+
+ for(Node child : removeableChildren) {
+ element.removeChild(child);
+ }
+ }
+
+ private void registerNamepsaces(Element element) {
+ NamedNodeMap attributes = element.getAttributes();
+
+ for(int i = 0; i < attributes.getLength(); i++) {
+ registerNamespace(attributes.item(i));
+ }
+
+ NodeList children = element.getChildNodes();
+ for(int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if(child.getNodeType() == Node.ELEMENT_NODE) {
+ registerNamespace(child);
+ registerNamepsaces((Element) child);
+ }
+ }
+ }
+
+ private void registerNamespace(Node node) {
+ String nsPrefix = node.getPrefix();
+ String nsURI = node.getNamespaceURI();
+
+ if(nsPrefix != null && nsURI != null) {
+ getNamespaces().setProperty(nsPrefix, nsURI);
+ }
+ }
+
+ public Document buildModel() throws ModelBuilderException {
+ return model;
+ }
+}
\ No newline at end of file
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/model/xml/XMLSampleModelBuilder.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/template/TemplateBuilder.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/template/TemplateBuilder.java 2010-01-15
22:44:22 UTC (rev 19785)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.templating/src/org/jboss/tools/smooks/templating/template/TemplateBuilder.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -279,7 +279,7 @@
Element collectionElement = getNearestCollectionElement(mappingNode);
if (collectionElement != null) {
CollectionMapping parentCollectionMapping = getCollectionMapping(collectionElement);
- if (parentCollectionMapping == null) {
+ if (parentCollectionMapping == null &&
ModelBuilder.getEnforceCollectionSubMappingRules(collectionElement)) {
throw new UnmappedCollectionNodeException(collectionElement);
}
}
Modified:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/TestUtil.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/TestUtil.java 2010-01-15
22:44:22 UTC (rev 19785)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/TestUtil.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -33,6 +33,7 @@
import org.custommonkey.xmlunit.XMLUnit;
import org.eclipse.emf.common.util.URI;
import org.jboss.tools.smooks.templating.model.ModelBuilderException;
+import org.jboss.tools.smooks.templating.model.xml.XMLSampleModelBuilder;
import org.jboss.tools.smooks.templating.model.xml.XSDModelBuilder;
import org.jboss.tools.smooks.templating.template.xml.XMLFreeMarkerTemplateBuilder;
import org.milyn.io.StreamUtils;
@@ -87,4 +88,14 @@
modelBuilder.setRootElementName(rootElementName);
return new XMLFreeMarkerTemplateBuilder(modelBuilder);
}
+
+ public static TemplateBuilder createXMLSampleFreeMarkerTemplateBuilder(File
xmlSampleFile) throws IOException, ModelBuilderException {
+ if(!xmlSampleFile.exists()) {
+ throw new RuntimeException("Unknown: " +
xmlSampleFile.getAbsolutePath());
+ }
+
+ URI fileURI = URI.createFileURI(xmlSampleFile.getCanonicalFile().toString());
+
+ return new XMLFreeMarkerTemplateBuilder(new XMLSampleModelBuilder(fileURI));
+ }
}
Deleted:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLFreeMarkerTemplateBuilderTest.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLFreeMarkerTemplateBuilderTest.java 2010-01-15
22:44:22 UTC (rev 19785)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLFreeMarkerTemplateBuilderTest.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -1,279 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2006, JBoss Inc., and others contributors as indicated
- * by the @authors tag. All rights reserved.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2005-2006, JBoss Inc.
- */
-package org.jboss.tools.smooks.templating.template.xml;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.xpath.XPathExpressionException;
-
-import junit.framework.TestCase;
-
-import org.eclipse.emf.common.util.URI;
-import org.jboss.tools.smooks.templating.model.ModelBuilderException;
-import org.jboss.tools.smooks.templating.model.xml.XSDModelBuilder;
-import org.jboss.tools.smooks.templating.template.Mapping;
-import org.jboss.tools.smooks.templating.template.TemplateBuilder;
-import org.jboss.tools.smooks.templating.template.TestUtil;
-import org.jboss.tools.smooks.templating.template.exception.TemplateBuilderException;
-import
org.jboss.tools.smooks.templating.template.exception.UnmappedCollectionNodeException;
-import org.milyn.util.FreeMarkerTemplate;
-import org.milyn.xml.XmlUtil;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
-
-/**
- * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
- */
-public class XMLFreeMarkerTemplateBuilderTest extends TestCase {
-
- public void test_SimplePerson_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
- SAXException, ModelBuilderException {
- TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
- "src/org/jboss/tools/smooks/templating/template/xml/simple-person.xsd"),
"person");
- String theTemplate = builder.buildTemplate();
-
- TestUtil.assertXMLEquals(theTemplate, "expected/simple-person-01.xml",
getClass());
-
- // Now, test that we can parse the template ....
- XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
- assertEquals(theTemplate, builder2.buildTemplate());
- }
-
- public void test_SimplePerson_02() throws IOException, ParserConfigurationException,
TemplateBuilderException,
- SAXException, ModelBuilderException, XPathExpressionException {
- TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
- "src/org/jboss/tools/smooks/templating/template/xml/simple-person.xsd"),
"person");
-
- builder.addValueMapping("male",
builder.getModelNode("ns0:person/name/smk:compositor/male"));
- builder.addValueMapping("firstName",
builder.getModelNode("ns0:person/name/@first"));
- builder.addValueMapping("lastName",
builder.getModelNode("ns0:person/name/@last"));
- builder.addValueMapping("address",
builder.getModelNode("ns0:person/address"));
- builder.addValueMapping("age",
builder.getModelNode("ns0:person/age"));
-
- Map javaCtx = createPersonObj("Tom", "Fennelly",
"Ireland", 21);
- String template = builder.buildTemplate();
- // System.out.println(template);
- FreeMarkerTemplate freemarker = new FreeMarkerTemplate(template);
- String templateRes = freemarker.apply(javaCtx);
-
- TestUtil.assertXMLEquals(templateRes, "expected/simple-person-02.xml",
getClass());
- TestUtil.validate(builder, templateRes);
-
- // Now, test that we can parse the template and extract the mappings
- // from it....
- XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), template);
- assertEquals(5, builder2.getMappings().size());
- assertEquals(template, builder2.buildTemplate());
- }
-
- public void test_people_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
- SAXException, ModelBuilderException, XPathExpressionException {
- TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
- "src/org/jboss/tools/smooks/templating/template/xml/people.xsd"),
"people");
-
- // Try adding a value mapping before adding the collection mapping.
- // Should get an exception...
- try {
- builder.addValueMapping("personObj.firtName",
builder.getModelNode("ns0:people/person/name/@first"));
- fail("Expected UnmappedCollectionNodeException");
- } catch (UnmappedCollectionNodeException e) {
- assertEquals("Unmapped collection node 'person'.", e.getMessage());
- }
- }
-
- public void test_people_02() throws IOException, ParserConfigurationException,
TemplateBuilderException,
- SAXException, ModelBuilderException, XPathExpressionException {
- TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
- "src/org/jboss/tools/smooks/templating/template/xml/people.xsd"),
"people");
-
- builder.addCollectionMapping("people", (Element)
builder.getModelNode("ns0:people/person"), "personObj");
- builder.addValueMapping("personObj.male",
builder.getModelNode("ns0:people/person/name/smk:compositor/male"));
- builder.addValueMapping("personObj.firstName",
builder.getModelNode("ns0:people/person/name/@first"));
- builder.addValueMapping("personObj.lastName",
builder.getModelNode("ns0:people/person/name/@last"));
- builder.addValueMapping("personObj.address",
builder.getModelNode("ns0:people/person/address"));
- builder.addValueMapping("personObj.age",
builder.getModelNode("ns0:people/person/age"));
-
- assertEquals(6, builder.getMappings().size());
-
- Map javaCtx = new HashMap();
- List people = new ArrayList();
- people.add(createPersonObj("Tom", "Fennelly", "Ireland",
6));
- people.add(createPersonObj("Mike", "Fennelly", "Ireland",
5));
- javaCtx.put("people", people);
-
- String template = builder.buildTemplate();
-
- FreeMarkerTemplate freemarker = new FreeMarkerTemplate(template);
- String templateRes = freemarker.apply(javaCtx);
-
- TestUtil.assertXMLEquals(templateRes, "expected/people-01.xml", getClass());
-
- // Now, test that we can parse the template and extract the mappings
- // from it....
- XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), template);
- assertEquals(6, builder2.getMappings().size());
- assertEquals(template, builder2.buildTemplate());
- }
-
- public void test_elementname() throws IOException, ModelBuilderException {
- File xsdFile = new
File("src/org/jboss/tools/smooks/templating/template/xml/smooks1_0.xsd");
- URI uri = URI.createFileURI(xsdFile.getCanonicalFile().toString());
- XSDModelBuilder builder = new XSDModelBuilder(uri);
- builder.setRootElementName("smooks-resource-list");
- XMLFreeMarkerTemplateBuilder templateBuilder = new
XMLFreeMarkerTemplateBuilder(builder);
- Document document = templateBuilder.getModel();
-
- StringWriter writer = new StringWriter();
- XmlUtil.serialize(document, true, writer);
- System.out.println(writer);
- //checkNodeName(document.getDocumentElement());
- }
-
- private void checkNodeName(Node node) {
- if (node instanceof Element) {
- assertNotNull(((Element)node).getTagName());
- }
- System.out.println(node.getNodeName());
- assertTrue(node.getNodeName().indexOf("null") == -1);
- NodeList nodeList = node.getChildNodes();
- int length = nodeList.getLength();
- for (int i = 0; i < length; i++) {
- checkNodeName(nodeList.item(i));
- }
- }
-
- public void test_creature_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
- SAXException, ModelBuilderException, XPathExpressionException {
- TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
- "src/org/jboss/tools/smooks/templating/template/xml/creature.xsd"),
"creature");
-
- Mapping mapping = builder.addValueMapping("dog",
builder.getModelNode("ns0:creature/smk:compositor/dog/@name"));
-
- // The mapping onto the <dog> node should result in the <person> and
- // <cat> nodes being hidden...
- assertEquals(2, mapping.getHideNodes().size());
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
mapping.getHideNodes().get(0));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
mapping.getHideNodes().get(1));
-
- String theTemplate = builder.buildTemplate();
- TestUtil.assertXMLEquals(theTemplate, "expected/creature-01.xml",
getClass());
- TestUtil.validate(builder, theTemplate);
-
- // Now, test that we can parse the template and extract the mappings
- // from it....
- XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
- assertEquals(theTemplate, builder2.buildTemplate());
-
- // ========================================================
-
- // Remove the <dog> mapping... should reshow the <person> and <cat>
- // nodes...
- List<Node> showNodes = builder.removeMapping(mapping);
- assertEquals(2, showNodes.size());
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
showNodes.get(0));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
showNodes.get(1));
-
- mapping = builder.addValueMapping("cat",
builder.getModelNode("ns0:creature/smk:compositor/cat"));
-
- // The mapping onto the <cat> node should result in the <person> and
- // <dog> nodes being hidden...
- assertEquals(2, mapping.getHideNodes().size());
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
mapping.getHideNodes().get(0));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
mapping.getHideNodes().get(1));
-
- theTemplate = builder.buildTemplate();
- TestUtil.assertXMLEquals(theTemplate, "expected/creature-02.xml",
getClass());
- TestUtil.validate(builder, theTemplate);
-
- // Now, test that we can parse the template and extract the mappings
- // from it....
- builder2 = new XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
- assertEquals(theTemplate, builder2.buildTemplate());
-
- // ========================================================
-
- // Remove the <cat> mapping... should reshow the <person> and <dog>
- // nodes...
- showNodes = builder.removeMapping(mapping);
- assertEquals(2, showNodes.size());
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
showNodes.get(0));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
showNodes.get(1));
-
- // Map the person... there are nested choice compositors on this one...
- mapping = builder.addValueMapping("male", builder
- .getModelNode("ns0:creature/smk:compositor/person/name/smk:compositor/male"));
- builder.addValueMapping("firstName",
builder.getModelNode("ns0:creature/smk:compositor/person/name/@first"));
- builder.addValueMapping("lastName",
builder.getModelNode("ns0:creature/smk:compositor/person/name/@last"));
- builder.addValueMapping("address",
builder.getModelNode("ns0:creature/smk:compositor/person/address"));
- builder.addValueMapping("age",
builder.getModelNode("ns0:creature/smk:compositor/person/age"));
-
- // The mapping onto the <person> node should result in the <female>,
- // <dog> and <cat> nodes being hidden...
- assertEquals(3, mapping.getHideNodes().size());
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person/name/smk:compositor/female"),
mapping
- .getHideNodes().get(0));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
mapping.getHideNodes().get(1));
- assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
mapping.getHideNodes().get(2));
-
- theTemplate = builder.buildTemplate();
- TestUtil.assertXMLEquals(theTemplate, "expected/creature-03.xml",
getClass());
-
- // Now, test that we can parse the template and extract the mappings
- // from it....
- builder2 = new XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
- assertEquals(theTemplate, builder2.buildTemplate());
- }
-
- // public void test_OrderStatusRequest() throws IOException,
- // ParserConfigurationException, ModelBuilderException, SAXException,
- // TemplateBuilderException {
- // TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new
- //
File("resources/xcbl40/schema/org/xcbl/path_delim/schemas/xcbl/v4_0/ordermanagement/v1_0/ordermanagement.xsd"),
- // "OrderStatusResult");
- // String theTemplate = builder.buildTemplate();
- //
- // TestUtil.assertXMLEquals(theTemplate, "expected/order-status-01.xml",
- // getClass());
- // }
-
- private Map createPersonObj(String first, String last, String address, int age) {
- Map javaCtx = new HashMap();
- javaCtx.put("male", true);
- javaCtx.put("firstName", first);
- javaCtx.put("lastName", last);
- javaCtx.put("address", address);
- javaCtx.put("age", age);
- return javaCtx;
- }
-
- private void printModel(TemplateBuilder builder) {
- System.out.println(XmlUtil.serialize(builder.getModel(), true));
- }
-}
Added:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLSample_XMLFreeMarkerTemplateBuilderTest.java
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLSample_XMLFreeMarkerTemplateBuilderTest.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLSample_XMLFreeMarkerTemplateBuilderTest.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.tools.smooks.templating.template.xml;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathExpressionException;
+
+import junit.framework.TestCase;
+
+import org.jboss.tools.smooks.templating.model.ModelBuilderException;
+import org.jboss.tools.smooks.templating.template.TemplateBuilder;
+import org.jboss.tools.smooks.templating.template.TestUtil;
+import org.jboss.tools.smooks.templating.template.exception.TemplateBuilderException;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class XMLSample_XMLFreeMarkerTemplateBuilderTest extends TestCase {
+
+ public void test_XML_Order_01() throws IOException, ParserConfigurationException,
TemplateBuilderException, SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXMLSampleFreeMarkerTemplateBuilder(new
File("src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml"));
+
+ Node identNode =
builder.getModelNode("ns0:OrderStatusResult/ns0:OrderStatusResultHeader/ns0:BuyerParty/ns1:PartyID/ns1:Ident");
+ builder.addValueMapping("order.status.id", identNode);
+
+ String theTemplate = builder.buildTemplate();
+
+// System.out.println(theTemplate);
+ TestUtil.assertXMLEquals(theTemplate,
"expected/order-status-expected-01.xml", getClass());
+
+ // Now, test that we can parse the template ....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+ }
+
+ public void test_XML_Order_02() throws IOException, ParserConfigurationException,
TemplateBuilderException, SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXMLSampleFreeMarkerTemplateBuilder(new
File("src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml"));
+
+ Node identNode =
builder.getModelNode("ns0:OrderStatusResult/ns0:OrderStatusResultHeader/ns0:BuyerParty/ns1:PartyID/ns1:Ident");
+ Element resultStatusCollectionNode = (Element)
builder.getModelNode("ns0:OrderStatusResult/ns0:ListOfOrderStatusResultDetail/ns0:OrderStatusResultDetail");
+ Node buyerRefNumberNode =
builder.getModelNode("ns0:OrderStatusResult/ns0:ListOfOrderStatusResultDetail/ns0:OrderStatusResultDetail/ns0:OrderStatusResultReference/ns0:BuyerReferenceNumber");
+
+ builder.addValueMapping("order.status.id", identNode);
+ builder.addCollectionMapping("order.orderItemsStatusList",
resultStatusCollectionNode, "itemStatus");
+ builder.addValueMapping("itemStatus.buyerRefNumber", buyerRefNumberNode);
+
+ String theTemplate = builder.buildTemplate();
+
+// System.out.println(theTemplate);
+ TestUtil.assertEquals(theTemplate, "expected/order-status-expected-02.xml",
getClass());
+
+ // Now, test that we can parse the template ....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+ }
+}
Property changes on:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLSample_XMLFreeMarkerTemplateBuilderTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XSD_XMLFreeMarkerTemplateBuilderTest.java
(from rev 19766,
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XMLFreeMarkerTemplateBuilderTest.java)
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XSD_XMLFreeMarkerTemplateBuilderTest.java
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XSD_XMLFreeMarkerTemplateBuilderTest.java 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,279 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2006, JBoss Inc., and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2006, JBoss Inc.
+ */
+package org.jboss.tools.smooks.templating.template.xml;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPathExpressionException;
+
+import junit.framework.TestCase;
+
+import org.eclipse.emf.common.util.URI;
+import org.jboss.tools.smooks.templating.model.ModelBuilderException;
+import org.jboss.tools.smooks.templating.model.xml.XSDModelBuilder;
+import org.jboss.tools.smooks.templating.template.Mapping;
+import org.jboss.tools.smooks.templating.template.TemplateBuilder;
+import org.jboss.tools.smooks.templating.template.TestUtil;
+import org.jboss.tools.smooks.templating.template.exception.TemplateBuilderException;
+import
org.jboss.tools.smooks.templating.template.exception.UnmappedCollectionNodeException;
+import org.milyn.util.FreeMarkerTemplate;
+import org.milyn.xml.XmlUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+/**
+ * @author <a
href="mailto:tom.fennelly@jboss.com">tom.fennelly@jboss.com</a>
+ */
+public class XSD_XMLFreeMarkerTemplateBuilderTest extends TestCase {
+
+ public void test_SimplePerson_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
+ SAXException, ModelBuilderException {
+ TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
+ "src/org/jboss/tools/smooks/templating/template/xml/simple-person.xsd"),
"person");
+ String theTemplate = builder.buildTemplate();
+
+ TestUtil.assertXMLEquals(theTemplate, "expected/simple-person-01.xml",
getClass());
+
+ // Now, test that we can parse the template ....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+ }
+
+ public void test_SimplePerson_02() throws IOException, ParserConfigurationException,
TemplateBuilderException,
+ SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
+ "src/org/jboss/tools/smooks/templating/template/xml/simple-person.xsd"),
"person");
+
+ builder.addValueMapping("male",
builder.getModelNode("ns0:person/name/smk:compositor/male"));
+ builder.addValueMapping("firstName",
builder.getModelNode("ns0:person/name/@first"));
+ builder.addValueMapping("lastName",
builder.getModelNode("ns0:person/name/@last"));
+ builder.addValueMapping("address",
builder.getModelNode("ns0:person/address"));
+ builder.addValueMapping("age",
builder.getModelNode("ns0:person/age"));
+
+ Map javaCtx = createPersonObj("Tom", "Fennelly",
"Ireland", 21);
+ String template = builder.buildTemplate();
+ // System.out.println(template);
+ FreeMarkerTemplate freemarker = new FreeMarkerTemplate(template);
+ String templateRes = freemarker.apply(javaCtx);
+
+ TestUtil.assertXMLEquals(templateRes, "expected/simple-person-02.xml",
getClass());
+ TestUtil.validate(builder, templateRes);
+
+ // Now, test that we can parse the template and extract the mappings
+ // from it....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), template);
+ assertEquals(5, builder2.getMappings().size());
+ assertEquals(template, builder2.buildTemplate());
+ }
+
+ public void test_people_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
+ SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
+ "src/org/jboss/tools/smooks/templating/template/xml/people.xsd"),
"people");
+
+ // Try adding a value mapping before adding the collection mapping.
+ // Should get an exception...
+ try {
+ builder.addValueMapping("personObj.firtName",
builder.getModelNode("ns0:people/person/name/@first"));
+ fail("Expected UnmappedCollectionNodeException");
+ } catch (UnmappedCollectionNodeException e) {
+ assertEquals("Unmapped collection node 'person'.", e.getMessage());
+ }
+ }
+
+ public void test_people_02() throws IOException, ParserConfigurationException,
TemplateBuilderException,
+ SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
+ "src/org/jboss/tools/smooks/templating/template/xml/people.xsd"),
"people");
+
+ builder.addCollectionMapping("people", (Element)
builder.getModelNode("ns0:people/person"), "personObj");
+ builder.addValueMapping("personObj.male",
builder.getModelNode("ns0:people/person/name/smk:compositor/male"));
+ builder.addValueMapping("personObj.firstName",
builder.getModelNode("ns0:people/person/name/@first"));
+ builder.addValueMapping("personObj.lastName",
builder.getModelNode("ns0:people/person/name/@last"));
+ builder.addValueMapping("personObj.address",
builder.getModelNode("ns0:people/person/address"));
+ builder.addValueMapping("personObj.age",
builder.getModelNode("ns0:people/person/age"));
+
+ assertEquals(6, builder.getMappings().size());
+
+ Map javaCtx = new HashMap();
+ List people = new ArrayList();
+ people.add(createPersonObj("Tom", "Fennelly", "Ireland",
6));
+ people.add(createPersonObj("Mike", "Fennelly", "Ireland",
5));
+ javaCtx.put("people", people);
+
+ String template = builder.buildTemplate();
+
+ FreeMarkerTemplate freemarker = new FreeMarkerTemplate(template);
+ String templateRes = freemarker.apply(javaCtx);
+
+ TestUtil.assertXMLEquals(templateRes, "expected/people-01.xml", getClass());
+
+ // Now, test that we can parse the template and extract the mappings
+ // from it....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), template);
+ assertEquals(6, builder2.getMappings().size());
+ assertEquals(template, builder2.buildTemplate());
+ }
+
+ public void test_elementname() throws IOException, ModelBuilderException {
+ File xsdFile = new
File("src/org/jboss/tools/smooks/templating/template/xml/smooks1_0.xsd");
+ URI uri = URI.createFileURI(xsdFile.getCanonicalFile().toString());
+ XSDModelBuilder builder = new XSDModelBuilder(uri);
+ builder.setRootElementName("smooks-resource-list");
+ XMLFreeMarkerTemplateBuilder templateBuilder = new
XMLFreeMarkerTemplateBuilder(builder);
+ Document document = templateBuilder.getModel();
+
+ StringWriter writer = new StringWriter();
+ XmlUtil.serialize(document, true, writer);
+ System.out.println(writer);
+ //checkNodeName(document.getDocumentElement());
+ }
+
+ private void checkNodeName(Node node) {
+ if (node instanceof Element) {
+ assertNotNull(((Element)node).getTagName());
+ }
+ System.out.println(node.getNodeName());
+ assertTrue(node.getNodeName().indexOf("null") == -1);
+ NodeList nodeList = node.getChildNodes();
+ int length = nodeList.getLength();
+ for (int i = 0; i < length; i++) {
+ checkNodeName(nodeList.item(i));
+ }
+ }
+
+ public void test_creature_01() throws IOException, ParserConfigurationException,
TemplateBuilderException,
+ SAXException, ModelBuilderException, XPathExpressionException {
+ TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new File(
+ "src/org/jboss/tools/smooks/templating/template/xml/creature.xsd"),
"creature");
+
+ Mapping mapping = builder.addValueMapping("dog",
builder.getModelNode("ns0:creature/smk:compositor/dog/@name"));
+
+ // The mapping onto the <dog> node should result in the <person> and
+ // <cat> nodes being hidden...
+ assertEquals(2, mapping.getHideNodes().size());
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
mapping.getHideNodes().get(0));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
mapping.getHideNodes().get(1));
+
+ String theTemplate = builder.buildTemplate();
+ TestUtil.assertXMLEquals(theTemplate, "expected/creature-01.xml",
getClass());
+ TestUtil.validate(builder, theTemplate);
+
+ // Now, test that we can parse the template and extract the mappings
+ // from it....
+ XMLFreeMarkerTemplateBuilder builder2 = new
XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+
+ // ========================================================
+
+ // Remove the <dog> mapping... should reshow the <person> and <cat>
+ // nodes...
+ List<Node> showNodes = builder.removeMapping(mapping);
+ assertEquals(2, showNodes.size());
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
showNodes.get(0));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
showNodes.get(1));
+
+ mapping = builder.addValueMapping("cat",
builder.getModelNode("ns0:creature/smk:compositor/cat"));
+
+ // The mapping onto the <cat> node should result in the <person> and
+ // <dog> nodes being hidden...
+ assertEquals(2, mapping.getHideNodes().size());
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
mapping.getHideNodes().get(0));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
mapping.getHideNodes().get(1));
+
+ theTemplate = builder.buildTemplate();
+ TestUtil.assertXMLEquals(theTemplate, "expected/creature-02.xml",
getClass());
+ TestUtil.validate(builder, theTemplate);
+
+ // Now, test that we can parse the template and extract the mappings
+ // from it....
+ builder2 = new XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+
+ // ========================================================
+
+ // Remove the <cat> mapping... should reshow the <person> and <dog>
+ // nodes...
+ showNodes = builder.removeMapping(mapping);
+ assertEquals(2, showNodes.size());
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
showNodes.get(0));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person"),
showNodes.get(1));
+
+ // Map the person... there are nested choice compositors on this one...
+ mapping = builder.addValueMapping("male", builder
+ .getModelNode("ns0:creature/smk:compositor/person/name/smk:compositor/male"));
+ builder.addValueMapping("firstName",
builder.getModelNode("ns0:creature/smk:compositor/person/name/@first"));
+ builder.addValueMapping("lastName",
builder.getModelNode("ns0:creature/smk:compositor/person/name/@last"));
+ builder.addValueMapping("address",
builder.getModelNode("ns0:creature/smk:compositor/person/address"));
+ builder.addValueMapping("age",
builder.getModelNode("ns0:creature/smk:compositor/person/age"));
+
+ // The mapping onto the <person> node should result in the <female>,
+ // <dog> and <cat> nodes being hidden...
+ assertEquals(3, mapping.getHideNodes().size());
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/person/name/smk:compositor/female"),
mapping
+ .getHideNodes().get(0));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/dog"),
mapping.getHideNodes().get(1));
+ assertEquals(builder.getModelNode("ns0:creature/smk:compositor/cat"),
mapping.getHideNodes().get(2));
+
+ theTemplate = builder.buildTemplate();
+ TestUtil.assertXMLEquals(theTemplate, "expected/creature-03.xml",
getClass());
+
+ // Now, test that we can parse the template and extract the mappings
+ // from it....
+ builder2 = new XMLFreeMarkerTemplateBuilder(builder.getModelBuilder(), theTemplate);
+ assertEquals(theTemplate, builder2.buildTemplate());
+ }
+
+ // public void test_OrderStatusRequest() throws IOException,
+ // ParserConfigurationException, ModelBuilderException, SAXException,
+ // TemplateBuilderException {
+ // TemplateBuilder builder = TestUtil.createXSDFreeMarkerTemplateBuilder(new
+ //
File("resources/xcbl40/schema/org/xcbl/path_delim/schemas/xcbl/v4_0/ordermanagement/v1_0/ordermanagement.xsd"),
+ // "OrderStatusResult");
+ // String theTemplate = builder.buildTemplate();
+ //
+ // TestUtil.assertXMLEquals(theTemplate, "expected/order-status-01.xml",
+ // getClass());
+ // }
+
+ private Map createPersonObj(String first, String last, String address, int age) {
+ Map javaCtx = new HashMap();
+ javaCtx.put("male", true);
+ javaCtx.put("firstName", first);
+ javaCtx.put("lastName", last);
+ javaCtx.put("address", address);
+ javaCtx.put("age", age);
+ return javaCtx;
+ }
+
+ private void printModel(TemplateBuilder builder) {
+ System.out.println(XmlUtil.serialize(builder.getModel(), true));
+ }
+}
Property changes on:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/XSD_XMLFreeMarkerTemplateBuilderTest.java
___________________________________________________________________
Name: svn:executable
+ *
Added:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-01.xml
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-01.xml
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-01.xml 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ns0:OrderStatusResult
xmlns:ns0="rrn:org.xcbl:schemas/xcbl/v4_0/ordermanagement/v1_0/ordermanagement.xsd">
+ <ns0:OrderStatusResultHeader>
+ <ns0:BuyerParty>
+ <ns1:PartyID
xmlns:ns1="rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd">
+ <ns1:Ident>${order.status.id?string}</ns1:Ident>
+ </ns1:PartyID>
+ </ns0:BuyerParty>
+ </ns0:OrderStatusResultHeader>
+</ns0:OrderStatusResult>
\ No newline at end of file
Added:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-02.xml
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-02.xml
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/expected/order-status-expected-02.xml 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ns0:OrderStatusResult
xmlns:ns0="rrn:org.xcbl:schemas/xcbl/v4_0/ordermanagement/v1_0/ordermanagement.xsd">
+ <ns0:OrderStatusResultHeader>
+ <ns0:BuyerParty>
+ <ns1:PartyID
xmlns:ns1="rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd">
+ <ns1:Ident>${order.status.id?string}</ns1:Ident>
+ </ns1:PartyID>
+ </ns0:BuyerParty>
+ </ns0:OrderStatusResultHeader>
+ <ns0:ListOfOrderStatusResultDetail>
+ <#list order.orderItemsStatusList as itemStatus>
+ <ns0:OrderStatusResultDetail>
+ <ns0:OrderStatusResultReference>
+
<ns0:BuyerReferenceNumber>${itemStatus.buyerRefNumber?string}</ns0:BuyerReferenceNumber>
+ </ns0:OrderStatusResultReference>
+ </ns0:OrderStatusResultDetail>
+ </#list>
+ </ns0:ListOfOrderStatusResultDetail>
+</ns0:OrderStatusResult>
\ No newline at end of file
Added:
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml
===================================================================
---
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml
(rev 0)
+++
trunk/smooks/tests/org.jboss.tools.smooks.templating.test/src/org/jboss/tools/smooks/templating/template/xml/order-status-01.xml 2010-01-17
09:06:10 UTC (rev 19786)
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ns0:OrderStatusResult
xmlns:ns0="rrn:org.xcbl:schemas/xcbl/v4_0/ordermanagement/v1_0/ordermanagement.xsd">
+ <ns0:OrderStatusResultHeader>
+ <ns0:OrderStatusID></ns0:OrderStatusID>
+ <ns0:BuyerParty>
+ <ns1:PartyID
xmlns:ns1="rrn:org.xcbl:schemas/xcbl/v4_0/core/core.xsd">
+ <ns1:Ident></ns1:Ident>
+ </ns1:PartyID>
+ </ns0:BuyerParty>
+ </ns0:OrderStatusResultHeader>
+ <ns0:ListOfOrderStatusResultDetail>
+ <ns0:OrderStatusResultDetail>
+ <ns0:OrderStatusResultReference>
+ <ns0:BuyerReferenceNumber></ns0:BuyerReferenceNumber>
+ </ns0:OrderStatusResultReference>
+ </ns0:OrderStatusResultDetail>
+ </ns0:ListOfOrderStatusResultDetail>
+</ns0:OrderStatusResult>
\ No newline at end of file