Java Code Examples for org.apache.xerces.xs.XSTypeDefinition#COMPLEX_TYPE

The following examples show how to use org.apache.xerces.xs.XSTypeDefinition#COMPLEX_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns list of xs:annotation from the element declaration or type
 * declaration.
 * 
 * @return list of xs:annotation from the element declaration or type
 *         declaration.
 */
private XSObjectList getAnnotations() {
	// Try get xs:annotation from the element declaration
	XSAttributeDeclaration attributeDeclaration = getAttrDeclaration();
	XSObjectList annotation = attributeDeclaration.getAnnotations();
	
	if (annotation != null && annotation.getLength() > 0) {
		return annotation;
	}
	// Try get xs:annotation from the type of element declaration
	XSTypeDefinition typeDefinition = attributeDeclaration.getTypeDefinition();
	if (typeDefinition == null) {
		return null;
	}
	if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
		return ((XSComplexTypeDecl) typeDefinition).getAnnotations();
	} else if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
		return ((XSSimpleTypeDecl) typeDefinition).getAnnotations();
	}
	return null;
}
 
Example 2
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns list of xs:annotation from the element declaration or type
 * declaration.
 * 
 * @return list of xs:annotation from the element declaration or type
 *         declaration.
 */
private XSObjectList getAnnotations() {
	// Try get xs:annotation from the element declaration
	XSObjectList annotation = elementDeclaration.getAnnotations();
	if (annotation != null && annotation.getLength() > 0) {
		return annotation;
	}
	// Try get xs:annotation from the type of element declaration
	XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
	if (typeDefinition == null) {
		return null;
	}
	if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
		return ((XSComplexTypeDecl) typeDefinition).getAnnotations();
	} else if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
		return ((XSSimpleTypeDecl) typeDefinition).getAnnotations();
	}
	return null;
}
 
Example 3
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns list of xs:annotation from the element declaration or type
 * declaration.
 * 
 * Indicated by:
 * https://msdn.microsoft.com/en-us/library/ms256143(v=vs.110).aspx
 * xs:attribute tags have content of either an xs:annotation or xs:simpleType
 * 
 * @return list of xs:annotation from the element declaration or type
 *         declaration.
 */
private XSObjectList getValueAnnotations() {
	// Try get xs:annotation from the element declaration
	XSAttributeDeclaration attributeDeclaration = getAttrDeclaration();
	XSSimpleTypeDefinition simpleTypeDefinition = attributeDeclaration.getTypeDefinition();
	XSSimpleTypeDecl simpleTypeDecl;
	

	XSObjectList annotation = null; // The XSD tag that holds the documentation tag

	if(simpleTypeDefinition instanceof XSSimpleTypeDecl) {
		simpleTypeDecl = (XSSimpleTypeDecl) simpleTypeDefinition;
		XSObjectList multiFacets = simpleTypeDecl.getMultiValueFacets();
		if(!multiFacets.isEmpty()) {
			XSMultiValueFacet facet = (XSMultiValueFacet) multiFacets.get(0);
			multiFacets = facet.getAnnotations();
			Object[] annotationArray = multiFacets.toArray();
			if(!onlyContainsNull(annotationArray)) { // if multiValueFacets has annotations
				annotation = simpleTypeDecl.getMultiValueFacets();
			}
		}
	}
	if(annotation == null){ // There was no specific documentation for the value, so use the general attribute documentation
		annotation = attributeDeclaration.getAnnotations();
	}
	if (annotation != null && annotation.getLength() > 0) {
		return annotation;
	}
	// Try get xs:annotation from the type of element declaration
	XSTypeDefinition typeDefinition = attributeDeclaration.getTypeDefinition();
	if (typeDefinition == null) {
		return null;
	}
	if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
		return ((XSComplexTypeDecl) typeDefinition).getAnnotations();
	} else if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) {
		return ((XSSimpleTypeDecl) typeDefinition).getAnnotations();
	}
	return null;
}
 
