Example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

List of usage examples for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Prototype

String W3C_XML_SCHEMA_NS_URI

To view the source code for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Click Source Link

Document

W3C XML Schema Namespace URI.

Usage

From source file:org.activiti.bpmn.converter.BpmnXMLConverter.java

protected Schema createSchema() throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;// ww  w. ja  va2  s .c om
    if (classloader != null) {
        schema = factory.newSchema(classloader.getResource(BPMN_XSD));
    }

    if (schema == null) {
        schema = factory.newSchema(BpmnXMLConverter.class.getClassLoader().getResource(BPMN_XSD));
    }

    if (schema == null) {
        throw new XMLException("BPMN XSD could not be found");
    }
    return schema;
}

From source file:org.activiti.dmn.xml.converter.DmnXMLConverter.java

protected Schema createSchema() throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;//from   w  ww.  ja v a  2  s .  c o m
    if (classloader != null) {
        schema = factory.newSchema(classloader.getResource(DMN_XSD));
    }

    if (schema == null) {
        schema = factory.newSchema(DmnXMLConverter.class.getClassLoader().getResource(DMN_XSD));
    }

    if (schema == null) {
        throw new DmnXMLException("DMN XSD could not be found");
    }
    return schema;
}

From source file:org.apache.bval.jsr.xml.ValidationParser.java

static Schema getSchema(final String xsd) {
    final Schema schema = SCHEMA_CACHE.get(xsd);
    if (schema != null) {
        return schema;
    }//from w  w  w . jav a  2 s .  co m

    final ClassLoader loader = Reflection.getClassLoader(ValidationParser.class);
    final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    final URL schemaUrl = loader.getResource(xsd);
    try {
        Schema s = sf.newSchema(schemaUrl);
        final Schema old = SCHEMA_CACHE.putIfAbsent(xsd, s);
        if (old != null) {
            s = old;
        }
        return s;
    } catch (SAXException e) {
        log.log(Level.WARNING, String.format("Unable to parse schema: %s", xsd), e);
        return null;
    }
}

From source file:org.apache.geode.internal.cache.xmlcache.CacheXmlParser.java

/**
 * Parses XML data and from it creates an instance of <code>CacheXmlParser</code> that can be used
 * to {@link #create}the {@link Cache}, etc.
 *
 * @param is the <code>InputStream</code> of XML to be parsed
 *
 * @return a <code>CacheXmlParser</code>, typically used to create a cache from the parsed XML
 *
 * @throws CacheXmlException Something went wrong while parsing the XML
 *
 * @since GemFire 4.0//from  w  ww  .  java  2 s  . c o m
 *
 */
public static CacheXmlParser parse(InputStream is) {

    /**
     * The API doc http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html for the SAX
     * InputSource says: "... standard processing of both byte and character streams is to close
     * them on as part of end-of-parse cleanup, so applications should not attempt to re-use such
     * streams after they have been handed to a parser."
     *
     * In order to block the parser from closing the stream, we wrap the InputStream in a filter,
     * i.e., UnclosableInputStream, whose close() function does nothing.
     * 
     */
    class UnclosableInputStream extends BufferedInputStream {
        public UnclosableInputStream(InputStream stream) {
            super(stream);
        }

        @Override
        public void close() {
        }
    }

    CacheXmlParser handler = new CacheXmlParser();
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true);
        factory.setValidating(true);
        factory.setNamespaceAware(true);
        UnclosableInputStream bis = new UnclosableInputStream(is);
        try {
            SAXParser parser = factory.newSAXParser();
            // Parser always reads one buffer plus a little extra worth before
            // determining that the DTD is there. Setting mark twice the parser
            // buffer size.
            bis.mark((Integer) parser.getProperty(BUFFER_SIZE) * 2);
            parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI);
            parser.parse(bis, new DefaultHandlerDelegate(handler));
        } catch (CacheXmlException e) {
            if (null != e.getCause() && e.getCause().getMessage().contains(DISALLOW_DOCTYPE_DECL_FEATURE)) {
                // Not schema based document, try dtd.
                bis.reset();
                factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false);
                SAXParser parser = factory.newSAXParser();
                parser.parse(bis, new DefaultHandlerDelegate(handler));
            } else {
                throw e;
            }
        }
        return handler;
    } catch (Exception ex) {
        if (ex instanceof CacheXmlException) {
            while (true /* ex instanceof CacheXmlException */) {
                Throwable cause = ex.getCause();
                if (!(cause instanceof CacheXmlException)) {
                    break;
                } else {
                    ex = (CacheXmlException) cause;
                }
            }
            throw (CacheXmlException) ex;
        } else if (ex instanceof SAXException) {
            // Silly JDK 1.4.2 XML parser wraps RunTime exceptions in a
            // SAXException. Pshaw!
            SAXException sax = (SAXException) ex;
            Exception cause = sax.getException();
            if (cause instanceof CacheXmlException) {
                while (true /* cause instanceof CacheXmlException */) {
                    Throwable cause2 = cause.getCause();
                    if (!(cause2 instanceof CacheXmlException)) {
                        break;
                    } else {
                        cause = (CacheXmlException) cause2;
                    }
                }
                throw (CacheXmlException) cause;
            }
        }
        throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_PARSING_XML.toLocalizedString(), ex);
    }
}

