📄 exportdocviewtest.java
字号:
private boolean setExportInvalidXmlNames(Node node, Element elem, boolean isSet) throws RepositoryException { if (!XMLChar.isValidName(node.getName())) { if (elem != null) { exportInvalidXmlNames = true; isSet = true; } else { exportInvalidXmlNames = false; isSet = true; } } // try properties if (!isSet) { PropertyIterator iter = node.getProperties(); while (iter.hasNext()) { Property prop = iter.nextProperty(); if (!exportMultivalProps && prop.getDefinition().isMultiple()) { continue; } if (!XMLChar.isValidName(prop.getName())) { // property exported? exportInvalidXmlNames = isExportedProp(prop, elem); isSet = true; } } } // try child nodes if (!isSet && !noRecurse) { // search again NodeIterator iter = node.getNodes(); while (iter.hasNext()) { Node n = iter.nextNode(); Element el = findElem(n, elem); isSet = setExportInvalidXmlNames(n, el, isSet); } } return isSet; } /** * Set the exportMultivalProps flag. Traverses the tree given by the node * and searches a multivalue property which is exported to an attribute of a * element of an element tree. (chapter 6.4.2.5 of the JCR specification). * * @param node * @param elem * @throws RepositoryException */ private boolean setExportMultivalProps(Node node, Element elem, boolean isSet) throws RepositoryException { Property[] props = searchMultivalProps(node); // multivalued property with valid xml name if (props[0] != null) { exportMultivalProps = isExportedProp(props[0], elem); isSet = true; } else { // invalid xml named multivalue property exported if (props[1] != null) { exportMultivalProps = isExportedProp(props[1], elem); if (!exportMultivalProps && exportInvalidXmlNames) { isSet = true; } } } if (!isSet && !noRecurse) { // search again NodeIterator iter = node.getNodes(); while (iter.hasNext()) { Node n = iter.nextNode(); Element el = findElem(n, elem); if (el != null) { isSet = setExportMultivalProps(n, el, isSet); } else { isSet = false; } } } return isSet; } //-----------------------------------< helper methods >----------------------------- /** * Search a given node if it contains a multivalue property. As invalid xml * names may be exported or not we want to find a multivalue property with * valid xml name and also one with an invalid xml name. Returned is a pair * of multivalued properties, the first has a valid xml name, the second an * invalid one. In case one of these is not found it is replaced by null in * the pair. * * @param node the node to start the search. * @return A pair of multivalued properties. * @throws RepositoryException */ private Property[] searchMultivalProps(Node node) throws RepositoryException { Property[] properties = {null, null}; for (PropertyIterator props = node.getProperties(); props.hasNext();) { Property property = props.nextProperty(); if (property.getDefinition().isMultiple()) { if (XMLChar.isValidName(property.getName())) { properties[0] = property; break; } else { properties[1] = property; } } } return properties; } /** * Tests if a property is exported or not. * * @param prop * @param elem * @return * @throws RepositoryException */ private boolean isExportedProp(Property prop, Element elem) throws RepositoryException { String name = prop.getName(); name = XMLChar.isValidName(prop.getName()) ? name : escapeNames(name); Attr attr = elem.getAttributeNode(name); return (attr != null); } /** * Checks if a given node is a jcr:xmltext named node and fulfills the * condition that the property's value is exported as text. * * @param node The node to check. * @return boolean indicating if the given node fulfills the required * conditions. * @throws RepositoryException */ private boolean isXMLTextNode(Node node) throws RepositoryException { boolean isTrue = node.getName().equals(JCR_XMLTEXT); if (node.hasProperty(JCR_XMLDATA)) { Property prop = node.getProperty(JCR_XMLDATA); isTrue = !prop.getDefinition().isMultiple() && prop.getType() == PropertyType.STRING // only one property beneath the required jcr:primaryType && getSize(node.getProperties()) == 2 && getSize(node.getNodes()) == 0; } else { isTrue = false; } return isTrue; }//-----------------------------------< static helper methods >----------------------------- /** * Decodes a given base 64 encoded string. * * @param str * @return * @throws IOException */ private static String decodeBase64(String str) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Base64.decode(str, bos); String decoded = bos.toString("UTF-8"); return decoded; } /** * Encodes a given stream to base64. * * @param in the stream to encode. * @return the encoded string in base64. * @throws IOException if an error occurs. */ private static String encodeBase64(InputStream in) throws IOException { StringWriter writer = new StringWriter(); Base64.encode(in, writer); return writer.getBuffer().toString(); } /** * Exports values of a multivalue property and concatenate the values * separated by a space. (chapter 6.4.4 of the JCR specification). * * @param prop * @param isBinary * @return * @throws RepositoryException */ private static String exportValues(Property prop, boolean isBinary) throws RepositoryException, IOException { Value[] vals = prop.getValues(); // order of multi values is preserved. // multival with empty array is exported as empty string StringBuffer exportedVal = new StringBuffer(); String space = ""; if (isBinary) { for (int i = 0; i < vals.length; i++) { exportedVal.append(space); InputStream in = vals[i].getStream(); try { exportedVal.append(encodeBase64(in)); } finally { in.close(); } space = " "; } } else { for (int i = 0; i < vals.length; i++) { exportedVal.append(space); exportedVal.append(escapeValues(vals[i].getString())); space = " "; } } return exportedVal.toString(); } /** * Escapes the characters of a given String representing a Name of an item. * The escaping scheme is according the requirements of the JSR 170 * Specification chapter 6.4.3 . No check performed if the given string is * indeed a Name or not. * * @param name * @return */ private static String escapeNames(String name) { return EscapeJCRUtil.escapeJCRNames(name); } /** * Escapes the characters of a given string value according the requirements * of chapter 6.4.4 of JSR 170 Specification. * * @param value The string to escape its characters. * @return */ private static String escapeValues(String value) { return EscapeJCRUtil.escapeJCRValues(value); }//----------------< helpers to retrieve data from an xml document >------------------- /** * Returns all child elements of the given xml element which have the given * name. * * @param elem * @param name * @return */ private ArrayList getChildElems(Element elem, String name) { ArrayList children = new ArrayList(); org.w3c.dom.Node child = elem.getFirstChild(); while (child != null) { if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { if (name.equals("*") || name.equals(child.getNodeName())) { children.add(child); } } child = child.getNextSibling(); } return children; } /** * Counts the number of child elements of the given xml element. * * @param elem * @return */ private long countChildElems(Element elem) { long length = 0; org.w3c.dom.Node child = elem.getFirstChild(); while (child != null) { if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { length++; } child = child.getNextSibling(); } return length; } /** * Collects the characters of successive text nodes of the given xml element * into an ArrayList. * * @param elem * @return */ private ArrayList getChildTextNodeValues(Element elem) { ArrayList textValues = new ArrayList(); StringBuffer buf = new StringBuffer(); org.w3c.dom.Node child = elem.getFirstChild(); // collect the characters of successive text nodes while (child != null) { if (child.getNodeType() == org.w3c.dom.Node.TEXT_NODE) { while (child != null && child.getNodeType() == org.w3c.dom.Node.TEXT_NODE) { buf.append(child.getNodeValue()); child = child.getNextSibling(); } textValues.add(buf.toString()); buf = new StringBuffer(); } else { child = child.getNextSibling(); } } return textValues; } /** * Reads a DOM document from the given XML stream. * * @param xml XML stream * @return DOM document * @throws RepositoryException if the document could not be read */ private Document readDocument(InputStream xml) throws RepositoryException { try { StreamSource source = new StreamSource(xml); DOMResult result = new DOMResult(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return (Document) result.getNode(); } catch (TransformerException e) { throw new RepositoryException("Unable to read xml file", e); } } /** * Helper class to separate the attributes with xmlns namespace from the * attributes without xmlns namspace. Solely used for the root element of an * xml document. */ private class AttributeSeparator { private static final String xmlnsURI = "http://www.w3.org/2000/xmlns/"; private static final String xmlnsPrefix = "xmlns"; Element elem; NamedNodeMap attrs; Properties nsAttrs; Properties nonNsAttrs; AttributeSeparator(Element elem) { this.elem = elem; nsAttrs = new Properties(); nonNsAttrs = new Properties(); attrs = elem.getAttributes(); separateAttrs(); } public Properties getNsAttrs() { return nsAttrs; } public Properties getNonNsAttrs() { return nonNsAttrs; } private void separateAttrs() { for (int i = 0; i < attrs.getLength(); i++) { Attr attribute = (Attr) attrs.item(i); if (xmlnsURI.equals(attribute.getNamespaceURI())) { String localName = attribute.getLocalName(); // ignore setting default namespace if (xmlnsPrefix.equals(localName)) { continue; } nsAttrs.put(localName, attribute.getValue()); } else { nonNsAttrs.put(attribute.getName(), attribute.getValue()); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -