Search in sources :

Example 1 with XSNamedMap

use of org.apache.xerces.xs.XSNamedMap in project jena by apache.

the class XSDDatatype method loadUserDefined.

/**
     * Internal implementation of loadUserDefined
     *
     * @param uri the absolute uri of the schema file to be loaded
     * @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
     * @param encoding the encoding of the source file (can be null)
     * @param tm the type mapper into which to load the definitions
     * @return a List of strings giving the uri's of the newly defined datatypes
     * @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
     * in default mode for load which may provide diagnostic output direct to stderr)
     */
private static List<String> loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException {
    XMLGrammarPreparser parser = new XMLGrammarPreparser();
    parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
    try {
        XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source);
        org.apache.xerces.xs.XSModel xsm = xsg.toXSModel();
        XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE);
        int numDefs = map.getLength();
        ArrayList<String> names = new ArrayList<>(numDefs);
        for (int i = 0; i < numDefs; i++) {
            XSSimpleType xstype = (XSSimpleType) map.item(i);
            // Filter built in types - only needed for 2.6.0
            if (!XSD.equals(xstype.getNamespace())) {
                //xstype.derivedFrom()
                XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId());
                tm.registerDatatype(definedType);
                names.add(definedType.getURI());
            }
        }
        return names;
    } catch (Exception e) {
        // Temp
        e.printStackTrace();
        throw new DatatypeFormatException(e.toString());
    }
}
Also used : DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) ArrayList(java.util.ArrayList) DatatypeFormatException(org.apache.jena.datatypes.DatatypeFormatException) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) XSGrammar(org.apache.xerces.xni.grammars.XSGrammar) XSNamedMap(org.apache.xerces.xs.XSNamedMap)

Example 2 with XSNamedMap

use of org.apache.xerces.xs.XSNamedMap in project iaf by ibissource.

the class XmlTypeToJsonSchemaConverter method getDefinitions.

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();
}
Also used : XSTypeDefinition(org.apache.xerces.xs.XSTypeDefinition) XSElementDeclaration(org.apache.xerces.xs.XSElementDeclaration) XSModel(org.apache.xerces.xs.XSModel) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 3 with XSNamedMap

use of org.apache.xerces.xs.XSNamedMap in project webtools.sourceediting by eclipse.

the class AbstractPsychoPathTest method addUserDefinedSimpleTypes.

protected void addUserDefinedSimpleTypes(XSModel schema) {
    XSNamedMap xstypes = schema.getComponents(XSConstants.TYPE_DEFINITION);
    if (xstypes.getLength() == 0) {
        return;
    }
    addNamespace("myType", "http://www.w3.org/XQueryTest/userDefinedTypes");
    UserDefinedCtrLibrary udl = new UserDefinedCtrLibrary("http://www.w3.org/XQueryTest/userDefinedTypes");
    for (int i = 0; i < xstypes.getLength(); i++) {
        XSObject xsobject = xstypes.item(i);
        if ("http://www.w3.org/XQueryTest/userDefinedTypes".equals(xsobject.getNamespace())) {
            if (xsobject instanceof XSSimpleTypeDefinition) {
                XSSimpleTypeDefinition typeDef = (XSSimpleTypeDefinition) xsobject;
                if (typeDef.getNumeric()) {
                    if (xsobject.getName().equals("floatBased") || xsobject.getName().equals("shoesize")) {
                        XercesFloatUserDefined fudt = new XercesFloatUserDefined(xsobject);
                        udl.add_type(fudt);
                    } else {
                        XercesIntegerUserDefined iudt = new XercesIntegerUserDefined(xsobject);
                        udl.add_type(iudt);
                    }
                } else {
                    if (xsobject.getName().equals("QNameBased")) {
                        XercesQNameUserDefined qudt = new XercesQNameUserDefined(xsobject);
                        udl.add_type(qudt);
                    } else {
                        XercesUserDefined udt = new XercesUserDefined(typeDef);
                        udl.add_type(udt);
                    }
                }
            }
        }
    }
    addFunctionLibrary(udl);
}
Also used : XercesIntegerUserDefined(org.eclipse.wst.xml.xpath2.processor.testsuite.userdefined.XercesIntegerUserDefined) XercesFloatUserDefined(org.eclipse.wst.xml.xpath2.processor.testsuite.userdefined.XercesFloatUserDefined) XSObject(org.apache.xerces.xs.XSObject) XSNamedMap(org.apache.xerces.xs.XSNamedMap) UserDefinedCtrLibrary(org.eclipse.wst.xml.xpath2.processor.internal.types.userdefined.UserDefinedCtrLibrary) XercesUserDefined(org.eclipse.wst.xml.xpath2.processor.testsuite.userdefined.XercesUserDefined) XSSimpleTypeDefinition(org.apache.xerces.xs.XSSimpleTypeDefinition) XercesQNameUserDefined(org.eclipse.wst.xml.xpath2.processor.testsuite.userdefined.XercesQNameUserDefined)