From source file:org.apache.nifi.authorization.AuthorityProviderFactoryBean.java

private AuthorityProviders loadAuthorityProvidersConfiguration() throws Exception {
    final File authorityProvidersConfigurationFile = properties.getAuthorityProviderConfiguraitonFile();

    // load the users from the specified file
    if (authorityProvidersConfigurationFile.exists()) {
        try {//  w w w. j  av  a 2 s  .c  om
            // find the schema
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final Schema schema = schemaFactory
                    .newSchema(AuthorityProviders.class.getResource(AUTHORITY_PROVIDERS_XSD));

            // attempt to unmarshal
            final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            unmarshaller.setSchema(schema);
            final JAXBElement<AuthorityProviders> element = unmarshaller
                    .unmarshal(new StreamSource(authorityProvidersConfigurationFile), AuthorityProviders.class);
            return element.getValue();
        } catch (SAXException | JAXBException e) {
            throw new Exception("Unable to load the authority provider configuration file at: "
                    + authorityProvidersConfigurationFile.getAbsolutePath());
        }
    } else {
        throw new Exception("Unable to find the authority provider configuration file at "
                + authorityProvidersConfigurationFile.getAbsolutePath());
    }
}

From source file:org.apache.nifi.authorization.AuthorizerFactoryBean.java

private Authorizers loadAuthorizersConfiguration() throws Exception {
    final File authorizersConfigurationFile = properties.getAuthorizerConfigurationFile();

    // load the authorizers from the specified file
    if (authorizersConfigurationFile.exists()) {
        try {/*w ww .java 2 s. com*/
            // find the schema
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final Schema schema = schemaFactory.newSchema(Authorizers.class.getResource(AUTHORIZERS_XSD));

            // attempt to unmarshal
            final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            unmarshaller.setSchema(schema);
            final JAXBElement<Authorizers> element = unmarshaller
                    .unmarshal(new StreamSource(authorizersConfigurationFile), Authorizers.class);
            return element.getValue();
        } catch (SAXException | JAXBException e) {
            throw new Exception("Unable to load the authorizer configuration file at: "
                    + authorizersConfigurationFile.getAbsolutePath(), e);
        }
    } else {
        throw new Exception("Unable to find the authorizer configuration file at "
                + authorizersConfigurationFile.getAbsolutePath());
    }
}

From source file:org.apache.nifi.authorization.FileAccessPolicyProvider.java

@Override
public void initialize(AccessPolicyProviderInitializationContext initializationContext)
        throws AuthorizerCreationException {
    userGroupProviderLookup = initializationContext.getUserGroupProviderLookup();

    try {//w  w  w .j a va 2  s  .c om
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        authorizationsSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(AUTHORIZATIONS_XSD));
        usersSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(USERS_XSD));
    } catch (Exception e) {
        throw new AuthorizerCreationException(e);
    }
}

From source file:org.apache.nifi.authorization.FileAuthorizationProvider.java

