org.apache.xerces.xs.XSSimpleTypeDefinition Java Examples

The following examples show how to use org.apache.xerces.xs.XSSimpleTypeDefinition. 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: 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 #2
Source File: ToXml.java    From iaf with Apache License 2.0 5 votes vote down vote up
protected void handleSimpleTypedElement(XSElementDeclaration elementDeclaration, @SuppressWarnings("unused") XSSimpleTypeDefinition simpleTypeDefinition, N node) throws SAXException {
	String text = getText(elementDeclaration, node);
	if (log.isTraceEnabled()) log.trace("textnode name ["+elementDeclaration.getName()+"] text ["+text+"]");
	if (StringUtils.isNotEmpty(text)) {
		sendString(text);
	}
}
 
Example #3
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 #4
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 #5
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 #6
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
static Collection<String> getEnumerationValues(XSSimpleTypeDefinition typeDefinition) {
	if (typeDefinition != null) {
		if (isBooleanType(typeDefinition)) {
			return StringUtils.TRUE_FALSE_ARRAY;
		}
		StringList enumerations = typeDefinition.getLexicalEnumeration();
		if (enumerations != null) {
			return enumerations;
		}
	}
	return Collections.emptyList();
}
 
Example #7
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 #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: MapContentContainer.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public void setAttribute(String name, String value, XSSimpleTypeDefinition attTypeDefinition) {
	setValue(currentName+attributeSeparator+name,stringToValue(value),false);
}
 
Example #10
Source File: TreeContentContainer.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public void setAttribute(String name, String value, XSSimpleTypeDefinition attTypeDefinition) {
	elementContainer.setAttribute(name, value, attTypeDefinition);
}
 
Example #11
Source File: JsonElementContainer.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public void setAttribute(String name, String value, XSSimpleTypeDefinition attTypeDefinition) {
	JsonElementContainer attributeContainer = new JsonElementContainer(attributePrefix+name, false, false, false, attributePrefix, mixedContentLabel, attTypeDefinition);
	attributeContainer.setContent(value);
	addContent(attributeContainer);
}
 
Example #12
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 #13
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 #14
Source File: EmptySampleValueGenerator.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public String generateSampleValue(XSElementDeclaration element, XSSimpleTypeDefinition simpleType, Path path){
    return "";
}
 
Example #15
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 #16
Source File: XMLSampleValueGenerator.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public String generateSampleValue(XSElementDeclaration element, XSSimpleTypeDefinition simpleType, Path path){
    List<String> values = elementValues.get(element);
    return values==null ? null : values.get(RandomUtil.random(0, values.size()-1));
}
 
Example #17
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
static boolean isBooleanType(XSSimpleTypeDefinition typeDefinition) {
	if (typeDefinition instanceof XSSimpleType) {
		return ((XSSimpleType) typeDefinition).getPrimitiveKind() == XSSimpleType.PRIMITIVE_BOOLEAN;
	}
	return false;
}
 
Example #18
Source File: ElementContainer.java    From iaf with Apache License 2.0 votes vote down vote up
public void setAttribute(String Name, String value, XSSimpleTypeDefinition attTypeDefinition);