Example 4
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private void collectAttributesDeclaration(XSElementDeclaration elementDecl,
		Collection<CMAttributeDeclaration> attributes) {
	XSTypeDefinition typeDefinition = elementDecl.getTypeDefinition();
	switch (typeDefinition.getTypeCategory()) {
	case XSTypeDefinition.SIMPLE_TYPE:
		// TODO...
		break;
	case XSTypeDefinition.COMPLEX_TYPE:
		collectAttributesDeclaration((XSComplexTypeDefinition) typeDefinition, attributes);
		break;
	}
}
 
Example 5
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private void collectElementsDeclaration(XSElementDeclaration elementDecl,
		Collection<CMElementDeclaration> elements) {
	XSTypeDefinition typeDefinition = elementDecl.getTypeDefinition();
	switch (typeDefinition.getTypeCategory()) {
	case XSTypeDefinition.SIMPLE_TYPE:
		// TODO...
		break;
	case XSTypeDefinition.COMPLEX_TYPE:
		collectElementsDeclaration((XSComplexTypeDefinition) typeDefinition, elements);
		break;
	}
}
 
Example 6
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isEmpty() {
	XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
	if (typeDefinition != null && typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
		XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition) typeDefinition;
		return complexTypeDefinition.getContentType() == XSComplexTypeDefinition.CONTENTTYPE_EMPTY;
	}
	return false;
}
 
Example 7
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
public JsonObject getDefinition(XSTypeDefinition typeDefinition, boolean shouldCreateReferences) {
	JsonObjectBuilder builder = Json.createObjectBuilder();

	switch (typeDefinition.getTypeCategory()) {
	case XSTypeDefinition.COMPLEX_TYPE:
		XSComplexTypeDefinition complexTypeDefinition = (XSComplexTypeDefinition)typeDefinition;
		switch (complexTypeDefinition.getContentType()) {
		case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
			if (log.isTraceEnabled()) log.trace("getDefinition complexTypeDefinition.contentType is Empty, no child elements");
			break;
		case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
			if (log.isTraceEnabled()) log.trace("getDefinition complexTypeDefinition.contentType is Simple, no child elements (only characters)");
			handleComplexTypeDefinitionOfSimpleContentType(complexTypeDefinition, shouldCreateReferences, builder);
			break;
		case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
			if (log.isTraceEnabled()) log.trace("getDefinition complexTypeDefinition.contentType is Element, complexTypeDefinition ["+ToStringBuilder.reflectionToString(complexTypeDefinition,ToStringStyle.MULTI_LINE_STYLE)+"]");
			handleComplexTypeDefinitionOfElementContentType(complexTypeDefinition, shouldCreateReferences, builder);
			break;
		case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
			if (log.isTraceEnabled()) log.trace("getDefinition complexTypeDefinition.contentType is Mixed");
			handleComplexTypeDefinitionOfSimpleContentType(complexTypeDefinition, shouldCreateReferences, builder);
			break;
		default:
			throw new IllegalStateException("getDefinition complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but ["+complexTypeDefinition.getContentType()+"]");
		}
		if (log.isTraceEnabled()) log.trace(ToStringBuilder.reflectionToString(complexTypeDefinition,ToStringStyle.MULTI_LINE_STYLE));
		break;
	case XSTypeDefinition.SIMPLE_TYPE:
		handleSimpleTypeDefinition(typeDefinition, builder);
		break;
	default:
		throw new IllegalStateException("getDefinition typeDefinition.typeCategory is not Complex or Simple, but ["+typeDefinition.getTypeCategory()+"]");
	}
	return builder.build();
}
 
