001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.edl.impl;
017
018 import static org.junit.Assert.assertTrue;
019
020 import javax.xml.parsers.DocumentBuilder;
021 import javax.xml.parsers.DocumentBuilderFactory;
022 import javax.xml.transform.Source;
023 import javax.xml.transform.Templates;
024 import javax.xml.transform.Transformer;
025 import javax.xml.transform.TransformerFactory;
026 import javax.xml.transform.stream.StreamResult;
027 import javax.xml.transform.stream.StreamSource;
028 import javax.xml.xpath.XPath;
029 import javax.xml.xpath.XPathConstants;
030 import javax.xml.xpath.XPathFactory;
031
032 import org.apache.log4j.Logger;
033 import org.junit.Ignore;
034 import org.junit.Test;
035 import org.kuali.rice.kew.test.KEWTestCase;
036 import org.kuali.rice.kew.xml.XPathTest;
037 import org.w3c.dom.Document;
038 import org.w3c.dom.Node;
039 import org.w3c.dom.NodeList;
040
041 /**
042 * This class is used to test edoc lite xml xpath operations
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 *
046 */
047 @Ignore
048 public class EDocLiteXPathTest extends KEWTestCase {
049 private static final Logger LOG = Logger.getLogger(XPathTest.class);
050
051 private static final String STYLESHEET_RESOURCE = "org/kuali/rice/kew/edoclite/DefaultStyle.xsl";
052 private static final String INITIAL_EDOC_XML = "initial_edldoc.xml";
053 private static final String SAMPLE_EDOC_XML = "sample_edldoc.xml";
054
055 @Test public void testTransformInitialDoc() throws Exception {
056 TransformerFactory factory = TransformerFactory.newInstance();
057 Source styleSheet = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(STYLESHEET_RESOURCE));
058 Templates templates = templates = factory.newTemplates(styleSheet);
059 Transformer transformer = templates.newTransformer();
060 transformer.setOutputProperty("indent", "yes");
061 transformer.setParameter("readOnly", "false");
062 //transformer.setParameter("docType", docType);
063 //transformer.setParameter("schema", schema);
064
065 Source input = new StreamSource(this.getClass().getResourceAsStream(INITIAL_EDOC_XML));
066 transformer.transform(input, new StreamResult(System.out));
067 }
068
069 @Test public void testFieldHasMatchingUserValues() throws Exception {
070 LOG.info("testFieldHasMatchingUserValues");
071 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
072
073 XPath xpath = XPathFactory.newInstance().newXPath();
074 Document doc = db.parse(this.getClass().getResourceAsStream(SAMPLE_EDOC_XML));
075 // enumerate all fields
076 final String fieldDefs = "/edlContent/edl/field/display/values";
077 NodeList nodes = (NodeList) xpath.evaluate(fieldDefs, doc, XPathConstants.NODESET);
078
079 for (int i = 0; i < nodes.getLength(); i++) {
080 Node node = nodes.item(i);
081 String name = (String) xpath.evaluate("../../@name", node, XPathConstants.STRING);
082 LOG.debug("Name: " + name);
083 LOG.debug("Value: " + node.getFirstChild().getNodeValue());
084 final String expr = "/edlContent/data/version[@current='true']/fieldEntry[@name=current()/../../@name and value=current()]";
085 NodeList matchingUserValues = (NodeList) xpath.evaluate(expr, node, XPathConstants.NODESET);
086 LOG.debug(matchingUserValues + "");
087 LOG.debug(matchingUserValues.getLength() + "");
088 if ("gender".equals(name)) {
089 assertTrue("Matching values > 0", matchingUserValues.getLength() > 0);
090 }
091 for (int j = 0; j < matchingUserValues.getLength(); j++) {
092 LOG.debug(matchingUserValues.item(j).getFirstChild().getNodeValue());
093 }
094 }
095 }
096
097 /*
098 @Test public void testUpdateEDLDocument() throws Exception {
099 final Map params = new HashMap();
100 params.put("givenname", new String[] { "Frank" });
101 params.put("surname", new String[] { "Miller" });
102 params.put("email", new String[] { "frank@bogus.blah.asdlajsd.co.uk" });
103 params.put("gender", new String[] { "male" });
104 params.put("color", new String[] { "blue" });
105 params.put("food", new String[] { "sandwiches", "soup" });
106
107 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
108
109 XPath xpath = XPathFactory.newInstance().newXPath();
110 String versionsExpression = "/edlContent/data/version";
111
112 // try an initial empty doc
113 EDLDocument edlDoc = new EDLDocument(db.parse(this.getClass().getResourceAsStream(INITIAL_EDOC_XML)));
114 int numVersionsBefore = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
115 LOG.debug("Initial before:");
116 LOG.debug(edlDoc);
117 edlDoc.update(null, params);
118 LOG.debug("Initial after:");
119 LOG.debug(edlDoc);
120 int numVersionsAfter = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
121 assertEquals(numVersionsBefore + 1, numVersionsAfter);
122
123 numVersionsBefore = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
124 LOG.debug("Initial 2nd time before:");
125 LOG.debug(edlDoc);
126 edlDoc.update(null, params);
127 LOG.debug("Initial 2nd time after:");
128 LOG.debug(edlDoc);
129 numVersionsAfter = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
130 assertEquals(numVersionsBefore + 1, numVersionsAfter);
131
132 // try a sample doc
133 edlDoc = new EDLDocument(db.parse(this.getClass().getResourceAsStream(SAMPLE_EDOC_XML)));
134 numVersionsBefore = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
135 LOG.debug("Sample before:");
136 LOG.debug(edlDoc);
137 edlDoc.update(null, params);
138 LOG.debug("Sample after:");
139 LOG.debug(edlDoc);
140 numVersionsAfter = ((NodeList) xpath.evaluate(versionsExpression, edlDoc.getDocument(), XPathConstants.NODESET)).getLength();
141 assertEquals(numVersionsBefore + 1, numVersionsAfter);
142 }
143
144 @Test public void testXPathStuff() throws Exception {
145 InputStream edlDocContent = new TestUtilities().loadResource(this.getClass(), "edldoccontent.xml");
146 org.w3c.dom.Document w3cDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(edlDocContent);
147 // Document document = new DOMBuilder().build(w3cDocument);
148 // DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
149 // Document routeDocument = builder.parse(new File("ParallelRouting.xml"));
150
151 XPath xpath = XPathFactory.newInstance().newXPath();
152 xpath.getXPathFunctionResolver();
153 // String expression = "//version[@current='true']/fieldEntry[@name='name']/value";
154 // xpath.getXPathFunctionResolver().resolveFunction();s
155 String expression = "//version[@current='true']/fieldEntry[@name=concat('n', 'ame')]/value";
156 String expression2 = "local-name(//field[@name='name']/@name)";
157 String expression3 = "//version[@current='true']/fieldEntry[@name=local-name(//field[@name='name']/@name)]/value";
158 Node node = (Node) xpath.evaluate(expression3, w3cDocument, XPathConstants.NODE);
159 xpath.evaluate(expression3, w3cDocument);
160 node.getNodeValue();
161 node.getNodeType();
162 ((Text)node.getFirstChild()).getNodeValue();
163 int i =1;
164 }
165 */
166 }