1 /** 2 * Copyright 2005-2011 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.rice.edl.impl; 17 18 import static org.junit.Assert.assertTrue; 19 20 import javax.xml.parsers.DocumentBuilder; 21 import javax.xml.parsers.DocumentBuilderFactory; 22 import javax.xml.transform.Source; 23 import javax.xml.transform.Templates; 24 import javax.xml.transform.Transformer; 25 import javax.xml.transform.TransformerFactory; 26 import javax.xml.transform.stream.StreamResult; 27 import javax.xml.transform.stream.StreamSource; 28 import javax.xml.xpath.XPath; 29 import javax.xml.xpath.XPathConstants; 30 import javax.xml.xpath.XPathFactory; 31 32 import org.apache.log4j.Logger; 33 import org.junit.Ignore; 34 import org.junit.Test; 35 import org.kuali.rice.kew.test.KEWTestCase; 36 import org.kuali.rice.kew.xml.XPathTest; 37 import org.w3c.dom.Document; 38 import org.w3c.dom.Node; 39 import org.w3c.dom.NodeList; 40 41 /** 42 * This class is used to test edoc lite xml xpath operations 43 * 44 * @author Kuali Rice Team (rice.collab@kuali.org) 45 * 46 */ 47 @Ignore 48 public class EDocLiteXPathTest extends KEWTestCase { 49 private static final Logger LOG = Logger.getLogger(XPathTest.class); 50 51 private static final String STYLESHEET_RESOURCE = "org/kuali/rice/kew/edoclite/DefaultStyle.xsl"; 52 private static final String INITIAL_EDOC_XML = "initial_edldoc.xml"; 53 private static final String SAMPLE_EDOC_XML = "sample_edldoc.xml"; 54 55 @Test public void testTransformInitialDoc() throws Exception { 56 TransformerFactory factory = TransformerFactory.newInstance(); 57 Source styleSheet = new StreamSource(this.getClass().getClassLoader().getResourceAsStream(STYLESHEET_RESOURCE)); 58 Templates templates = templates = factory.newTemplates(styleSheet); 59 Transformer transformer = templates.newTransformer(); 60 transformer.setOutputProperty("indent", "yes"); 61 transformer.setParameter("readOnly", "false"); 62 //transformer.setParameter("docType", docType); 63 //transformer.setParameter("schema", schema); 64 65 Source input = new StreamSource(this.getClass().getResourceAsStream(INITIAL_EDOC_XML)); 66 transformer.transform(input, new StreamResult(System.out)); 67 } 68 69 @Test public void testFieldHasMatchingUserValues() throws Exception { 70 LOG.info("testFieldHasMatchingUserValues"); 71 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 72 73 XPath xpath = XPathFactory.newInstance().newXPath(); 74 Document doc = db.parse(this.getClass().getResourceAsStream(SAMPLE_EDOC_XML)); 75 // enumerate all fields 76 final String fieldDefs = "/edlContent/edl/field/display/values"; 77 NodeList nodes = (NodeList) xpath.evaluate(fieldDefs, doc, XPathConstants.NODESET); 78 79 for (int i = 0; i < nodes.getLength(); i++) { 80 Node node = nodes.item(i); 81 String name = (String) xpath.evaluate("../../@name", node, XPathConstants.STRING); 82 LOG.debug("Name: " + name); 83 LOG.debug("Value: " + node.getFirstChild().getNodeValue()); 84 final String expr = "/edlContent/data/version[@current='true']/fieldEntry[@name=current()/../../@name and value=current()]"; 85 NodeList matchingUserValues = (NodeList) xpath.evaluate(expr, node, XPathConstants.NODESET); 86 LOG.debug(matchingUserValues + ""); 87 LOG.debug(matchingUserValues.getLength() + ""); 88 if ("gender".equals(name)) { 89 assertTrue("Matching values > 0", matchingUserValues.getLength() > 0); 90 } 91 for (int j = 0; j < matchingUserValues.getLength(); j++) { 92 LOG.debug(matchingUserValues.item(j).getFirstChild().getNodeValue()); 93 } 94 } 95 } 96 97 /* 98 @Test public void testUpdateEDLDocument() throws Exception { 99 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 }