Example 8
Source File: JsonElementContainer.java    From iaf with Apache License 2.0 5 votes vote down vote up
public JsonElementContainer(String name, boolean xmlArrayContainer, boolean repeatedElement, boolean skipArrayElementContainers, String attributePrefix, String mixedContentLabel, XSTypeDefinition typeDefinition) {
	this.name=name;
	this.xmlArrayContainer=xmlArrayContainer;
	this.repeatedElement=repeatedElement;
	this.skipArrayElementContainers=skipArrayElementContainers;
	this.attributePrefix=attributePrefix;
	this.mixedContentLabel=mixedContentLabel;
	if (typeDefinition!=null) {
		switch(typeDefinition.getTypeCategory()) {
		case XSTypeDefinition.SIMPLE_TYPE:
			setType(ScalarType.findType(((XSSimpleType)typeDefinition)));
			break;
		case XSTypeDefinition.COMPLEX_TYPE:
			XSComplexTypeDefinition complexTypeDefinition=(XSComplexTypeDefinition)typeDefinition;
			switch (complexTypeDefinition.getContentType()) {
			case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
				if (log.isTraceEnabled()) log.trace("JsonElementContainer complexTypeDefinition.contentType is Empty, no child elements");
				break;
			case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
				if (log.isTraceEnabled()) log.trace("JsonElementContainer complexTypeDefinition.contentType is Simple, no child elements (only characters)");
				setType(ScalarType.findType((XSSimpleType)complexTypeDefinition.getBaseType()));
				break;
			case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
				if (log.isTraceEnabled()) log.trace("JsonElementContainer complexTypeDefinition.contentType is Element");
				break;
			case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
				if (log.isTraceEnabled()) log.trace("JsonElementContainer complexTypeDefinition.contentType is Mixed");
				break;
			}
		}
	}
}
 
Example 9
Source File: ToXml.java    From iaf with Apache License 2.0 5 votes vote down vote up
public void handleElementContents(XSElementDeclaration elementDeclaration, N node) throws SAXException {
	XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
	if (typeDefinition==null) {
		log.warn("handleElementContents typeDefinition is null");
		handleSimpleTypedElement(elementDeclaration, null, node);
		return;
	}
	switch (typeDefinition.getTypeCategory()) {
	case XSTypeDefinition.SIMPLE_TYPE:
		if (log.isTraceEnabled()) log.trace("handleElementContents typeDefinition.typeCategory is SimpleType, no child elements");
		handleSimpleTypedElement(elementDeclaration, (XSSimpleTypeDefinition)typeDefinition, node);
		return;
	case XSTypeDefinition.COMPLEX_TYPE:
		XSComplexTypeDefinition complexTypeDefinition=(XSComplexTypeDefinition)typeDefinition;
		switch (complexTypeDefinition.getContentType()) {
		case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
			if (log.isTraceEnabled()) log.trace("handleElementContents complexTypeDefinition.contentType is Empty, no child elements");
			return;
		case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
			if (log.isTraceEnabled()) log.trace("handleElementContents complexTypeDefinition.contentType is Simple, no child elements (only characters)");
			handleSimpleTypedElement(elementDeclaration, null, node);
			return;
		case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
		case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
			handleComplexTypedElement(elementDeclaration,node);
			return;
		default:
			throw new IllegalStateException("handleElementContents complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but ["+complexTypeDefinition.getContentType()+"]");
		}
	default:
		throw new IllegalStateException("handleElementContents typeDefinition.typeCategory is not SimpleType or ComplexType, but ["+typeDefinition.getTypeCategory()+"] class ["+typeDefinition.getClass().getName()+"]");
	}
}
 
Example 10
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Collection<CMElementDeclaration> getPossibleElements(DOMElement parentElement, int offset) {
	XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
	if (typeDefinition != null && typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) {
		// The type definition is complex (ex: xs:all; xs:sequence), returns list of
		// element declaration according those XML Schema constraints

		// Initialize Xerces validator
		CMBuilder cmBuilder = new CMBuilder(new CMNodeFactory());
		XSCMValidator validator = ((XSComplexTypeDecl) typeDefinition).getContentModel(cmBuilder);
		if (validator == null) {
			return Collections.emptyList();
		}

		SubstitutionGroupHandler handler = new SubstitutionGroupHandler(document);
		// Compute list of child element (QName)
		List<QName> qNames = toQNames(parentElement, offset);
		// Loop for each element (QName) and check if it is valid according the XML
		// Schema constraint
		int[] states = validator.startContentModel();
		for (QName elementName : qNames) {
			Object decl = validator.oneTransition(elementName, states, handler);
			if (decl == null) {
				return Collections.emptyList();
			}
		}

		// At this step, all child elements are valid, the call of
		// XSCMValidator#oneTransition has updated the states flag.
		// Collect the next valid elements according the XML Schema constraints
		Vector<?> result = validator.whatCanGoHere(states);
		if (result.isEmpty()) {
			return Collections.emptyList();
		}

		// Compute list of possible elements
		Collection<CMElementDeclaration> possibleElements = new HashSet<>();
		for (Object object : result) {
			if (object instanceof XSElementDeclaration) {
				XSElementDeclaration elementDecl = (XSElementDeclaration) object;
				document.collectElement(elementDecl, possibleElements);
				// Collect substitution group
				XSObjectList group = document.getSubstitutionGroup(elementDecl);
				if (group != null) {
					for (int i = 0; i < group.getLength(); i++) {
						XSElementDeclaration o = (XSElementDeclaration) group.item(i);
						document.collectElement(o, possibleElements);
					}
				}
			} else {
				// case with xs:any. Ex:
				// <xs:sequence>
				// <xs:any maxOccurs="2" processContents="lax" />
				// </xs:sequence>
				Collection<CMElementDeclaration> anyElements = getXSAnyElements(object);
				if (anyElements != null) {
					return anyElements;
				}
			}
		}
		return possibleElements;
	}
	return getElements();
}
 
Example 11
Source File: ToXml.java    From iaf with Apache License 2.0 4 votes vote down vote up
public List<XSParticle> getBestChildElementPath(XSElementDeclaration elementDeclaration, N node, boolean silent) throws SAXException {
		XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
		if (typeDefinition==null) {
			log.warn("getBestChildElementPath typeDefinition is null");
			return null;
		}
		switch (typeDefinition.getTypeCategory()) {
		case XSTypeDefinition.SIMPLE_TYPE:
			if (log.isTraceEnabled()) log.trace("getBestChildElementPath typeDefinition.typeCategory is SimpleType, no child elements");
			return null;
		case XSTypeDefinition.COMPLEX_TYPE:
			XSComplexTypeDefinition complexTypeDefinition=(XSComplexTypeDefinition)typeDefinition;
			switch (complexTypeDefinition.getContentType()) {
			case XSComplexTypeDefinition.CONTENTTYPE_EMPTY:
				if (log.isTraceEnabled()) log.trace("getBestChildElementPath complexTypeDefinition.contentType is Empty, no child elements");
				return null;
			case XSComplexTypeDefinition.CONTENTTYPE_SIMPLE:
				if (log.isTraceEnabled()) log.trace("getBestChildElementPath complexTypeDefinition.contentType is Simple, no child elements (only characters)");
				return null;
			case XSComplexTypeDefinition.CONTENTTYPE_ELEMENT:
			case XSComplexTypeDefinition.CONTENTTYPE_MIXED:
				XSParticle particle = complexTypeDefinition.getParticle();
				if (particle==null) {
					throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.particle is null for Element or Mixed contentType");
//					log.warn("typeDefinition particle is null, is this a problem?");
//					return null;
				} 
				if (log.isTraceEnabled()) log.trace("typeDefinition particle ["+ToStringBuilder.reflectionToString(particle,ToStringStyle.MULTI_LINE_STYLE)+"]");
				List<XSParticle> result=new LinkedList<XSParticle>();
				List<String> failureReasons=new LinkedList<String>();
				if (getBestMatchingElementPath(elementDeclaration, node, particle, result, failureReasons)) {
					return result;
				}
				String msg="Cannot find path:";
				for (String reason:failureReasons) {
					msg+='\n'+reason;
				}
				if (!silent) {
					handleError(msg);
				}
				return null;
			default:
				throw new IllegalStateException("getBestChildElementPath complexTypeDefinition.contentType is not Empty,Simple,Element or Mixed, but ["+complexTypeDefinition.getContentType()+"]");
			}
		default:
			throw new IllegalStateException("getBestChildElementPath typeDefinition.typeCategory is not SimpleType or ComplexType, but ["+typeDefinition.getTypeCategory()+"] class ["+typeDefinition.getClass().getName()+"]");
		}
	}