org.apache.xerces.xs.XSConstants Java Examples

The following examples show how to use org.apache.xerces.xs.XSConstants. 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: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 6 votes vote down vote up
public JsonObject getDefinitions() {
	JsonObjectBuilder definitionsBuilder = Json.createObjectBuilder();
	for (XSModel model:models) {
		XSNamedMap elements = model.getComponents(XSConstants.ELEMENT_DECLARATION);
		for (int i=0; i<elements.getLength(); i++) {
			XSElementDeclaration elementDecl = (XSElementDeclaration)elements.item(i);
			handleElementDeclaration(definitionsBuilder, elementDecl, false, false);
		}
		XSNamedMap types = model.getComponents(XSConstants.TYPE_DEFINITION);
		for (int i=0; i<types.getLength(); i++) {
			XSTypeDefinition typeDefinition = (XSTypeDefinition)types.item(i);
			String typeNamespace = typeDefinition.getNamespace();
			if (typeNamespace==null || !typeDefinition.getNamespace().equals(XML_SCHEMA_NS)) {
				definitionsBuilder.add(typeDefinition.getName(), getDefinition(typeDefinition, false));
			}
		}
	}
	return definitionsBuilder.build();
}
 
Example #4
Source File: ToXml.java    From iaf with Apache License 2.0 6 votes vote down vote up
public Set<XSElementDeclaration> findElementDeclarationsForName(String namespace, String name) {
	Set<XSElementDeclaration> result=new LinkedHashSet<XSElementDeclaration>();
	if (schemaInformation==null) {
		log.warn("No SchemaInformation specified, cannot find namespaces for ["+namespace+"]:["+name+"]");
		return null;
	}
	for (XSModel model:schemaInformation) {
		XSNamedMap components = model.getComponents(XSConstants.ELEMENT_DECLARATION);
		for (int i=0;i<components.getLength();i++) {
			XSElementDeclaration item=(XSElementDeclaration)components.item(i);
			if ((namespace==null || namespace.equals(item.getNamespace())) && (name==null || name.equals(item.getName()))) {
				if (log.isTraceEnabled()) log.trace("name ["+item.getName()+"] found in namespace ["+item.getNamespace()+"]");
				result.add(item);
			}
		}
	}
	return result;
}
 
Example #5
Source File: CMXSDDocument.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Collection<CMElementDeclaration> getElements() {
	if (elements == null) {
		elements = new ArrayList<>();
		XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);
		for (int j = 0; j < map.getLength(); j++) {
			XSElementDeclaration elementDeclaration = (XSElementDeclaration) map.item(j);
			collectElement(elementDeclaration, elements);
		}
	}
	return elements;
}
 
Example #6
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 #7
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 #8
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 #9
Source File: XmlTypeToJsonSchemaConverter.java    From iaf with Apache License 2.0 5 votes vote down vote up
private String getJsonDataType(short builtInKind){
	String type;
	switch(builtInKind) {
		case XSConstants.BOOLEAN_DT:
			type="boolean";
			break;
		case XSConstants.SHORT_DT:
		case XSConstants.INT_DT:
		case XSConstants.INTEGER_DT:
		case XSConstants.NEGATIVEINTEGER_DT:
		case XSConstants.NONNEGATIVEINTEGER_DT:
		case XSConstants.NONPOSITIVEINTEGER_DT:
		case XSConstants.POSITIVEINTEGER_DT:
		case XSConstants.BYTE_DT:
		case XSConstants.UNSIGNEDBYTE_DT:
		case XSConstants.UNSIGNEDINT_DT:
		case XSConstants.UNSIGNEDSHORT_DT:
			type="integer";
			break;
		case XSConstants.UNSIGNEDLONG_DT:
		case XSConstants.LONG_DT:
		case XSConstants.DECIMAL_DT:
		case XSConstants.FLOAT_DT:
		case XSConstants.DOUBLE_DT:
			type="number";
			break;
		case XSConstants.DATE_DT:
			type="date";
			break;
		case XSConstants.DATETIME_DT:
			type="date-time";
			break;
		default:
			type="string";
			break;
	}
	return type;
}
 
Example #10
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 #11
Source File: SentinelXsdDumpMetadata.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args)
{
   if (!args[0].equals("--dump"))
   {
      // Activates the resolver for Drb
      try
      {
         DrbFactoryResolver.setMetadataResolver (
               new DrbCortexMetadataResolver (
            DrbCortexModel.getDefaultModel ()));
      }
      catch (IOException e)
      {
         System.err.println ("Resolver cannot be handled.");
      }
      process(args[0]);
   }
   else
   {
   System.out.println("declare function local:deduplicate($list) {");
   System.out.println("  if (fn:empty($list)) then ()");
   System.out.println("  else");
   System.out.println("    let $head := $list[1],");
   System.out.println("      $tail := $list[position() > 1]");
   System.out.println("    return");
   System.out
         .println("      if (fn:exists($tail[ . = $head ])) then " +
               "local:deduplicate($tail)");
   System.out.println("      else ($head, local:deduplicate($tail))");
   System.out.println("};");
   System.out.println();
   System.out.println("declare function local:values($list) {");
   System.out
         .println("  fn:string-join(local:deduplicate(" +
               "fn:data($list)), ' ')");
   System.out.println("};");
   System.out.println();

   System.out.println("let $doc := !!!! GIVE PATH HERE !!!!");
   System.out.println("return\n(\n");

   /**
    * Open XML Schema
    */
   final XSLoader loader = new XMLSchemaLoader();

   final XSModel model = loader.loadURI(args[1]);

   XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);

   if (map != null)
   {
      for (int j = 0; j < map.getLength(); j++)
      {
         dumpElement("", (XSElementDeclaration) map.item(j));
      }
   }

   System.out.println("\n) ^--- REMOVE LAST COMMA !!!");
   }
}