📄 propertyutil.java
字号:
} /** * Checks if the given path follows the path syntax rules. * * @param jcrPath the string to test */ public static boolean checkPathFormat(String jcrPath, Session session) throws RepositoryException { if (jcrPath == null || jcrPath.length() == 0) { return false; } else if (jcrPath.equals("/")) { return true; } else { NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry(); boolean match = false; boolean prefixOk = true; // split path into path elements and validate each of them String[] elems = jcrPath.split("/", -1); for (int i = jcrPath.startsWith("/") ? 1 : 0; i < elems.length; i++) { // validate path element String elem = elems[i]; Matcher matcher = PATTERN_PATH.matcher(elem); match = matcher.matches(); if (!match) { break; } // validate namespace prefixes if present String[] split = elem.split(":"); if (split.length > 1) { String prefix = split[0]; try { nsr.getURI(prefix); } catch (NamespaceException nse) { prefixOk = false; break; } } } return match && prefixOk; } } /** * Checks if the String is a valid date in string format. * * @param str the string to test. * @return <code>true</code> if <code>str</code> is a valid date format. */ public static boolean isDateFormat(String str) { return PATTERN_DATE.matcher(str).matches(); } /** * Counts the number of bytes of a Binary value. * * @param val the binary value. * @return the number of bytes or -1 in case of any exception */ public static long countBytes(Value val) { int length = 0; InputStream in = null; try { in = val.getStream(); BufferedInputStream bin = new BufferedInputStream(in); while (bin.read() != -1) { length++; } bin.close(); } catch (Exception e) { length = -1; } finally { if (in != null) { try { in.close(); } catch (IOException ignore) {} } } return length; } /** * Helper method to test the type received with Value.getType() and * Property.getType() . */ public static boolean checkGetType(Property prop, int propType) throws RepositoryException { Value val = getValue(prop); boolean samePropType = (val.getType() == propType); int requiredType = prop.getDefinition().getRequiredType(); if (requiredType != PropertyType.UNDEFINED) { samePropType = (val.getType() == requiredType); } return samePropType; } /** * Helper method to compare the equality of two values for equality with the * fulfilling of the equality conditions. These conditions for the values * are to have the same type and the same string representation. * * @param val1 first value * @param val2 second value * @return true if the equals method is equivalent to the normative * definition of value equality, false in the other case. */ public static boolean equalValues(Value val1, Value val2) throws RepositoryException { boolean isEqual = val1.equals(val2); boolean conditions = false; try { conditions = (val1.getType() == val2.getType()) && val1.getString().equals(val2.getString()); } catch (ValueFormatException vfe) { return false; } return (isEqual == conditions); } /** * Helper method to assure that no property with a null value exist. * * @param node the node to start the search from. * @return <code>true</code> if a null value property is found; * <code>false</code> in the other case. */ public static boolean nullValues(Node node) throws RepositoryException { boolean nullValue = false; for (PropertyIterator props = node.getProperties(); props.hasNext();) { Property property = props.nextProperty(); if (!property.getDefinition().isMultiple()) { nullValue = (property.getValue() == null); if (nullValue) { break; } } } if (!nullValue) { for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) { Node n = nodes.nextNode(); nullValue = nullValues(n); } } return nullValue; } /** * Helper method to find a multivalue property. * * @param node the node to start the search from. * @return a multivalue property or null if not found any. */ public static Property searchMultivalProp(Node node) throws RepositoryException { Property multiVal = null; for (PropertyIterator props = node.getProperties(); props.hasNext();) { Property property = props.nextProperty(); if (property.getDefinition().isMultiple()) { multiVal = property; break; } } if (multiVal == null) { for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) { Node n = nodes.nextNode(); multiVal = searchMultivalProp(n); if (multiVal != null) { break; } } } return multiVal; } /** * Helper method to find a multivalue property of a given type. * * @param node the node to start the search from. * @param type the property type. * @return a multivalue property or null if not found any. */ public static Property searchMultivalProp(Node node, int type) throws RepositoryException { Property multiVal = null; for (PropertyIterator props = node.getProperties(); props.hasNext();) { Property property = props.nextProperty(); if (property.getDefinition().isMultiple() && property.getType() == type) { multiVal = property; break; } } if (multiVal == null) { for (NodeIterator nodes = node.getNodes(); nodes.hasNext();) { Node n = nodes.nextNode(); multiVal = searchMultivalProp(n, type); if (multiVal != null) { break; } } } return multiVal; } /** * Retrieve a single valued property from the given node. * * @param node * @return the property found or null if no property is found. */ public static Property searchSingleValuedProperty(Node node) throws RepositoryException, ValueFormatException { PropertyIterator props = node.getProperties(); while (props.hasNext()) { Property p = props.nextProperty(); if (!p.getDefinition().isMultiple()) { return p; } } // should never get here, since every Node must provide the jcr:primaryType // property, which is single valued. return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -