org.apache.xerces.xs.XSTypeDefinition Java Examples

The following examples show how to use org.apache.xerces.xs.XSTypeDefinition. 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: DatatypeMappingTest.java    From exificient with MIT License 6 votes vote down vote up
public static Datatype getSimpleDatatypeFor(String schemaAsString,
		String typeName, String typeURI) throws EXIException {
	XSDGrammarsBuilder xsdGB = XSDGrammarsBuilder.newInstance();
	ByteArrayInputStream bais = new ByteArrayInputStream(
			schemaAsString.getBytes());
	xsdGB.loadGrammars(bais);
	xsdGB.toGrammars();

	XSModel xsModel = xsdGB.getXSModel();

	XSTypeDefinition td = xsModel.getTypeDefinition(typeName, typeURI);

	assertTrue("SimpleType expected",
			td.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE);

	Datatype dt = xsdGB.getDatatype((XSSimpleTypeDefinition) td);

	return dt;
}
 
Example #4
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void buildSkippableArrayContainer(XSParticle childParticle, JsonObjectBuilder builder){
	JsonObjectBuilder refBuilder = Json.createObjectBuilder();
	handleParticle(refBuilder,childParticle,null, false);

	XSTerm childTerm = childParticle.getTerm();
	if( childTerm instanceof XSElementDeclaration ){
		XSElementDeclaration elementDeclaration=(XSElementDeclaration) childTerm;
		XSTypeDefinition elementTypeDefinition = elementDeclaration.getTypeDefinition();
		JsonStructure definition =getDefinition(elementTypeDefinition, true);
	
		builder.add("type", "array");
		if (elementDeclaration.getNillable()) {
			definition=nillable(definition);
		}
		builder.add("items", definition);
	}
}
 
Example #5
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
public JsonObject getDefinitions() {
	JsonObjectBuilder definitionsBuilder = Json.createObjectBuilder();
	for (XSModel model:models) {
		XSNamedMap elements = model.getComponents(XSConstants.ELEMENT_DECLARATION);
		for (int i=0; i<elements.getLength(); i++) {
			XSElementDeclaration elementDecl = (XSElementDeclaration)elements.item(i);
			handleElementDeclaration(definitionsBuilder, elementDecl, false, false);
		}
		XSNamedMap types = model.getComponents(XSConstants.TYPE_DEFINITION);
		for (int i=0; i<types.getLength(); i++) {
			XSTypeDefinition typeDefinition = (XSTypeDefinition)types.item(i);
			String typeNamespace = typeDefinition.getNamespace();
			if (typeNamespace==null || !typeDefinition.getNamespace().equals(XML_SCHEMA_NS)) {
				definitionsBuilder.add(typeDefinition.getName(), getDefinition(typeDefinition, false));
			}
		}
	}
	return definitionsBuilder.build();
}
 
Example #6
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 #7
Source File: ToXml.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected void processChildElement(N node, String parentName, XSElementDeclaration childElementDeclaration, boolean mandatory, Set<String> processedChildren) throws SAXException {
		String childElementName = childElementDeclaration.getName(); 
		if (log.isTraceEnabled()) log.trace("To2Xml.processChildElement() parent name ["+parentName+"] childElementName ["+childElementName+"]");
		Iterable<N> childNodes = getChildrenByName(node,childElementDeclaration);
		boolean childSeen=false;
		if (childNodes!=null) {
			childSeen=true;
			int i=0;
			for (N childNode:childNodes) {
				i++;
				handleElement(childElementDeclaration,childNode);
			}
			if (log.isTraceEnabled()) log.trace("processed ["+i+"] children found by name ["+childElementName+"] in ["+parentName+"]");
			if (i==0 && isDeepSearch() && childElementDeclaration.getTypeDefinition().getTypeCategory()!=XSTypeDefinition.SIMPLE_TYPE) {
				if (log.isTraceEnabled()) log.trace("no children processed, and deepSearch, not a simple type therefore handle node ["+childElementName+"] in ["+parentName+"]");
				handleElement(childElementDeclaration,node);
				childSeen=true;
			}
		} else {
			if (log.isTraceEnabled()) log.trace("no children found by name ["+childElementName+"] in ["+parentName+"]");
			if (isDeepSearch() && childElementDeclaration.getTypeDefinition().getTypeCategory()!=XSTypeDefinition.SIMPLE_TYPE) {
				if (log.isTraceEnabled()) log.trace("no children found, and deepSearch, not a simple type therefore handle node ["+childElementName+"] in ["+parentName+"]");
				handleElement(childElementDeclaration,node);
				childSeen=true;
			}
		}
		if (childSeen) {
			if (processedChildren.contains(childElementName)) {
				throw new IllegalStateException("child element ["+childElementName+"] already processed for node ["+parentName+"]");
			}
			processedChildren.add(childElementName);
		}
//		if (!childSeen && mandatory && isAutoInsertMandatory()) {
//			if (log.isDebugEnabled()) log.debug("inserting mandatory element ["+childElementName+"]");
//			handleElement(childElementDeclaration,node); // insert node when minOccurs > 0, and no node is present
//		}
	}
 
Example #8
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 #9
Source File: XmlAligner.java    From iaf with Apache License 2.0 5 votes vote down vote up
public XSTypeDefinition getTypeDefinition(PSVIProvider psviProvider) {
	ElementPSVI elementPSVI = psviProvider.getElementPSVI();
	if (log.isTraceEnabled()) log.trace("getTypeDefinition() elementPSVI ["+ToStringBuilder.reflectionToString(elementPSVI)+"]");
	XSElementDeclaration elementDeclaration = elementPSVI.getElementDeclaration();
	if (log.isTraceEnabled()) log.trace("getTypeDefinition() elementPSVI element declaration ["+ToStringBuilder.reflectionToString(elementDeclaration)+"]");
	if (elementDeclaration==null) {
		return null;
	}
	XSTypeDefinition typeDefinition = elementDeclaration.getTypeDefinition();
	if (log.isTraceEnabled()) log.trace("getTypeDefinition() elementDeclaration typeDefinition ["+ToStringBuilder.reflectionToString(typeDefinition)+"]");
	return typeDefinition;
}
 
Example #10
Source File: XmlAligner.java    From iaf with Apache License 2.0 5 votes vote down vote up
public XSObjectList getAttributeUses(XSTypeDefinition typeDefinition) {
	if (typeDefinition==null) {
		if (log.isTraceEnabled()) log.trace("getAttributeUses typeDefinition is null");
		return null;
	}
	if (typeDefinition instanceof XSComplexTypeDefinition) {
		XSComplexTypeDefinition complexTypeDefinition=(XSComplexTypeDefinition)typeDefinition;
		return complexTypeDefinition.getAttributeUses();
	} 
	if (log.isTraceEnabled()) log.trace("typeDefinition ["+typeDefinition.getClass().getSimpleName()+"] SimpleType, no attributes");
	return null;
}
 
Example #11
Source File: MapContentContainer.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
	public void startElementGroup(String localName, boolean xmlArrayContainer, boolean repeatedElement, XSTypeDefinition typeDefinition) {
//		if (repeatedElement) {
//			Object entry=data.get(localName);
//			if (entry!=null) {
//				if (entry instanceof List) {
//					return;
//				}
//				throw new IllegalStateException("expected list for ["+localName+"] but was ["+entry+"]");
//			}
//			data.put(localName, new ArrayList<String>());
//		}
	}
 
Example #12
Source File: MapContentContainer.java    From iaf with Apache License 2.0 5 votes vote down vote up
@Override
public void startElement(String localName, boolean xmlArrayContainer, boolean repeatedElement, XSTypeDefinition typeDefinition) {
	currentName=localName;
	currentValue=null;
	currentIsNull=false;
	if (typeDefinition instanceof XSSimpleType) {
		type=(ScalarType.findType(((XSSimpleType)typeDefinition)));
	}
}
 
Example #13
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 #14
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void handleElementDeclaration(JsonObjectBuilder builder, XSElementDeclaration elementDeclaration, boolean multiOccurring, boolean shouldCreateReferences){	
	String elementName=elementDeclaration.getName();
	//if (log.isTraceEnabled()) log.trace("XSElementDeclaration name ["+elementName+"]");
	if (log.isTraceEnabled()) log.trace("XSElementDeclaration element ["+elementName+"]["+ToStringBuilder.reflectionToString(elementDeclaration,ToStringStyle.MULTI_LINE_STYLE)+"]");

	XSTypeDefinition elementTypeDefinition = elementDeclaration.getTypeDefinition();
	JsonStructure definition;
	if (elementTypeDefinition.getAnonymous() || XML_SCHEMA_NS.equals(elementTypeDefinition.getNamespace())) {
		definition =getDefinition(elementTypeDefinition, shouldCreateReferences);
	} else {
		definition = Json.createObjectBuilder().add("$ref",definitionsPath+elementTypeDefinition.getName()).build();
	}
	if (elementDeclaration.getNillable()) {
		definition=nillable(definition);
	}
	if (multiOccurring) {
		JsonObjectBuilder arrayBuilder = Json.createObjectBuilder();
		arrayBuilder.add("type", "array");
		arrayBuilder.add("items", definition);

		builder.add(elementName, arrayBuilder.build());
	} else {
		if (definition!=null) {
			builder.add(elementName, definition);
		}
	}
	
}
 
Example #15
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void handleSimpleTypeDefinition(XSTypeDefinition typeDefinition, JsonObjectBuilder builder){
	XSSimpleTypeDefinition simpleTypeDefinition = (XSSimpleTypeDefinition)typeDefinition;
	if (log.isTraceEnabled()) log.trace("typeDefinition.name ["+typeDefinition.getName()+"]");
	if (log.isTraceEnabled()) log.trace("simpleTypeDefinition.getBuiltInKind ["+simpleTypeDefinition.getBuiltInKind()+"]");
	if (log.isTraceEnabled()) log.trace(ToStringBuilder.reflectionToString(typeDefinition,ToStringStyle.MULTI_LINE_STYLE));

	short builtInKind = simpleTypeDefinition.getBuiltInKind();
	String dataType = getJsonDataType(builtInKind);
	
	if (dataType.equalsIgnoreCase("integer") || dataType.equalsIgnoreCase("number")) {
		builder.add("type", dataType.toLowerCase());

		applyFacet(simpleTypeDefinition, builder, "maximum", XSSimpleTypeDefinition.FACET_MAXINCLUSIVE);
		applyFacet(simpleTypeDefinition, builder, "minimum", XSSimpleTypeDefinition.FACET_MININCLUSIVE);
		applyFacet(simpleTypeDefinition, builder, "exclusiveMaximum", XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE);
		applyFacet(simpleTypeDefinition, builder, "exclusiveMinimum", XSSimpleTypeDefinition.FACET_MINEXCLUSIVE);
		applyFacet(simpleTypeDefinition, builder, "enum", XSSimpleTypeDefinition.FACET_ENUMERATION);
	} else if (dataType.equalsIgnoreCase("boolean")) {
		builder.add("type", "boolean");
	} else if (dataType.equalsIgnoreCase("string")) {	
		builder.add("type", "string");
	
		applyFacet(simpleTypeDefinition, builder, "maxLength", XSSimpleTypeDefinition.FACET_MAXLENGTH);
		applyFacet(simpleTypeDefinition, builder, "minLength", XSSimpleTypeDefinition.FACET_MINLENGTH);
		applyFacet(simpleTypeDefinition, builder, "pattern", XSSimpleTypeDefinition.FACET_PATTERN);
		applyFacet(simpleTypeDefinition, builder, "enum", XSSimpleTypeDefinition.FACET_ENUMERATION);
	} else if (dataType.equalsIgnoreCase("date") || dataType.equalsIgnoreCase("date-time") || dataType.equalsIgnoreCase("time")) {		
		builder.add("type", "string");
		
		builder.add("format", dataType);

		applyFacet(simpleTypeDefinition, builder, "pattern", XSSimpleTypeDefinition.FACET_PATTERN);
		applyFacet(simpleTypeDefinition, builder, "enum", XSSimpleTypeDefinition.FACET_ENUMERATION);
	}
}
 
Example #16
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 #17
Source File: AlignmentContext.java    From iaf with Apache License 2.0 5 votes vote down vote up
public AlignmentContext(AlignmentContext parent, String namespaceUri, String localName, String qName,
		Attributes attributes, XSTypeDefinition typeDefinition, int indentLevel,
		Set<String> multipleOccurringChildElements, boolean parentOfSingleMultipleOccurringChildElement) {
	super();
	this.parent = parent;
	this.namespaceUri = namespaceUri;
	this.localName = localName;
	this.qName = qName;
	this.attributes = attributes;
	this.typeDefinition = typeDefinition;
	this.indentLevel = indentLevel;
	this.multipleOccurringChildElements = multipleOccurringChildElements;
	this.parentOfSingleMultipleOccurringChildElement = parentOfSingleMultipleOccurringChildElement;
}
 
Example #18
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 #19
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
private static boolean includesBaseType(XSTypeDefinition type,
      String type_name)
{
   XSTypeDefinition base_type = type.getBaseType();

   if (base_type != null)
   {
      if (base_type.getName().equals(type_name))
         return true;
      else
         return includesBaseType(base_type, type_name);
   }
   else
      return false;
}
 
Example #20
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 #21
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 #22
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings ("unused")
private static XSTypeDefinition getBaseType(XSTypeDefinition type)
{
   if ("http://www.w3.org/2001/XMLSchema".equals(type.getNamespace()))
      return type;

   XSTypeDefinition base_type = type.getBaseType();

   if (base_type != null)
   {
      return getBaseType(base_type);
   }
   else
      return type;
}
 
Example #23
Source File: TreeContentContainer.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public void startElement(String localName, boolean xmlArrayContainer, boolean repeatedElement, XSTypeDefinition typeDefinition) {
	elementStack.push(elementContainer);
	elementContainer=createElementContainer(localName, xmlArrayContainer, repeatedElement, typeDefinition);
}
 
Example #24
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()+"]");
		}
	}
 
Example #25
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 #26
Source File: ToXml.java    From iaf with Apache License 2.0 4 votes vote down vote up
public void handleElement(XSElementDeclaration elementDeclaration, N node) throws SAXException {
		String name = elementDeclaration.getName();
		String elementNamespace=elementDeclaration.getNamespace();
		String qname=getQName(elementNamespace, name);
		if (log.isTraceEnabled()) log.trace("handleNode() name ["+name+"] elementNamespace ["+elementNamespace+"]");
		newLine();
		AttributesImpl attributes=new AttributesImpl();
		Map<String,String> nodeAttributes = getAttributes(elementDeclaration, node);
		if (log.isTraceEnabled()) log.trace("node ["+name+"] search for attributeDeclaration");
		XSTypeDefinition typeDefinition=elementDeclaration.getTypeDefinition();
		XSObjectList attributeUses=getAttributeUses(typeDefinition);
		if (attributeUses==null || attributeUses.getLength()==0) {
			if (nodeAttributes!=null && nodeAttributes.size()>0) {
				log.warn("node ["+name+"] found ["+nodeAttributes.size()+"] attributes, but no declared AttributeUses");
			} else {
				if (log.isTraceEnabled()) log.trace("node ["+name+"] no attributeUses, no attributes");
			}
		} else {
			if (nodeAttributes==null || nodeAttributes.isEmpty()) {
				log.warn("node ["+name+"] declared ["+attributeUses.getLength()+"] attributes, but no attributes found");
			} else {
				for (int i=0;i<attributeUses.getLength(); i++) {
					XSAttributeUse attributeUse=(XSAttributeUse)attributeUses.item(i);
					XSAttributeDeclaration attributeDeclaration=attributeUse.getAttrDeclaration();
					//XSSimpleTypeDefinition attTypeDefinition=attributeDeclaration.getTypeDefinition();
					//if (log.isTraceEnabled()) log.trace("node ["+name+"] attTypeDefinition ["+ToStringBuilder.reflectionToString(attTypeDefinition)+"]");
					String attName=attributeDeclaration.getName();
					if (nodeAttributes.containsKey(attName)) {
						String value=nodeAttributes.remove(attName);
						String uri=attributeDeclaration.getNamespace();
						String attqname=getQName(uri,attName);
						String type=null;
						if (log.isTraceEnabled()) log.trace("node ["+name+"] adding attribute ["+attName+"] value ["+value+"]");
						attributes.addAttribute(uri, attName, attqname, type, value);
					}
				}
			}
		}
		if (isNil(elementDeclaration, node)) {
			validatorHandler.startPrefixMapping(XSI_PREFIX_MAPPING, XML_SCHEMA_INSTANCE_NAMESPACE);
			attributes.addAttribute(XML_SCHEMA_INSTANCE_NAMESPACE, XML_SCHEMA_NIL_ATTRIBUTE, XSI_PREFIX_MAPPING+":"+XML_SCHEMA_NIL_ATTRIBUTE, "xs:boolean", "true");
			validatorHandler.startElement(elementNamespace, name, qname, attributes);
			validatorHandler.endElement(elementNamespace, name, qname);
			validatorHandler.endPrefixMapping(XSI_PREFIX_MAPPING);
		} else {
			validatorHandler.startElement(elementNamespace, name, qname, attributes);
			handleElementContents(elementDeclaration, node);
			validatorHandler.endElement(elementNamespace, name, qname);
		}
//		if (createdPrefix!=null) {
//			validatorHandler.endPrefixMapping(createdPrefix);
//		}
	}
 