@Override
public void onConfigured(final AuthorityProviderConfigurationContext configurationContext)
        throws ProviderCreationException {
    try {/*  www .  jav  a2 s. co m*/
        final String usersFilePath = configurationContext.getProperty("Authorized Users File");
        if (usersFilePath == null || usersFilePath.trim().isEmpty()) {
            throw new ProviderCreationException("The authorized users file must be specified.");
        }

        // the users file instance will never be null because a default is used
        usersFile = new File(usersFilePath);
        final File usersFileDirectory = usersFile.getParentFile();

        // the restore directory is optional and may be null
        final File restoreDirectory = properties.getRestoreDirectory();

        if (restoreDirectory != null) {

            // sanity check that restore directory is a directory, creating it if necessary
            FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);

            // check that restore directory is not the same as the primary directory
            if (usersFileDirectory.getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
                throw new ProviderCreationException(
                        String.format("Authorized User's directory '%s' is the same as restore directory '%s' ",
                                usersFileDirectory.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
            }

            // the restore copy will have same file name, but reside in a different directory
            restoreUsersFile = new File(restoreDirectory, usersFile.getName());

            // sync the primary copy with the restore copy
            try {
                FileUtils.syncWithRestore(usersFile, restoreUsersFile, logger);
            } catch (final IOException | IllegalStateException ioe) {
                throw new ProviderCreationException(ioe);
            }

        }

        // load the users from the specified file
        if (usersFile.exists()) {
            // find the schema
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            final Schema schema = schemaFactory
                    .newSchema(FileAuthorizationProvider.class.getResource(USERS_XSD));

            // attempt to unmarshal
            final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
            unmarshaller.setSchema(schema);
            final JAXBElement<Users> element = unmarshaller.unmarshal(new StreamSource(usersFile), Users.class);
            users = element.getValue();
        } else {
            final ObjectFactory objFactory = new ObjectFactory();
            users = objFactory.createUsers();
        }

        // attempt to load a default roles
        final String rawDefaultAuthorities = configurationContext.getProperty("Default User Roles");
        if (StringUtils.isNotBlank(rawDefaultAuthorities)) {
            final Set<String> invalidDefaultAuthorities = new HashSet<>();

            // validate the specified authorities
            final String[] rawDefaultAuthorityList = rawDefaultAuthorities.split(",");
            for (String rawAuthority : rawDefaultAuthorityList) {
                rawAuthority = rawAuthority.trim();
                final Authority authority = Authority.valueOfAuthority(rawAuthority);
                if (authority == null) {
                    invalidDefaultAuthorities.add(rawAuthority);
                } else {
                    defaultAuthorities.add(rawAuthority);
                }
            }

            // report any unrecognized authorities
            if (!invalidDefaultAuthorities.isEmpty()) {
                logger.warn(String.format(
                        "The following default role(s) '%s' were not recognized. Possible values: %s.",
                        StringUtils.join(invalidDefaultAuthorities, ", "),
                        StringUtils.join(Authority.getRawAuthorities(), ", ")));
            }
        }
    } catch (IOException | ProviderCreationException | SAXException | JAXBException e) {
        throw new ProviderCreationException(e);
    }

}

From source file:org.apache.nifi.authorization.FileAuthorizer.java

@Override
public void initialize(final AuthorizerInitializationContext initializationContext)
        throws AuthorizerCreationException {
    try {//  w  w w .j a va  2 s.c  o m
        schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        tenantsSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(TENANTS_XSD));
        authorizationsSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(AUTHORIZATIONS_XSD));
        usersSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(USERS_XSD));
    } catch (Exception e) {
        throw new AuthorizerCreationException(e);
    }
}

From source file:org.apache.nifi.authorization.FileUserGroupProvider.java

@Override
public void initialize(UserGroupProviderInitializationContext initializationContext)
        throws AuthorizerCreationException {
    try {/* www .  j  a  va 2s .  c  o  m*/
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        tenantsSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(TENANTS_XSD));
        usersSchema = schemaFactory.newSchema(FileAuthorizer.class.getResource(USERS_XSD));
    } catch (Exception e) {
        throw new AuthorizerCreationException(e);
    }
}