org.apache.xerces.xs.XSObjectList Java Examples

The following examples show how to use org.apache.xerces.xs.XSObjectList. 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: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void handleComplexTypeDefinitionOfSimpleContentType(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 (!(complexTypeDefinitionName.equals("anyType") && complexTypeDefinition.getNamespace().endsWith(XML_SCHEMA_NS))) {
				if (log.isTraceEnabled()) log.trace("handleComplexTypeDefinitionOfElementContentType creating ref!");
				builder.add("$ref", definitionsPath+complexTypeDefinitionName);
			}
			
			return;
		}
	}
	
	XSObjectList attributeUses = complexTypeDefinition.getAttributeUses();
	
	buildObject(builder, null, attributeUses, mixedContentLabel, complexTypeDefinition.getBaseType());
}
 
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: 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 #4
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 #5
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 #6
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 #7
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getValueDocumentation(String value, ISharedSettingsRequest request) {
	SchemaDocumentationType currStrategy = request.getSharedSettings().getPreferences().getShowSchemaDocumentationType();
	if (this.docStrategy != currStrategy) {
		clearDocumentation();
	}
	String documentation = valuesDocumentation.get(value);
	if (documentation != null) {
		return documentation;
	}
	this.docStrategy = currStrategy;
	// Try get xs:annotation from the element declaration or type
	XSObjectList annotations = getValueAnnotations();
	boolean markdownSupported = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
	documentation = new XSDDocumentation(annotations, value, docStrategy, !markdownSupported)
			.getFormattedDocumentation(markdownSupported);
	valuesDocumentation.put(value, documentation);
	return documentation;
}
 
Example #8
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getDocumentation(ISharedSettingsRequest request) {
	SchemaDocumentationType currStrategy = request.getSharedSettings().getPreferences().getShowSchemaDocumentationType();
	if (this.docStrategy != currStrategy) {
		clearDocumentation();
	} else if (this.attrDocumentation != null) {
		return this.attrDocumentation;
	}
	this.docStrategy = currStrategy;
	// Try get xs:annotation from the element declaration or type
	XSObjectList annotations = getAnnotations();
	boolean markdownSupported = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
	this.attrDocumentation = new XSDDocumentation(annotations, docStrategy, !markdownSupported)
			.getFormattedDocumentation(markdownSupported);
	return this.attrDocumentation;
}
 
Example #9
Source File: XSDAnnotationModel.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns the <code>XSAnnotation</code> instance for the 
 * provided <code>annotation</code>
 * 
 * If <code>value</code> is provided, the <code>XSAnnotation</code> for
 * an attribute value will be searched for
 * 
 * If not provided, the <code>XSAnnotation</code> for
 * an attribute or element will be searched for
 * 
 * @param annotation the annotation object 
 * @param value      the attribute value
 * @return the <code>XSAnnotation</code> instance for the 
 * provided <code>annotation</code>
 */
private static XSAnnotation getXSAnnotation(XSObject annotation, String value) {
	if (annotation instanceof XSMultiValueFacet && value != null) {
		XSMultiValueFacet multiValueFacet = (XSMultiValueFacet) annotation;
			ObjectList enumerationValues = multiValueFacet.getEnumerationValues();
			XSObjectList annotationValues = multiValueFacet.getAnnotations();
			for (int i = 0; i < enumerationValues.getLength(); i++) {
				Object enumValue = enumerationValues.get(i);

				// Assuming always ValidatedInfo
				String enumString = ((ValidatedInfo) enumValue).stringValue();

				if (value.equals(enumString)) {
					return (XSAnnotation) annotationValues.get(i);
				}
			}
	} else if (annotation instanceof XSAnnotation) {
		return  (XSAnnotation) annotation;
	}
	return null;
}
 
Example #10
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
private void handleModelGroup(JsonObjectBuilder builder, XSModelGroup modelGroup, XSObjectList attributeUses, boolean forProperties){
	short compositor = modelGroup.getCompositor();			
	XSObjectList particles = modelGroup.getParticles();
	if (log.isTraceEnabled()) log.trace("modelGroup ["+ToStringBuilder.reflectionToString(modelGroup,ToStringStyle.MULTI_LINE_STYLE)+"]");
	if (log.isTraceEnabled()) log.trace("modelGroup particles ["+ToStringBuilder.reflectionToString(particles,ToStringStyle.MULTI_LINE_STYLE)+"]");
	switch (compositor) {
	case XSModelGroup.COMPOSITOR_SEQUENCE:
	case XSModelGroup.COMPOSITOR_ALL:
		handleCompositorsAllAndSequence(builder, particles, attributeUses);
		return;
	case XSModelGroup.COMPOSITOR_CHOICE:
		handleCompositorChoice(builder, particles, forProperties);	
		return;
	default:
		throw new IllegalStateException("handleModelGroup modelGroup.compositor is not COMPOSITOR_SEQUENCE, COMPOSITOR_ALL or COMPOSITOR_CHOICE, but ["+compositor+"]");
	} 
}
 
Example #11
Source File: XSDAnnotationModel.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns documentation content from the provided collection of annotations
 * 
 * @param annotations the collection of attribute value annotations
 * @param value       the attribute value to find documentation content for
 * @return documentation content from the provided collection of annotations
 */
public static List<String> getDocumentation(XSObjectList annotations, String value) {
	if (annotations == null) {
		return Collections.emptyList();
	}
	List<String> result = new ArrayList<>();
	for (Object object : annotations) {
		XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
		XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
		if (annotationModel != null) {
			List<String> documentation = annotationModel.getDocumentation();
			if (documentation.size() > 0) {
				result.addAll(documentation);
			} else {
				String annotationString = annotation.getAnnotationString();
				if (!isEmpty(annotationString)) {
					String docFromPattern = getDocumentation(annotationString);
					if (!isEmpty(docFromPattern)) {
						result.add(docFromPattern);
					}
				}
			}
		}
	}
	return result;
}
 
