org.apache.xerces.xs.XSParticle Java Examples

The following examples show how to use org.apache.xerces.xs.XSParticle. 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: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void collectElementsDeclaration(XSTerm term, Collection<CMElementDeclaration> elements) {
	if (term == null) {
		return;
	}
	switch (term.getType()) {
	case XSConstants.WILDCARD:
		// XSWildcard wildcard = (XSWildcard) term;
		// ex : xsd:any
		document.getElements().forEach(e -> {
			if (!elements.contains(e)) {
				elements.add(e);
			}
		});
		break;
	case XSConstants.MODEL_GROUP:
		XSObjectList particles = ((XSModelGroup) term).getParticles();
		particles.forEach(p -> collectElementsDeclaration(((XSParticle) p).getTerm(), elements));
		break;
	case XSConstants.ELEMENT_DECLARATION:
		XSElementDeclaration elementDeclaration = (XSElementDeclaration) term;
		document.collectElement(elementDeclaration, elements);
		break;
	}
}
 
Example #2
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void dumpParticle(String path, XSParticle particle)
{
   XSTerm term = particle.getTerm();

   switch (term.getType())
   {
      case XSConstants.ELEMENT_DECLARATION:
         dumpElement(path, (XSElementDeclaration) term);
         break;
      case XSConstants.MODEL_GROUP:
         XSModelGroup model_group = (XSModelGroup) term;
         final XSObjectList particles = model_group.getParticles();

         for (int ipar = 0; ipar < particles.getLength(); ipar++)
         {
            dumpParticle(path, (XSParticle) particles.item(ipar));
         }
         break;
      default:
         System.err.println(path + " - UNKNOWN");
   }

}
 
Example #3
Source File: XSContentModel.java    From jlibs with Apache License 2.0 6 votes vote down vote up
private void appendCardinality(Path path){
    path = path.getParentPath(XSParticle.class);
    if(path!=null){
        XSParticle particle = (XSParticle)path.getElement();
        if(particle.getMinOccurs()==1 && particle.getMaxOccurs()==1)
            return;
        if(particle.getMinOccurs()==0 && particle.getMaxOccurs()==1)
            buff.append("?");
        else if(particle.getMinOccurs()==0 && particle.getMaxOccursUnbounded())
            buff.append("*");
        else if(particle.getMinOccurs()==1 && particle.getMaxOccursUnbounded())
            buff.append("+");
        else{
            buff.append("[");
            if(particle.getMaxOccursUnbounded())
                buff.append(particle.getMinOccurs()).append("+");
            else if(particle.getMinOccurs()==particle.getMaxOccurs())
                buff.append(particle.getMinOccurs());
            else
                buff.append(particle.getMinOccurs()).append(",").append(particle.getMaxOccurs());
            buff.append("]");
        }
    }
}
 
Example #4
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void handleComplexTypeDefinitionOfElementContentType(XSComplexTypeDefinition complexTypeDefinition, boolean shouldCreateReferences, JsonObjectBuilder builder){
	if(shouldCreateReferences){
		String complexTypeDefinitionName = complexTypeDefinition.getName();

		if(complexTypeDefinitionName == null && complexTypeDefinition.getContext() != null  && complexTypeDefinition.getContext().getNamespaceItem() != null){
			complexTypeDefinitionName = complexTypeDefinition.getContext().getName(); // complex type definition name defaults to name of context
		}

		if(complexTypeDefinitionName != null){
			if (log.isTraceEnabled()) log.trace("handleComplexTypeDefinitionOfElementContentType creating ref!");

			builder.add("$ref", definitionsPath+complexTypeDefinitionName);
			return;
		}
	}
	
	XSObjectList attributeUses = complexTypeDefinition.getAttributeUses();

	XSParticle particle = complexTypeDefinition.getParticle();
	handleParticle(builder, particle, attributeUses, false);
}
 
Example #5
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 #6
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 #7
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 #8
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private void collectElementsDeclaration(XSComplexTypeDefinition typeDefinition,
		Collection<CMElementDeclaration> elements) {
	XSParticle particle = typeDefinition.getParticle();
	if (particle != null) {
		collectElementsDeclaration(particle.getTerm(), elements);
	}
}
 
Example #9
Source File: XMLToJSON.java    From ts-reaktive with MIT License 5 votes vote down vote up
private Map<QName,XSParticle> getValidSubTags(XSElementDeclaration elmt) {
    if (!(elmt.getTypeDefinition() instanceof XSComplexTypeDefinition)) {
        return HashMap.empty();
    }
    
    XSComplexTypeDefinition type = (XSComplexTypeDefinition) elmt.getTypeDefinition();
    if (type.getParticle() == null || !(type.getParticle().getTerm() instanceof XSModelGroup)) {
        return HashMap.empty();
    }
    
    XSModelGroup group = (XSModelGroup) type.getParticle().getTerm();
    if (group.getCompositor() != XSModelGroup.COMPOSITOR_SEQUENCE && group.getCompositor() != XSModelGroup.COMPOSITOR_CHOICE) {
        return HashMap.empty();
    }
    
    // We don't care whether it's SEQUENCE or CHOICE, we only want to know what are the valid sub-elements at this level.
    XSObjectList particles = group.getParticles();
    Map<QName,XSParticle> content = HashMap.empty();
    for (int j = 0; j < particles.getLength(); j++) {
        XSParticle sub = (XSParticle) particles.get(j);
        if (sub.getTerm() instanceof XSElementDeclaration) {
            XSElementDeclaration term = (XSElementDeclaration) sub.getTerm();
            content = content.put(new QName(term.getNamespace(), term.getName()), sub);
        }
    }
    return content;
}
 
Example #10
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
public void handleParticle(JsonObjectBuilder builder, XSParticle particle, XSObjectList attributeUses, boolean forProperties) {
	if (particle==null) {
		throw new NullPointerException("particle is null");
	} 
	XSTerm term = particle.getTerm();
	if (term==null) {
		throw new NullPointerException("particle.term is null");
	}
	handleTerm(builder,term,attributeUses, particle.getMaxOccursUnbounded() || particle.getMaxOccurs()>1, forProperties);
}
 
Example #11
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 #12
Source File: XMLToJSON.java    From ts-reaktive with MIT License 4 votes vote down vote up
private boolean isMultiValued(XSParticle particle) {
    return particle.getMaxOccursUnbounded() || particle.getMaxOccurs() > 1;
}
 
Example #13
Source File: XSDisplayFilter.java    From jlibs with Apache License 2.0 4 votes vote down vote up
protected boolean process(XSParticle particle){
    return !(!particle.getMaxOccursUnbounded() && particle.getMinOccurs() == 1 && particle.getMaxOccurs() == 1);
}
 
Example #14
Source File: XSPathDiplayFilter.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
protected boolean process(XSParticle particle){
    return navigator.children(particle).length()!=1;
}
 
Example #15
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 #16
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()+"]");
		}
	}