org.apache.xerces.xs.XSAttributeDeclaration Java Examples

The following examples show how to use org.apache.xerces.xs.XSAttributeDeclaration. 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: 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 #3
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Collection<String> getEnumerationValues() {
	XSAttributeDeclaration attributeDeclaration = getAttrDeclaration();
	if (attributeDeclaration != null) {
		XSSimpleTypeDefinition typeDefinition = attributeDeclaration.getTypeDefinition();
		return CMXSDDocument.getEnumerationValues(typeDefinition);
	}
	return Collections.emptyList();
}
 
Example #4
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public LocationLink findTypeLocation(DOMNode originNode) {
	DOMElement originElement = null;
	DOMAttr originAttribute = null;
	if (originNode.isElement()) {
		originElement = (DOMElement) originNode;
	} else if (originNode.isAttribute()) {
		originAttribute = (DOMAttr) originNode;
		originElement = originAttribute.getOwnerElement();
	}
	if (originElement == null || originElement.getLocalName() == null) {
		return null;
	}
	// Try to retrieve XSD element declaration from the given element.
	CMXSDElementDeclaration elementDeclaration = (CMXSDElementDeclaration) findCMElement(originElement,
			originElement.getNamespaceURI());
	if (elementDeclaration == null) {
		return null;
	}

	// Try to find the Xerces xs:element (which stores the offset) bound with the
	// XSElementDeclaration
	// case when xs:element is declared inside xs:choice, xs:all, xs:sequence, etc
	ElementImpl xercesElement = findLocalMappedXercesElement(elementDeclaration.getElementDeclaration(), xsLoader);
	// case when xs:element is declared as global or inside xs:complexType
	SchemaGrammar schemaGrammar = getOwnerSchemaGrammar(elementDeclaration.getElementDeclaration());
	if (schemaGrammar == null && xercesElement == null) {
		return null;
	}

	String documentURI = xercesElement != null ? xercesElement.getOwnerDocument().getDocumentURI()
			: getSchemaURI(schemaGrammar);
	if (URIUtils.isFileResource(documentURI)) {
		// Only XML Schema file is supported. In the case of XML file is bound with an
		// HTTP url and cache is enable, documentURI is a file uri from the cache
		// folder.

		// Xerces doesn't give the capability to know the location of xs:element,
		// xs:attribute.
		// To retrieve the proper location of xs:element, xs:attribute, we load the XML
		// Schema in the DOM Document which stores location.
		DOMDocument targetSchema = DOMUtils.loadDocument(documentURI,
				originNode.getOwnerDocument().getResolverExtensionManager());
		if (targetSchema == null) {
			return null;
		}
		if (originAttribute != null) {
			// find location of xs:attribute declaration
			String attributeName = originAttribute.getName();
			CMXSDAttributeDeclaration attributeDeclaration = (CMXSDAttributeDeclaration) elementDeclaration
					.findCMAttribute(attributeName);
			if (attributeDeclaration != null) {
				XSAttributeDeclaration attributeDecl = attributeDeclaration.getAttrDeclaration();
				if (attributeDecl.getScope() == XSConstants.SCOPE_LOCAL) {
					return findLocalXSAttribute(originAttribute, targetSchema,
							attributeDecl.getEnclosingCTDefinition(), schemaGrammar);
				}
			}
		} else {
			// find location of xs:element declaration
			boolean globalElement = elementDeclaration.getElementDeclaration()
					.getScope() == XSElementDecl.SCOPE_GLOBAL;
			if (globalElement) {
				// global xs:element
				return findGlobalXSElement(originElement, targetSchema);
			} else {
				// local xs:element
				// 1) use the Xerces xs:element strategy
				if (xercesElement != null) {
					return findLocalXSElement(originElement, targetSchema, xercesElement.getCharacterOffset());
				}
				// 2) use the Xerces xs:complexType strategy
				XSComplexTypeDefinition complexTypeDefinition = elementDeclaration.getElementDeclaration()
						.getEnclosingCTDefinition();
				if (complexTypeDefinition != null) {
					return findLocalXSElement(originElement, targetSchema, complexTypeDefinition, schemaGrammar);
				}
			}
		}
	}
	return null;
}
 
Example #5
Source File: CMXSDAttributeDeclaration.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
XSAttributeDeclaration getAttrDeclaration() {
	return attributeUse.getAttrDeclaration();
}
 
Example #6
Source File: XMLSampleValueGenerator.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public String generateSampleValue(XSAttributeDeclaration attribute, XSSimpleTypeDefinition simpleType, Path path){
    List<String> values = attributeValues.get(attribute);
    return values==null ? null : values.get(RandomUtil.random(0, values.size()-1));
}
 
Example #7
Source File: EmptySampleValueGenerator.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public String generateSampleValue(XSAttributeDeclaration attribute, XSSimpleTypeDefinition simpleType, Path path){
    return "";
}
 
Example #8
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 #9
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 #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);
//		}
	}