nodetypeutil.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 845 行 · 第 1/3 页
JAVA
845 行
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.test.api.nodetype;import org.apache.jackrabbit.test.ISO8601;import javax.jcr.PropertyType;import javax.jcr.RepositoryException;import javax.jcr.Session;import javax.jcr.Value;import javax.jcr.ValueFormatException;import javax.jcr.nodetype.NodeDefinition;import javax.jcr.nodetype.NodeType;import javax.jcr.nodetype.NodeTypeIterator;import javax.jcr.nodetype.NodeTypeManager;import javax.jcr.nodetype.PropertyDefinition;import java.util.Calendar;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * Utility class to locate item definitions in the NodeTyeManager. */public class NodeTypeUtil { public static final int ANY_PROPERTY_TYPE = -1; /** * Locate a non-protected child node def parsing all node types * * @param session the session to access the node types * @param regardDefaultPrimaryType if true, the default primary type of the * returned <code>NodeDef</code> is * according to param <code>defaultPrimaryType</code>. * If false, the returned <code>NodeDef</code> * might have a default primary type or * not. * @param defaultPrimaryType if <code>regardDefaultPrimaryType</code> * is true: if true, the returned * <code>NodeDef</code> has a default * primary type, else not * @param residual if true, the returned <code>NodeDef</code> * is of the residual name "*", else not * @return * @throws RepositoryException */ public static NodeDefinition locateChildNodeDef(Session session, boolean regardDefaultPrimaryType, boolean defaultPrimaryType, boolean residual) throws RepositoryException { NodeTypeManager manager = session.getWorkspace().getNodeTypeManager(); NodeTypeIterator types = manager.getAllNodeTypes(); boolean overjump = false; while (types.hasNext()) { NodeType type = types.nextNodeType(); // node types with more than one residual child node definition // will cause trouble in test cases. the implementation // might pick another definition than the definition returned by // this method, when a child node is set. NodeDefinition[] childDefs = type.getChildNodeDefinitions(); int residuals = 0; for (int i = 0; i < childDefs.length; i++) { if (childDefs[i].getName().equals("*")) { residuals++; } } if (residuals > 1) { // more than one residual, not suitable for tests continue; } NodeDefinition nodeDefs[] = type.getDeclaredChildNodeDefinitions(); for (int i = 0; i < nodeDefs.length; i++) { NodeDefinition nodeDef = nodeDefs[i]; if (nodeDef.isProtected()) { continue; } if (nodeDef.getRequiredPrimaryTypes().length > 1) { // behaviour of implementations that support multiple multiple inheritance // of primary node types is not specified continue; } if (regardDefaultPrimaryType) { if (defaultPrimaryType && nodeDef.getDefaultPrimaryType() == null) { continue; } if (!defaultPrimaryType && nodeDef.getDefaultPrimaryType() != null) { continue; } } if (residual && !nodeDef.getName().equals("*")) { continue; } if (!residual) { // if another child node def is a residual definition // overjump the current node type NodeDefinition nodeDefsAll[] = type.getChildNodeDefinitions(); for (int j = 0; j < nodeDefsAll.length; j++) { if (nodeDefsAll[j].getName().equals("*")) { overjump = true; break; } } if (overjump) { // break the loop of the current child not defs overjump = false; break; } } return nodeDef; } } return null; } /** * Locate a child node def parsing all node types * * @param session the session to access the node types * @param isProtected if true, the returned <code>NodeDef</code> is * protected, else not * @param mandatory if true, the returned <code>NodeDef</code> is * mandatory, else not * @return the first <code>NodeDef</code> found fitting the requirements */ public static NodeDefinition locateChildNodeDef(Session session, boolean isProtected, boolean mandatory) throws RepositoryException { NodeTypeManager manager = session.getWorkspace().getNodeTypeManager(); NodeTypeIterator types = manager.getAllNodeTypes(); while (types.hasNext()) { NodeType type = types.nextNodeType(); NodeDefinition nodeDefs[] = type.getDeclaredChildNodeDefinitions(); for (int i = 0; i < nodeDefs.length; i++) { NodeDefinition nodeDef = nodeDefs[i]; if (nodeDef.getName().equals("*")) { continue; } if (isProtected && !nodeDef.isProtected()) { continue; } if (!isProtected && nodeDef.isProtected()) { continue; } if (mandatory && !nodeDef.isMandatory()) { continue; } if (!mandatory && nodeDef.isMandatory()) { continue; } return nodeDef; } } return null; } /** * Locate a property def parsing all node types * * @param session the session to access the node types * @param propertyType the type of the returned property. -1 indicates to * return a property of any type but not UNDEFIEND * @param multiple if true, the returned <code>PropertyDef</code> is * multiple, else not * @param isProtected if true, the returned <code>PropertyDef</code> is * protected, else not * @param residual if true, the returned <code>PropertyDef</code> is of * the residual name "*", else not * @return the first <code>PropertyDef</code> found fitting the * requirements */ public static PropertyDefinition locatePropertyDef(Session session, int propertyType, boolean multiple, boolean isProtected, boolean constraints, boolean residual) throws RepositoryException { NodeTypeManager manager = session.getWorkspace().getNodeTypeManager(); NodeTypeIterator types = manager.getAllNodeTypes(); while (types.hasNext()) { NodeType type = types.nextNodeType(); PropertyDefinition propDefs[] = type.getDeclaredPropertyDefinitions(); for (int i = 0; i < propDefs.length; i++) { PropertyDefinition propDef = propDefs[i]; if (propertyType != ANY_PROPERTY_TYPE && propDef.getRequiredType() != propertyType) { continue; } if (propertyType == ANY_PROPERTY_TYPE && propDef.getRequiredType() == PropertyType.UNDEFINED) { continue; } if (multiple && !propDef.isMultiple()) { continue; } if (!multiple && propDef.isMultiple()) { continue; } if (isProtected && !propDef.isProtected()) { continue; } if (!isProtected && propDef.isProtected()) { continue; } String vc[] = propDef.getValueConstraints(); if (!constraints && vc != null && vc.length > 0) { continue; } if (constraints) { // property def with constraints requested if (vc == null || vc.length == 0) { // property def has no constraints continue; } } if (!residual && propDef.getName().equals("*")) { continue; } if (residual && !propDef.getName().equals("*")) { continue; } // also skip property residual property definition if there // is another residual definition if (residual) { // check if there is another residual property def if (getNumResidualPropDefs(type) > 1) { continue; } } if (!residual) { // if not looking for a residual property def then there // must not be any residual definition at all on the node // type if (getNumResidualPropDefs(type) > 0) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?