📄 sysviewcontenthandler.java
字号:
Map sessionNamespaces = new HashMap(); String[] sessionPrefixes = session.getNamespacePrefixes(); for (int i = 0; i < sessionPrefixes.length; i++) { sessionNamespaces.put(sessionPrefixes[i], session.getNamespaceURI(sessionPrefixes[i])); } // check prefixes against namespace mapping in session for (Iterator it = prefixes.keySet().iterator(); it.hasNext(); ) { String prefix = (String) it.next(); if ("xml".equals(prefix)) { Assert.fail("Prefix mapping for 'xml' must not be exported."); } String uri = (String) prefixes.get(prefix); checkCondition("Exported uri " + uri + " is not a registered namespace.", sessionNamespaces.containsValue(uri)); checkCondition("Exported prefix " + prefix + " does not match " + "current namespacce mapping in Session", sessionNamespaces.containsKey(prefix)); } } catch (RepositoryException re) { throw new SAXException(re); } } // helpers for test result forward private void checkCondition(String str, boolean bool) { Assert.assertTrue(str, bool); } public class ConditionException extends SAXException { public ConditionException(String message) { super(message); } } //--------------helper methods to check the data --------------------------------- /** * Checks the correct position of the jcr:primarType, jcr:mixinTypes * and jcr:uuid in the property elements of the given nodeElem. * @param nodeElem * @throws RepositoryException */ private void checkPropOrder(NodeElemData nodeElem) throws RepositoryException, SAXException { boolean jcrPrimaryTypeFound = false; boolean jcrMixinTypesFound = true; boolean uuidFound = true; PropElemData propElem = (PropElemData) nodeElem.propElems.get(0); jcrPrimaryTypeFound = (jcrPrimaryType.equals(propElem.name)); checkCondition("Exported property jcr:primaryType of node " + nodeElem.path + " is not at the first position.", jcrPrimaryTypeFound); if (nodeElem.node.hasProperty(jcrMixinTypes)) { PropElemData propElem2 = (PropElemData) nodeElem.propElems.get(1); jcrMixinTypesFound = (jcrMixinTypes.equals(propElem2.name)); checkCondition("Exported property jcr:jcrMixinTypes of node " + nodeElem.path + " is not at the second position.", jcrMixinTypesFound); NodeType[] mixins = nodeElem.node.getMixinNodeTypes(); for (int j=0; j<mixins.length; j++) { if (mixins[j].getName().equals(mixReferenceable)) { uuidFound = (jcrUuid.equals(((PropElemData)nodeElem.propElems.get(2)).name)); checkCondition("Exported property jcr:uuid of node " + nodeElem.path + " is not at the third position.", uuidFound); } } } } /** * Checks the values of all exported properties of a given node. * * @param nodeElem The nodeElem of the given node. * @param skipBinary Boolean if the binary properties should be exported. * @throws RepositoryException * @throws SAXException * @throws IOException */ private void checkAllProps(NodeElemData nodeElem, boolean skipBinary) throws RepositoryException, SAXException, IOException { boolean allFound = false; boolean correctVal = false; Node node = nodeElem.node; ArrayList propElems = nodeElem.propElems; // no props exported if (propElems.size() == 0) { // if node has properties they should be of Binary type and skipBinary should be true if (node.hasProperties()) { if (skipBinary) { PropertyIterator iter = node.getProperties(); while (iter.hasNext()) { Property prop = iter.nextProperty(); checkCondition("Property " + prop.getName() + " of node " + node.getPath() + " is not exported.", prop.getType()== PropertyType.BINARY); } } else { checkCondition("One or more properties of node " + node.getPath() + " are not exported.", false); } } else { // ok } } else { // compare the propElems with the properties of the given node for (int i = 0; i < propElems.size(); i++) { correctVal = false; PropElemData propElem = (PropElemData) propElems.get(i); int propType = propElem.type; if (node.hasProperty(propElem.name)) { Property prop = node.getProperty(propElem.name); // compare the propTypes correctVal = (propType == prop.getType()); checkCondition("Property type of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly." + "expected: "+prop.getType()+" received: "+propType, correctVal); // property which should be exported if (propType == PropertyType.BINARY && !skipBinary || propType != PropertyType.BINARY) { try { int size = propElem.values.size(); // multivalue property with empty value array if (size == 0) { if (prop.getDefinition().isMultiple()) { long length = prop.getValues().length; checkCondition("Value of property " + prop.getPath() + " is not exported.", length == 0); } else { checkCondition("Singler value property " + prop.getPath() + " with no value is exported.", false); } } // single value property or multivalue property with one value if (size == 1) { String str = ""; if (prop.getDefinition().isMultiple()) { str = (prop.getValues()[0]).getString(); } else { str = prop.getString(); } String val = (String) propElem.values.get(0); if (prop.getType() == PropertyType.BINARY) { // decode value val = decodeBase64(val); } correctVal = (str.equals(val)); checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly:" + " expected value: "+str+" found value: "+val, correctVal); } // multivalue property with several values else { Value[] vals = prop.getValues(); checkCondition("Number of exported values of property " + prop.getPath() + " does not match the number " + "its values", vals.length == size); for (int j = 0; j < size; j++) { // we know that the order of the values // of a mulitval prop is preserved during export String val = (String)propElem.values.get(j); if (prop.getType() == PropertyType.BINARY) { // decode value val = decodeBase64(val); } String str = vals[j].getString(); correctVal = (val.equals(str)); checkCondition("Property value of property " + propElem.name + " of node " + nodeElem.path + " is not exported correctly.", correctVal); } } } catch (ValueFormatException vfe ) { checkCondition("Error during retreiviing the value(s)" + " of property " + prop.getPath() + vfe.toString() + " .", false); } } // skipBinary true and propType is Binary, should be skipped else { checkCondition("Value of binary property "+ prop.getPath() + " exported although skipBinary flag is true.", propElem.values.isEmpty()); } } // given node has no property with the name given by the prop element else { checkCondition("Property element " + propElem.name + " found but node " + nodeElem.node.getPath() + " does not have a property with that name", false); } } // compare the sizes here long otherSize = getSize(node.getProperties()); allFound = propElems.size() == otherSize; checkCondition("Not all properties of node " + nodeElem.path + " are exported.", allFound); } } /** * Counts the number of child nodes exported and compare with the number * of child nodes of a given node. * @param nodeElem The node to check. * @param noRecurse Boolean if child nodes should be exported at all. * @throws RepositoryException * @throws SAXException */ private void checkChildren(NodeElemData nodeElem, boolean noRecurse) throws RepositoryException, SAXException { Hashtable childElemsFound = nodeElem.childNodeElemNames; boolean totalSumOk = false; boolean partialSumOk = true; if (noRecurse) { totalSumOk = (childElemsFound.size() == 0); } else { // all children found if number of node.getNodes(name) is the same as found // in childElemsFound and if sum(number of nodeGetNodes(names)) // == number of node.getNodes() long childrenFound = 0; NodeIterator nodeIter = nodeElem.node.getNodes(); long children = getSize(nodeIter); for (Enumeration e = childElemsFound.elements(); e.hasMoreElements();) { ChildNodeElem child = (ChildNodeElem) e.nextElement(); String name = child.name; long number = child.number; NodeIterator iter = nodeElem.node.getNodes(name); long size = 0; size = getSize(iter); if (size != number) { partialSumOk = false; break; } else { childrenFound += number; } } totalSumOk = (children == childrenFound); checkCondition("The number of child nodes of node" + nodeElem.path + " which are exported does not match the number of its child nodes.", totalSumOk && partialSumOk); } } // helper methods /** * Decodes Base64 encoded binary values. * @param str the string to decode * @return * @throws IOException */ private String decodeBase64(String str) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Base64.decode(str, bos); String decoded = bos.toString("UTF-8"); return decoded; } /** * * @param it * @return */ private long getSize(RangeIterator it) { long size = it.getSize(); if (size != -1) { return size; } size = 0; while (it.hasNext()) { it.next(); size++; } return size; } //---------------- helper classes for collecting the xml data found ---------------- /** * Node data class holding the collected data found during event processing. */ private class NodeElemData { // Name of the node String name; // the number of the occurence of this name as child element name // this is then the same as the index of this node. long index; // the path of the node String path; // List of PropElemData ArrayList propElems = new ArrayList(); // the node itself Node node; // the current position of the child node in process among its same name siblings. // /the index of the child node in proces is therefore position+1) int position = 0; // the childNodeElems (stored are key: name and // value: number of the same name siblings) Hashtable childNodeElemNames = new Hashtable(); } /** * Property data of the current property element. */ private class PropElemData { String name; String typeName; int type; ArrayList values; } /** * Child node data. */ private class ChildNodeElem { String name; long number; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -