📄 exportdocviewtest.java
字号:
// xmltext nodes are not exported as elements if (isXMLTextNode(node)) { fail("Xml text node " + node.getPath() + " is wronlgy exported as element."); } else { // order of same name siblings is preserved during export int index = node.getIndex(); try { nodeElem = (Element) children.get(index - 1); } catch (IndexOutOfBoundsException iobe) { fail("Node " + node.getPath() + " is not exported." + iobe.toString()); } } } else { // need to be exported if (!isXMLTextNode(node) && (isValidName || exportInvalidXmlNames)) { fail("Node " + node.getPath() + " is not exported."); } } return nodeElem; } /** * Check if a property of a node is exported. This is true if a * corresponding attribute is found in the element the node is exported to. * An attribute is corresponding when it has the same name as the given * property (or it is equal to its escaped name). (chapter 6.4.2.1 point 5 * of the JCR specification). * * @param prop * @param elem * @return * @throws RepositoryException */ private Attr findAttribute(Property prop, Element elem) throws RepositoryException { String name = prop.getName(); boolean isValidName = XMLChar.isValidName(name); name = !isValidName ? escapeNames(name) : name; Attr attribute = elem.getAttributeNode(name); return attribute; } /** * Check if a property should be exported according the three choices * skipBinary, exportMultivalProps and exportInvalidXmlNames. * * @param prop * @param attribute * @throws RepositoryException */ private void checkAttribute(Property prop, Attr attribute) throws RepositoryException { boolean isBinary = (prop.getType() == PropertyType.BINARY); boolean isMultiple = prop.getDefinition().isMultiple(); if (skipBinary) { if (isBinary && !(isMultiple && !exportMultivalProps)) { assertEquals("Value of binary property " + prop.getPath() + " exported although skipBinary is true", attribute.getValue().length(), 0); } // check the flags else { checkExportFlags(prop, attribute); } } // saveBinary else { if (isBinary && !(isMultiple && !exportMultivalProps)) { assertTrue("Binary property " + prop.getPath() + " not exported although skipBinary is false", attribute != null); } // check anyway the flags checkExportFlags(prop, attribute); } } /** * Checks attribute export regarding the two flags and without considering * skipBinary. * * @param prop * @param attribute * @throws RepositoryException */ private void checkExportFlags(Property prop, Attr attribute) throws RepositoryException { String name = prop.getName(); boolean isMultiple = prop.getDefinition().isMultiple(); boolean isValidName = XMLChar.isValidName(name); if (isMultiple) { if (exportMultivalProps) { assertTrue("Not all multivalued properties are exported: " + prop.getPath() + " is not exported.", attribute != null); } else { // skipping multi-valued properties entirely is legal // according to "6.4.2.5 Multi-value Properties" of the // jsr-170 specification return; } } // check anyway the other flag if (exportInvalidXmlNames && !isValidName) { assertTrue("Not all properties with invalid xml name are exported: " + prop.getPath() + " is not exported.", attribute != null); } else { assertTrue("Property " + prop.getPath() + " is not exported.", attribute != null); } } /** * Compares the given node with the given element. Comparison is succesful * if the number of exported child nodes and exported properties match the * found child elements and attributes considering the possible exceptions * and if the comparison of the properties of the node with the attributes * of the element is successful too. * * @param node * @param elem * @throws RepositoryException */ private void compareNode(Node node, Element elem) throws RepositoryException, IOException { // count the child nodes and compare with the exported child elements compareChildNumber(node, elem); // count the properties and compare with attributes exported comparePropNumber(node, elem); PropertyIterator propIter = node.getProperties(); while (propIter.hasNext()) { Property prop = propIter.nextProperty(); Attr attr = findAttribute(prop, elem); checkAttribute(prop, attr); if (attr != null) { compareProperty(prop, attr); } } } /** * Compare the given property with the given attribute. Comparison is * successful if their values can be matched considering binary type, * multivalue export. (chapter 6.4.2.1 point 6 of the JCR specification). * * @param prop * @param attr * @throws RepositoryException */ private void compareProperty(Property prop, Attr attr) throws RepositoryException, IOException { boolean isMultiple = prop.getDefinition().isMultiple(); boolean isBinary = (prop.getType() == PropertyType.BINARY); String attrVal = attr.getValue(); String val = null; if (isMultiple) { val = exportValues(prop, isBinary); } else { if (isBinary) { try { attrVal = decodeBase64(attrVal); val = prop.getString(); } catch (IOException ioe) { fail("Could not decode value of binary attribute " + attr.getName() + " of element " + attr.getOwnerElement().getTagName()); } } else { val = prop.getString(); } } if (isBinary && skipBinary) { assertEquals("Value of binary property " + prop.getPath() + " is not exported correctly: ", "", attrVal); assertEquals("Value of binary property " + prop.getPath() + " exported although skipBinary is true", "", attrVal); } else { assertTrue("Value of property " + prop.getPath() + " is not exported correctly: " + attrVal, val.equals(attrVal) || escapeValues(val).equals(attrVal)); } } /** * Checks if all registered namespaces are exported into the root element. * (chapter 6.4.2.1 point 1 of the JCR specification). * * @param root * @throws RepositoryException */ private void compareNamespaces(Element root) throws RepositoryException { Properties nameSpaces = new AttributeSeparator(root).getNsAttrs(); // check if all namespaces exist that were exported for (Enumeration e = nameSpaces.keys(); e.hasMoreElements();) { String prefix = (String) e.nextElement(); String URI = nameSpaces.getProperty(prefix); assertEquals("Prefix of uri" + URI + "is not exported correctly.", nsr.getPrefix(URI), prefix); assertEquals("Uri of prefix " + prefix + "is not exported correctly.", nsr.getURI(prefix), URI); } String[] registeredNamespaces = nsr.getURIs(); // check if all required namespaces are exported for (int i = 0; i < registeredNamespaces.length; i++) { String prefix = nsr.getPrefix(registeredNamespaces[i]); // skip default namespace and xml namespaces if (prefix.length() == 0 || prefix.startsWith("xml")) { continue; } else { assertTrue("namespace: " + registeredNamespaces[i] + " not exported", nameSpaces.keySet().contains(prefix)); } } } /** * Count the number of child nodes of a node which are exported and compare * with the number expected. * * @param node * @param elem * @throws RepositoryException */ private void compareChildNumber(Node node, Element elem) throws RepositoryException { NodeIterator iter = node.getNodes(); long size = 0; long exported = countChildElems(elem); // child tree is exported too if (!noRecurse) { size = getSize(node.getNodes()); while (iter.hasNext()) { Node n = iter.nextNode(); String name = n.getName(); // xmltext node ? if (isXMLTextNode(n)) { size--; } if (!exportInvalidXmlNames && !XMLChar.isValidName(name)) { size--; } } } assertEquals("The number of child nodes of node " + node.getPath() + " which are exported is not correct: ", size, exported); } /** * Count the number of exported properties of a given node and compare with * the number of the properties expected to be exported. * * @param node * @param elem * @throws RepositoryException */ private void comparePropNumber(Node node, Element elem) throws RepositoryException { PropertyIterator iter = node.getProperties(); long size = getSize(node.getProperties()); long exported = new AttributeSeparator(elem).getNonNsAttrs().size(); while (iter.hasNext()) { Property prop = iter.nextProperty(); String name = prop.getName(); boolean isMultiple = prop.getDefinition().isMultiple(); // props not exported so we decrease the expected size. if (!exportInvalidXmlNames && !XMLChar.isValidName(name)) { size--; } else if (!exportMultivalProps && isMultiple) { size--; } } assertEquals("The number of properties exported of node " + node.getPath() + " is not correct.", size, exported); } /** * Compares the text of a given XML element with the values of the * jcr:xmlcharacters properties of the given jcr:xmltext nodes sequel. If * the sequel has more than one node the serialized values are concatenated * with a space. We only check the case withHandler is true. * * @param nodes * @param parentElem * @throws RepositoryException */ private void compareXmltextNodes(ArrayList nodes, Element parentElem) throws RepositoryException { // only this case if (withHandler) { String value = ""; String exportedVal = ""; StackEntry currentEntry = (StackEntry) textValuesStack.pop(); try { exportedVal = (String) currentEntry.textValues.get(currentEntry.position); currentEntry.position++; textValuesStack.push(currentEntry); } catch (IndexOutOfBoundsException iobe) { fail("Xmltext nodes not correctly exported: " + iobe.getMessage()); } int size = nodes.size(); if (size == 1) { Node node = (Node) nodes.get(0); Property prop = node.getProperty(JCR_XMLDATA); value = prop.getString(); assertEquals("The " + JCR_XMLTEXT + " node " + node.getPath() + " is not exported correctly.", value, exportedVal); } else { // check the concatenated values sequenceally for (int i = 0; i < nodes.size(); i++) { Node node = (Node) nodes.get(i); Property prop = node.getProperty(JCR_XMLDATA); value = prop.getString(); // the first one if (i == 0) { if (exportedVal.regionMatches(0, value, 0, value.length())) { // test ok, remove the checked part of the text exportedVal = exportedVal.substring(0, value.length()); } else { fail("The " + JCR_XMLTEXT + " node " + node.getPath() + " is not exported correctly: expected: " + value + " found: " + exportedVal); } } // we assume at the moment that any white space char is possible // between two adjacent xmltext nodesso we try to match as long // as space characters are at the beginning of the // remaining exported string // todo once this will be specified in the spec more exactly else { // the last one if (exportedVal.regionMatches(0, value, 0, value.length())) { // test ok exportedVal = exportedVal.substring(0, value.length()); } else { boolean match = false; int j = 0; char c = exportedVal.charAt(j); while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\u000B') { if (exportedVal.regionMatches(j, value, 0, value.length())) { exportedVal = exportedVal.substring(j, value.length() + j); match = true; break; } else { j++; c = exportedVal.charAt(j); } } assertTrue("The " + JCR_XMLTEXT + " node " + node.getPath() + " is not exported correctly: expected: " + value + " found: " + exportedVal, match); } } } } } } /** * Loops through all child items of a given node to test if items with * invalid xml name are exported. (chapter 6.4.2.4 of the JCR * specification). * * @param node the root node of the tree to search * @param elem the parent element of the element to which the parent node of * the given node is exported. * @throws RepositoryException */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -