Java Code Examples for org.apache.xerces.xs.XSObjectList#item()

The following examples show how to use org.apache.xerces.xs.XSObjectList#item() . 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: XmlAligner.java    From iaf with Apache License 2.0 6 votes vote down vote up
protected void collectChildElements(XSParticle particle, Set<String> elementNames) {
	if (particle==null) {
		log.warn("collectChildElements() particle is null, is this a problem?");	
		return;
	}
	XSTerm term = particle.getTerm();
	if (term==null) {
		throw new IllegalStateException("collectChildElements particle.term is null");
	} 
	if (term instanceof XSModelGroup) {
		XSModelGroup modelGroup = (XSModelGroup)term;
		XSObjectList particles = modelGroup.getParticles();
		for (int i=0;i<particles.getLength();i++) {
			XSParticle childParticle = (XSParticle)particles.item(i);
			collectChildElements(childParticle, elementNames);
		}
		return;
	} 
	if (term instanceof XSElementDeclaration) {
		XSElementDeclaration elementDeclaration=(XSElementDeclaration)term;
		String elementName=elementDeclaration.getName();
		if (log.isTraceEnabled()) log.trace("collectChildElements() ElementDeclaration name ["+elementName+"]");
		elementNames.add(elementName);
	}
	return;
}
 
Example 2
Source File: XmlAligner.java    From iaf with Apache License 2.0 6 votes vote down vote up
protected Set<String> findMultipleOccurringChildElements(XSParticle particle) {
	Set<String> result=new HashSet<String>();
	if (particle==null) {
		log.warn("findMultipleOccurringChildElements() typeDefinition particle is null, is this a problem?");	
		return result;
	}
	XSTerm term = particle.getTerm();
	if (term==null) {
		throw new IllegalStateException("findMultipleOccurringChildElements particle.term is null");
	} 
	if (log.isTraceEnabled()) log.trace("findMultipleOccurringChildElements() term name ["+term.getName()+"] occurring unbounded ["+particle.getMaxOccursUnbounded()+"] max occur ["+particle.getMaxOccurs()+"] term ["+ToStringBuilder.reflectionToString(term)+"]");
	if (particle.getMaxOccursUnbounded()||particle.getMaxOccurs()>1) {
		collectChildElements(particle,result);
		return result;
	} 
	if (term instanceof XSModelGroup) {
		XSModelGroup modelGroup = (XSModelGroup)term;
		XSObjectList particles = modelGroup.getParticles();
			if (log.isTraceEnabled()) log.trace("findMultipleOccurringChildElements() modelGroup particles ["+ToStringBuilder.reflectionToString(particles)+"]");
			for (int i=0;i<particles.getLength();i++) {
				XSParticle childParticle = (XSParticle)particles.item(i);
				result.addAll(findMultipleOccurringChildElements(childParticle));
			}
	} 
	return result;
}
 
Example 3
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Fill the given elements list from the given Xerces elementDeclaration
 * 
 * @param elementDeclaration
 * @param elements
 */
void collectElement(XSElementDeclaration elementDeclaration, Collection<CMElementDeclaration> elements) {
	if (elementDeclaration.getAbstract()) {
		// element declaration is marked as abstract
		// ex with xsl: <xs:element name="declaration" type="xsl:generic-element-type"
		// abstract="true"/>
		XSObjectList list = getSubstitutionGroup(elementDeclaration);
		if (list != null) {
			// it exists elements list bind with this abstract declaration with
			// substitutionGroup
			// ex xsl : <xs:element name="template" substitutionGroup="xsl:declaration">
			for (int i = 0; i < list.getLength(); i++) {
				XSObject object = list.item(i);
				if (object.getType() == XSConstants.ELEMENT_DECLARATION) {
					XSElementDeclaration subElementDeclaration = (XSElementDeclaration) object;
					collectElement(subElementDeclaration, elements);
				}
			}
		}
	} else {
		CMElementDeclaration cmElement = getXSDElement(elementDeclaration);
		// check element declaration is not already added (ex: xs:annotation)
		if (!elements.contains(cmElement)) {
			elements.add(cmElement);
		}
	}
}
 
Example 4
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private void collectAttributesDeclaration(XSComplexTypeDefinition typeDefinition,
		Collection<CMAttributeDeclaration> attributes) {
	XSObjectList list = typeDefinition.getAttributeUses();
	if (list != null) {
		for (int i = 0; i < list.getLength(); i++) {
			XSObject object = list.item(i);
			if (object.getType() == XSConstants.ATTRIBUTE_USE) {
				XSAttributeUse attributeUse = (XSAttributeUse) object;
				attributes.add(new CMXSDAttributeDeclaration(attributeUse));
			}
		}
	}
}
 
Example 5
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
private void handleCompositorsAllAndSequence(JsonObjectBuilder builder, XSObjectList particles, XSObjectList attributeUses){
	if (log.isTraceEnabled()) log.trace("modelGroup COMPOSITOR_SEQUENCE or COMPOSITOR_ALL");
	if (skipArrayElementContainers && particles.getLength()==1) {
		XSParticle childParticle = (XSParticle)particles.item(0);
		if (childParticle.getMaxOccursUnbounded() || childParticle.getMaxOccurs()>1) {
			if (log.isTraceEnabled()) log.trace("skippable array element childParticle ["+ToStringBuilder.reflectionToString(particles.item(0),ToStringStyle.MULTI_LINE_STYLE)+"]");
			buildSkippableArrayContainer(childParticle, builder);
			return;
		}
	}
	buildObject(builder, particles, attributeUses, null, null);
}
 
Example 6
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 7
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);
						}
					}
				}
			}
		}
	}
}
 
Example 8
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 4 votes vote down vote up
private void buildObject(JsonObjectBuilder builder, XSObjectList particles, XSObjectList attributeUses, String textAttribute, XSTypeDefinition baseType){
	builder.add("type", "object");
	builder.add("additionalProperties", false);
	JsonObjectBuilder propertiesBuilder = Json.createObjectBuilder();
	List<String> requiredProperties = new ArrayList<String>();

	if (attributeUses!=null) {
		for (int i=0; i< attributeUses.getLength(); i++) {
			XSAttributeUse attributeUse = (XSAttributeUse)attributeUses.get(i);
			XSAttributeDeclaration attributeDecl = attributeUse.getAttrDeclaration();
			propertiesBuilder.add(attributePrefix+attributeDecl.getName(), getDefinition(attributeDecl.getTypeDefinition(), true));
		}
	}
	if (textAttribute!=null && ((attributeUses!=null && attributeUses.getLength()>0) || (particles!=null && particles.getLength()>0))) {
		JsonObject elementType = baseType!=null ? getDefinition(baseType, true) : Json.createObjectBuilder().add("type", "string").build();
		propertiesBuilder.add(textAttribute, elementType);
	}
	if (particles!=null) {
		for (int i=0;i<particles.getLength();i++) {
			XSParticle childParticle = (XSParticle)particles.item(i);
			if (log.isTraceEnabled()) log.trace("childParticle ["+i+"]["+ToStringBuilder.reflectionToString(childParticle,ToStringStyle.MULTI_LINE_STYLE)+"]");
		
			XSTerm childTerm = childParticle.getTerm();
			if (childTerm instanceof XSElementDeclaration) {
				XSElementDeclaration elementDeclaration = (XSElementDeclaration) childTerm;
				String elementName = elementDeclaration.getName();

				if(elementName != null && childParticle.getMinOccurs() != 0){
					requiredProperties.add(elementName);
				}
			}
			
			handleParticle(propertiesBuilder, childParticle, null, true);
		}
	}
	builder.add("properties", propertiesBuilder.build());
	if(requiredProperties.size() > 0){
		JsonArrayBuilder requiredPropertiesBuilder = Json.createArrayBuilder();
		for (String requiredProperty : requiredProperties) {
			requiredPropertiesBuilder.add(requiredProperty);
		}
		builder.add("required", requiredPropertiesBuilder.build());
	}
}
 
Example 9
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 4 votes vote down vote up
private void applyFacet(XSSimpleTypeDefinition simpleTypeDefinition, JsonObjectBuilder builder, String key, short facet){
	if(simpleTypeDefinition.getFacet(facet) != null){
		String lexicalFacetValue = simpleTypeDefinition.getLexicalFacetValue(facet);
		if(lexicalFacetValue != null){
			switch(facet){
				case XSSimpleTypeDefinition.FACET_MAXINCLUSIVE:
				case XSSimpleTypeDefinition.FACET_MININCLUSIVE:
				case XSSimpleTypeDefinition.FACET_MAXEXCLUSIVE:
				case XSSimpleTypeDefinition.FACET_MINEXCLUSIVE:
				case XSSimpleTypeDefinition.FACET_MAXLENGTH:
				case XSSimpleTypeDefinition.FACET_MINLENGTH:
					/*
						Not sure about this.. 

						simpleTypeDefinition.getLexicalFacetValue(facet) returns a numeric value as string
						if value > MAX_INT, Integer.parseInt(value) will throw NumberFormatException

						currently this exception is catched and retried as Long.ParseLong(value)
						but what if this throws NumberFormatException?

						how to deal with this properly?
						-----
						UPDATE:
						Tried parsing as long and logging the value when couldn't parse, appears to be a 20 digit numeric value
						which would require to use BigInteger

						What is the best method to do this? Try and catch int, long & then bigint or directly to big int?
					*/
					try {
						builder.add(key, Integer.parseInt(lexicalFacetValue));
					} catch (NumberFormatException nfe) {
						log.warn("Couldn't parse value ["+lexicalFacetValue+"] as Integer... retrying as Long");

						try {
							builder.add(key, Long.parseLong(lexicalFacetValue));
						} catch (NumberFormatException nfex) {
							log.warn("Couldn't parse value ["+lexicalFacetValue+"] as Long... retrying as BigInteger");
							
							try {
								builder.add(key, new BigInteger(lexicalFacetValue));
							} catch (NumberFormatException nfexx) {
								log.warn("Couldn't parse value ["+lexicalFacetValue+"] as BigInteger");
							}
						}
					}	
					break;
				default:
					// hmm never reaches this block?
					log.debug("Setting value ["+lexicalFacetValue+"] as String for facet ["+simpleTypeDefinition.getFacet(facet)+"]");
					builder.add(key, lexicalFacetValue);
					break;
			}
		} else if (facet == XSSimpleTypeDefinition.FACET_PATTERN || facet == XSSimpleTypeDefinition.FACET_ENUMERATION) {
			XSObjectList multiValuedFacets = simpleTypeDefinition.getMultiValueFacets();

			for (int i=0; i<multiValuedFacets.getLength(); i++) {
				XSMultiValueFacet multiValuedFacet = (XSMultiValueFacet) multiValuedFacets.item(i);

				if (log.isTraceEnabled()) log.trace("Inspecting single multi valued facet ["+multiValuedFacet+"] which is named ["+multiValuedFacet.getName()+"] which is of type ["+multiValuedFacet.getType()+"]");
				if (log.isTraceEnabled()) log.trace("Inspecting multiValuedFacet.getLexicalFacetValues() for ["+multiValuedFacet.getName()+"] which has value of ["+multiValuedFacet.getLexicalFacetValues()+"]");
				if (log.isTraceEnabled()) log.trace("Inspecting multiValuedFacet.getEnumerationValues() for ["+multiValuedFacet.getName()+"] which has value of ["+multiValuedFacet.getEnumerationValues()+"]");
				if (log.isTraceEnabled()) log.trace("Inspecting multiValuedFacet.getFacetKind() == enum for ["+multiValuedFacet.getName()+"] which has value of ["+(multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_ENUMERATION)+"]");
				if (log.isTraceEnabled()) log.trace("Inspecting multiValuedFacet.getFacetKind() == pattern for ["+multiValuedFacet.getName()+"] which has value of ["+(multiValuedFacet.getFacetKind() == XSSimpleTypeDefinition.FACET_PATTERN)+"]");

				if(facet == multiValuedFacet.getFacetKind()){
					StringList lexicalFacetValues = multiValuedFacet.getLexicalFacetValues();

					/* 
						Isn't this strange?
						This assumes that an enumeration/pattern value is always a string, 
						
						don't we need to try and parse?
					*/

					if(facet == XSSimpleTypeDefinition.FACET_ENUMERATION){
						JsonArrayBuilder enumBuilder = Json.createArrayBuilder();
						for (int x=0; x<lexicalFacetValues.getLength(); x++) {
							lexicalFacetValue = lexicalFacetValues.item(x); 
							enumBuilder.add(lexicalFacetValue);
						}

						builder.add(key, enumBuilder.build());
					}
					else if(facet == XSSimpleTypeDefinition.FACET_PATTERN){
						builder.add(key, lexicalFacetValues.item(0));
					}
				}
			}
		}
	}
	
}
 
Example 10
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);
//		}
	}