Example #12
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 #13
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 #14
Source File: XSDDocumentation.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public XSDDocumentation(XSObjectList annotations, String value, SchemaDocumentationType docStrategy, boolean convertToPlainText) {
	List<String> documentation = Collections.emptyList();
	List<String> appinfo = Collections.emptyList();
	switch(docStrategy) {
		case all: {
			documentation = XSDAnnotationModel.getDocumentation(annotations, value);
			appinfo = XSDAnnotationModel.getAppInfo(annotations, value);
			break;
		}
		case documentation: {
			documentation = XSDAnnotationModel.getDocumentation(annotations, value);
			break;
		}
		case appinfo: {
			appinfo = XSDAnnotationModel.getAppInfo(annotations, value);
			break;
		}
		case none:{
			break;
		}
	}

	if (convertToPlainText) {
		// convert content to plain text
		
		// if the content contains html tags, converting to plaintext
		// will remove them
		convertToPlainText(documentation);
		convertToPlainText(appinfo);
	}

	this.documentation = documentation;
	this.appinfo = appinfo;
	this.strategy = docStrategy;
	this.prefix = XSDAnnotationModel.getPrefix(annotations, value);
}
 
Example #15
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 #16
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
public void handleTerm(JsonObjectBuilder builder, XSTerm term, XSObjectList attributeUses, boolean multiOccurring, boolean forProperties) {
	if (term instanceof XSModelGroup) {
		handleModelGroup(builder, (XSModelGroup)term, attributeUses, forProperties);
		return;
	} 
	if (term instanceof XSElementDeclaration) {
		XSElementDeclaration elementDeclaration = (XSElementDeclaration)term;
		if (elementDeclaration.getScope()==XSConstants.SCOPE_GLOBAL) {
			JsonObject typeDefininition = Json.createObjectBuilder().add("$ref", "#/definitions/"+elementDeclaration.getName()).build();
			if (multiOccurring) {
				JsonObjectBuilder arrayBuilder = Json.createObjectBuilder();
				arrayBuilder.add("type", "array");
				arrayBuilder.add("items", typeDefininition);

				builder.add(elementDeclaration.getName(), arrayBuilder.build());
			} else {
				builder.add(elementDeclaration.getName(), typeDefininition);
			}
		} else {
			handleElementDeclaration(builder, elementDeclaration, multiOccurring, true);
		}
		return;
	}
	if (term instanceof XSWildcard) {
		handleWildcard((XSWildcard)term);
		return;
	} 
	throw new IllegalStateException("handleTerm unknown Term type ["+term.getClass().getName()+"]");
}
 
Example #17
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 #18
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 #19
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 #20
Source File: CMXSDElementDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String getDocumentation(ISharedSettingsRequest request) {
	SchemaDocumentationType currStrategy = request.getSharedSettings().getPreferences().getShowSchemaDocumentationType();
	if (this.documentation != null && this.docStrategy == currStrategy) {
		return this.documentation;
	}

	this.docStrategy = currStrategy;
	XSObjectList annotations = getAnnotations();
	boolean markdownSupported = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
	this.documentation = (new XSDDocumentation(annotations, docStrategy, !markdownSupported))
			.getFormattedDocumentation(markdownSupported);
	return this.documentation;
}
 
Example #21
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 #22
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 #23
Source File: XSDAnnotationModel.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the prefix (ie. xs) from the provided collection of annotations
 * 
 * Prerequisite: <code>value</code> should be provided if <code>annotations</code>
 * is a collection of attribute value annotations
 * 
 * @param annotations the collection of annotations
 * @param value       the attribute value
 * @return the prefix (ie. xs) from the provided collection of annotations
 */
public static String getPrefix(XSObjectList annotations, String value) {
	if (annotations == null) {
		return "";
	}
	for (Object object : annotations) {
		XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
		String content = annotation.getAnnotationString();
		int index = content.indexOf(":");
		if (index != -1) {
			return content.substring(1, index);
		}
	}
	return "";
}
 
Example #24
Source File: XSDAnnotationModel.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns appinfo content from the provided collection of annotations
 * 
 * @param annotations the collection of attribute value annotations
 * @param value       the attribute value to find appinfo content for
 * @return appinfo content from the provided collection of annotations
 */
public static List<String> getAppInfo(XSObjectList annotations, String value) {
	if (annotations == null) {
		return Collections.emptyList();
	}
	List<String> appinfo = new ArrayList<>();
	for (Object object : annotations) {
		XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
		XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
		if (annotationModel != null) {
			appinfo.addAll(annotationModel.getAppInfo());
		}
	}
	return appinfo;
}
 
Example #25
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 #26
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 #27
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 #28
Source File: XSDDocumentation.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public XSDDocumentation(XSObjectList annotations, SchemaDocumentationType docStrategy, boolean convertToPlainText) {
	this(annotations, null, docStrategy, convertToPlainText);
}
 
Example #29
Source File: XmlAligner.java    From iaf with Apache License 2.0 4 votes vote down vote up
public XSObjectList getAttributeUses() {
	return getAttributeUses(typeDefinition);
}
 
Example #30
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
XSObjectList getSubstitutionGroup(XSElementDeclaration elementDeclaration) {
	return model.getSubstitutionGroup(elementDeclaration);
}