Example #27
Source File: XmlAligner.java    From iaf with Apache License 2.0 4 votes vote down vote up
public XSTypeDefinition getTypeDefinition() {
	return typeDefinition;
}
 
Example #28
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void dumpElement(String path, XSElementDeclaration element)
   {
      String output_path = "" + path + "/" + element.getName();

      XSTypeDefinition type = element.getTypeDefinition();

      if (type.getName().endsWith("Array"))
      {
//         System.err.println(element.getName() + " - " + type.getName()
//            + " SKIPPED !");
         return;
      }

      if (((type.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE)
            || ((XSComplexTypeDefinition) type)
            .getContentType() == XSComplexTypeDefinition.CONTENTTYPE_SIMPLE))
      {
         if (includesBaseType(type, "string")
               || includesBaseType(type, "integer")
               || includesBaseType(type, "boolean"))
         {
//            System.out.println("         <metadata name=\"" +
// element.getName() + "\" type=\"text/plain\" category=\"\">");
//            System.out.println("            " +
// indexedName(element.getName())
//               + " { local:values($doc" + output_path + ") }");
//            System.out.println("         </metadata>,");

            System.out.println("         local:getMetadata('" +
                  element.getName() + "', '" +
                  indexedName(element.getName()) + "',");
            System.out.println("            $doc" + output_path + "),");
         }
      }
      else
      {
         dumpParticle(output_path,
               ((XSComplexTypeDefinition)type).getParticle());
      }

   }
 
Example #29
Source File: JsonDocumentContainer.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
protected JsonElementContainer createElementContainer(String localName, boolean xmlArrayContainer, boolean repeatedElement, XSTypeDefinition typeDefinition) {
	return new JsonElementContainer(localName, xmlArrayContainer, repeatedElement, skipArrayElementContainers, attributePrefix, mixedContentLabel, typeDefinition);
}
 
Example #30
Source File: XmlTo.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
	boolean xmlArrayContainer=aligner.isParentOfSingleMultipleOccurringChildElement();
	boolean repeatedElement=aligner.isMultipleOccurringChildInParentElement(localName);
	XSTypeDefinition typeDefinition=aligner.getTypeDefinition();
	if (!localName.equals(topElement)) {
		if (topElement!=null) {
			if (log.isTraceEnabled()) log.trace("endElementGroup ["+topElement+"]");
			documentContainer.endElementGroup(topElement);	
		}
		if (log.isTraceEnabled()) log.trace("startElementGroup ["+localName+"]");
		documentContainer.startElementGroup(localName, xmlArrayContainer, repeatedElement, typeDefinition);	
		topElement=localName;			
	}
	element.push(topElement);
	topElement=null;
	if (log.isTraceEnabled()) log.trace("startElement ["+localName+"] xml array container ["+aligner.isParentOfSingleMultipleOccurringChildElement()+"] repeated element ["+aligner.isMultipleOccurringChildInParentElement(localName)+"]");
	documentContainer.startElement(localName,xmlArrayContainer,repeatedElement, typeDefinition);
	super.startElement(uri, localName, qName, atts);
	if (aligner.isNil(atts)) {
		documentContainer.setNull();
	} else {
		if (writeAttributes) {
			XSObjectList attributeUses=aligner.getAttributeUses();
			if (attributeUses==null) {
				if (atts.getLength()>0) {
					log.warn("found ["+atts.getLength()+"] attributes, but no declared AttributeUses");
				}
			} else {
				for (int i=0;i<attributeUses.getLength(); i++) {
					XSAttributeUse attributeUse=(XSAttributeUse)attributeUses.item(i);
					XSAttributeDeclaration attributeDeclaration=attributeUse.getAttrDeclaration();
					XSSimpleTypeDefinition attTypeDefinition=attributeDeclaration.getTypeDefinition();
					String attName=attributeDeclaration.getName();
					String attNS=attributeDeclaration.getNamespace();
					if (log.isTraceEnabled()) log.trace("startElement ["+localName+"] searching attribute ["+attNS+":"+attName+"]");
					int attIndex=attNS!=null? atts.getIndex(attNS, attName):atts.getIndex(attName);
					if (attIndex>=0) {
						String value=atts.getValue(attIndex);
						if (log.isTraceEnabled()) log.trace("startElement ["+localName+"] attribute ["+attNS+":"+attName+"] value ["+value+"]");
						if (StringUtils.isNotEmpty(value)) {
							documentContainer.setAttribute(attName, value, attTypeDefinition);
						}
					}
				}
			}
		}
	}
}