Example 4 with XSNamedMap

use of org.apache.xerces.xs.XSNamedMap in project winery by eclipse.

the class RepositoryBasedXsdImportManager method getAllDefinedLocalNames.

// we need "unchecked", because of the parsing of the cache
@SuppressWarnings("unchecked")
private List<String> getAllDefinedLocalNames(final XSDImportId id, final boolean getTypes) {
    Objects.requireNonNull(id);
    Optional<RepositoryFileReference> ref = this.getXsdFileReference(id);
    if (!ref.isPresent()) {
        return Collections.emptyList();
    }
    short type = getTypes ? XSConstants.TYPE_DEFINITION : XSConstants.ELEMENT_DECLARATION;
    Date lastUpdate = owner.getLastUpdate(ref.get());
    @NonNull final String cacheFileName = "definedLocalNames " + Integer.toString(type) + ".cache";
    @NonNull final RepositoryFileReference cacheRef = new RepositoryFileReference(id, cacheFileName);
    boolean cacheNeedsUpdate = true;
    if (owner.exists(cacheRef)) {
        Date lastUpdateCache = owner.getLastUpdate(cacheRef);
        if (lastUpdate.compareTo(lastUpdateCache) <= 0) {
            cacheNeedsUpdate = false;
        }
    }
    List<String> result;
    if (cacheNeedsUpdate) {
        final Optional<XSModel> model = BackendUtils.getXSModel(ref.get(), owner);
        if (!model.isPresent()) {
            return Collections.emptyList();
        }
        XSNamedMap components = model.get().getComponents(type);
        // @SuppressWarnings("unchecked")
        int len = components.getLength();
        result = new ArrayList<>(len);
        for (int i = 0; i < len; i++) {
            XSObject item = components.item(i);
            // We want to return only types defined in the namespace of this resource
            if (id.getNamespace().getDecoded().equals(item.getNamespace())) {
                result.add(item.getName());
            }
        }
        String cacheContent = null;
        try {
            cacheContent = JacksonProvider.mapper.writeValueAsString(result);
        } catch (JsonProcessingException e) {
            LOGGER.error("Could not generate cache content", e);
        }
        try {
            owner.putContentToFile(cacheRef, cacheContent, MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } catch (IOException e) {
            LOGGER.error("Could not update cache", e);
        }
    } else {
        // cache should contain most recent information
        try (InputStream is = owner.newInputStream(cacheRef)) {
            result = JacksonProvider.mapper.readValue(is, java.util.List.class);
        } catch (IOException e) {
            LOGGER.error("Could not read from cache", e);
            result = Collections.emptyList();
        }
    }
    return result;
}
Also used : InputStream(java.io.InputStream) XSObject(org.apache.xerces.xs.XSObject) IOException(java.io.IOException) Date(java.util.Date) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) XSModel(org.apache.xerces.xs.XSModel) ArrayList(java.util.ArrayList) List(java.util.List) XSNamedMap(org.apache.xerces.xs.XSNamedMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

XSNamedMap (org.apache.xerces.xs.XSNamedMap)4 ArrayList (java.util.ArrayList)2 XSModel (org.apache.xerces.xs.XSModel)2 XSObject (org.apache.xerces.xs.XSObject)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 List (java.util.List)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 DatatypeFormatException (org.apache.jena.datatypes.DatatypeFormatException)1 XMLGrammarPreparser (org.apache.xerces.parsers.XMLGrammarPreparser)1 XSGrammar (org.apache.xerces.xni.grammars.XSGrammar)1 XSElementDeclaration (org.apache.xerces.xs.XSElementDeclaration)1 XSSimpleTypeDefinition (org.apache.xerces.xs.XSSimpleTypeDefinition)1 XSTypeDefinition (org.apache.xerces.xs.XSTypeDefinition)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)1 UserDefinedCtrLibrary (org.eclipse.wst.xml.xpath2.processor.internal.types.userdefined.UserDefinedCtrLibrary)1 XercesFloatUserDefined (org.eclipse.wst.xml.xpath2.processor.testsuite.userdefined.XercesFloatUserDefined)1