NodeTypeDef def = new NodeTypeDef();
// name
String name = definition.getName();
if (name == null) {
throw new InvalidNodeTypeDefinitionException("No node type name specified");
}
try {
def.setName(session.getQName(name));
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid name: " + name, e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid name: " + name, e);
}
// supertypes
String[] names = definition.getDeclaredSupertypeNames();
Name[] qnames = new Name[names.length];
for (int i = 0; i < names.length; i++) {
try {
qnames[i] = session.getQName(names[i]);
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid supertype name: " + names[i], e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid supertype name: " + names[i], e);
}
}
def.setSupertypes(qnames);
// primary item
name = definition.getPrimaryItemName();
if (name != null) {
try {
def.setPrimaryItemName(session.getQName(name));
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid primary item name: " + name, e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid primary item name: " + name, e);
}
}
// misc. flags
def.setMixin(definition.isMixin());
def.setAbstract(definition.isAbstract());
def.setOrderableChildNodes(definition.hasOrderableChildNodes());
// child nodes
NodeDefinition[] ndefs = definition.getDeclaredChildNodeDefinitions();
if (ndefs != null) {
NodeDef[] qndefs = new NodeDef[ndefs.length];
for (int i = 0; i < ndefs.length; i++) {
NodeDefImpl qndef = new NodeDefImpl();
// declaring node type
qndef.setDeclaringNodeType(def.getName());
// name
name = ndefs[i].getName();
if (name != null) {
if (name.equals("*")) {
qndef.setName(ItemDef.ANY_NAME);
} else {
try {
qndef.setName(session.getQName(name));
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid node name: " + name, e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid node name: " + name, e);
}
}
}
// default primary type
//name = ndefs[i].getDefaultPrimaryTypeName();
// FIXME when JCR 2.0 API has been finalized
name = ((NodeDefinitionTemplateImpl) ndefs[i]).getDefaultPrimaryTypeName();
if (name != null) {
try {
qndef.setDefaultPrimaryType(session.getQName(name));
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid default primary type: " + name, e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid default primary type: " + name, e);
}
}
// required primary types
//names = ndefs[i].getRequiredPrimaryTypeNames();
// FIXME when JCR 2.0 API has been finalized
names = ((NodeDefinitionTemplateImpl) ndefs[i]).getRequiredPrimaryTypeNames();
qnames = new Name[names.length];
for (int j = 0; i < names.length; j++) {
try {
qnames[i] = session.getQName(names[i]);
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid required primary type: " + names[i], e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid required primary type: " + names[i], e);
}
}
qndef.setRequiredPrimaryTypes(qnames);
// misc. flags/attributes
qndef.setAutoCreated(ndefs[i].isAutoCreated());
qndef.setMandatory(ndefs[i].isMandatory());
qndef.setProtected(ndefs[i].isProtected());
qndef.setOnParentVersion(ndefs[i].getOnParentVersion());
qndef.setAllowsSameNameSiblings(ndefs[i].allowsSameNameSiblings());
qndefs[i] = qndef;
}
def.setChildNodeDefs(qndefs);
}
// properties
PropertyDefinition[] pdefs = definition.getDeclaredPropertyDefinitions();
if (pdefs != null) {
PropDef[] qpdefs = new PropDef[pdefs.length];
for (int i = 0; i < pdefs.length; i++) {
PropDefImpl qpdef = new PropDefImpl();
// declaring node type
qpdef.setDeclaringNodeType(def.getName());
// name
name = pdefs[i].getName();
if (name != null) {
if (name.equals("*")) {
qpdef.setName(ItemDef.ANY_NAME);
} else {
try {
qpdef.setName(session.getQName(name));
} catch (NamespaceException e) {
throw new InvalidNodeTypeDefinitionException("Invalid property name: " + name, e);
} catch (NameException e) {
throw new InvalidNodeTypeDefinitionException("Invalid property name: " + name, e);
}
}
}
// misc. flags/attributes
int type = pdefs[i].getRequiredType();
qpdef.setRequiredType(type);
qpdef.setAutoCreated(pdefs[i].isAutoCreated());
qpdef.setMandatory(pdefs[i].isMandatory());
qpdef.setProtected(pdefs[i].isProtected());
qpdef.setOnParentVersion(pdefs[i].getOnParentVersion());
qpdef.setMultiple(pdefs[i].isMultiple());
// value constraints
String[] constraints = pdefs[i].getValueConstraints();
if (constraints != null) {
ValueConstraint[] qconstraints = new ValueConstraint[constraints.length];
for (int j = 0; j < constraints.length; j++) {
try {
qconstraints[j] = ValueConstraint.create(type, constraints[i], session);
} catch (InvalidConstraintException e) {
throw new InvalidNodeTypeDefinitionException(
"Invalid value constraint " + constraints[i], e);
}
}
qpdef.setValueConstraints(qconstraints);
}
// default values
Value[] values = pdefs[i].getDefaultValues();
if (values != null) {
InternalValue[] qvalues = new InternalValue[values.length];
for (int j = 0; j < values.length; j++) {
try {
qvalues[j] = InternalValue.create(values[i], session);
} catch (ValueFormatException e) {
throw new InvalidNodeTypeDefinitionException(
"Invalid default value format", e);
}
}
qpdef.setDefaultValues(qvalues);
}