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:Main.java

public static Schema getSchema(String schemaString) {
    Schema schema = null;/*from  w ww  . j  ava  2  s.co m*/
    try {
        if (schemaString != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            StreamSource ss = new StreamSource();
            ss.setReader(new StringReader(schemaString));
            schema = sf.newSchema(ss);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to create schemma from string: " + schemaString, e);
    }
    return schema;
}

From source file:Main.java

/**
 * This method validate XML by XML File and XSD File.
 *
 * @param xml input XML File/* w w w  .  j a v  a 2  s. c o  m*/
 * @param xsd input XSD File
 *
 * @return true or false, valid or not
 */
public static boolean validateXMLByXSD(File xml, File xsd) {
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(xsd).newValidator()
                .validate(new StreamSource(xml));
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:org.vincibean.salestaxes.jaxb.JaxbFactory.java

/**
 * Factory method, creates a {@link Marshaller} from the context given in the constructor; moreover, ensure that
 * the marshalled XML data is formatted with linefeeds and indentation.  
 * @return an {@link Optional} object which may or may not contain a {@link Marshaller}
 *///  www .j  ava2s.  co m
public static Optional<Marshaller> createMarshaller(final Class<?> context) {
    try {
        Marshaller marshaller = JAXBContext.newInstance(context).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new ClassPathResource("receipt/Poiuyt.xsd").getFile()));
        marshaller.setEventHandler(new FoobarValidationEventHandler());
        return Optional.fromNullable(marshaller);
    } catch (JAXBException | SAXException | IOException e) {
        logger.warn("Exception on jaxb factory creation: ", e);
        return Optional.absent();
    }
}

From source file:Main.java

public static Schema getSchema(URL schemaURL) {
    Schema schema = null;/*from   ww  w . j  a v a2  s.  c  om*/
    try {
        if (schemaURL != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schema = sf.newSchema(schemaURL);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to create shcemma from URL " + schemaURL, e);
    }
    return schema;
}

From source file:com.amalto.core.storage.hibernate.UserTypeMappingRepository.java

public MetadataRepository visit(ComplexTypeMetadata complexType) {
    TypeMapping typeMapping = complexType.accept(getTypeMappingCreator(complexType, strategy));
    // Add MDM specific record specific metadata
    ComplexTypeMetadata database = typeMapping.getDatabase();
    if (database.isInstantiable() && !database.isFrozen()) {
        TypeMetadata longType = new SoftTypeRef(internalRepository, XMLConstants.W3C_XML_SCHEMA_NS_URI,
                Types.LONG, false);
        TypeMetadata stringType = new SoftTypeRef(internalRepository, XMLConstants.W3C_XML_SCHEMA_NS_URI,
                Types.STRING, false);
        database.addField(new SimpleTypeFieldMetadata(database, false, false, true, Storage.METADATA_TIMESTAMP,
                longType, Collections.<String>emptyList(), Collections.<String>emptyList(),
                Collections.<String>emptyList(), StringUtils.EMPTY));
        database.addField(new SimpleTypeFieldMetadata(database, false, false, false, Storage.METADATA_TASK_ID,
                stringType, Collections.<String>emptyList(), Collections.<String>emptyList(),
                Collections.<String>emptyList(), StringUtils.EMPTY));
    }// w ww  .  j  a v  a 2  s.  c  om
    // Register mapping
    internalRepository.addTypeMetadata(typeMapping.getDatabase());
    mappings.addMapping(typeMapping);
    return internalRepository;
}

From source file:com.aol.advertising.qiao.util.XmlConfigUtil.java

/**
 * Validate an XML document against the given schema.
 *
 * @param xmlFileLocationUri//from  w ww. ja  va 2 s  .  c o  m
 *            Location URI of the document to be validated.
 * @param schemaLocationUri
 *            Location URI of the XML schema in W3C XML Schema Language.
 * @return true if valid, false otherwise.
 */
public static boolean validateXml(String xmlFileLocationUri, String schemaLocationUri) {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    InputStream is = null;
    try {
        Schema schema = factory.newSchema(CommonUtils.uriToURL(schemaLocationUri));
        Validator validator = schema.newValidator();
        URL url = CommonUtils.uriToURL(xmlFileLocationUri);
        is = url.openStream();
        Source source = new StreamSource(is);

        validator.validate(source);
        logger.info(">> successfully validated configuration file: " + xmlFileLocationUri);

        return true;
    } catch (SAXParseException e) {
        logger.error(e.getMessage() + " (line:" + e.getLineNumber() + ", col:" + e.getColumnNumber() + ")");

    } catch (Exception e) {
        if (e instanceof SAXParseException) {
            SAXParseException e2 = (SAXParseException) e;
            logger.error(e2.getMessage() + "at line:" + e2.getLineNumber() + ", col:" + e2.getColumnNumber());
        } else {
            logger.error(e.getMessage());
        }
    } finally {
        IOUtils.closeQuietly(is);
    }

    return false;
}

From source file:com.amalto.core.storage.hibernate.StagingTypeMappingRepository.java

public MetadataRepository visit(ComplexTypeMetadata complexType) {
    TypeMapping typeMapping = complexType.accept(getTypeMappingCreator(complexType, strategy));

    // Add MDM specific record specific metadata
    ComplexTypeMetadata database = typeMapping.getDatabase();
    if (database.isInstantiable() && !database.isFrozen()) {
        TypeMetadata intType = new SoftTypeRef(internalRepository, XMLConstants.W3C_XML_SCHEMA_NS_URI,
                Types.INT, false);
        TypeMetadata longType = new SoftTypeRef(internalRepository, XMLConstants.W3C_XML_SCHEMA_NS_URI,
                Types.LONG, false);
        TypeMetadata stringType = new SimpleTypeMetadata(XMLConstants.W3C_XML_SCHEMA_NS_URI, Types.STRING);
        TypeMetadata limitedStringType = new SimpleTypeMetadata(XMLConstants.W3C_XML_SCHEMA_NS_URI,
                Types.STRING);/*from w  w w  .  j a v  a2 s  . co m*/
        limitedStringType.setData(MetadataRepository.DATA_MAX_LENGTH, UUID.randomUUID().toString().length());
        // Time stamp
        database.addField(new SimpleTypeFieldMetadata(database, false, false, true, Storage.METADATA_TIMESTAMP,
                longType, Collections.<String>emptyList(), Collections.<String>emptyList(),
                Collections.<String>emptyList(), StringUtils.EMPTY));
        // Task id
        database.addField(new SimpleTypeFieldMetadata(database, false, false, false, Storage.METADATA_TASK_ID,
                limitedStringType, Collections.<String>emptyList(), Collections.<String>emptyList(),
                Collections.<String>emptyList(), StringUtils.EMPTY));
        // Staging status
        database.addField(new SimpleTypeFieldMetadata(database, false, false, false,
                Storage.METADATA_STAGING_STATUS, intType, Collections.<String>emptyList(),
                Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY));
        // Staging source
        database.addField(new SimpleTypeFieldMetadata(database, false, false, false,
                Storage.METADATA_STAGING_SOURCE, limitedStringType, Collections.<String>emptyList(),
                Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY));
        // Staging block key
        database.addField(new SimpleTypeFieldMetadata(database, false, false, false,
                Storage.METADATA_STAGING_BLOCK_KEY, limitedStringType, Collections.<String>emptyList(),
                Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY));
        // Staging error field
        SimpleTypeFieldMetadata errorField = new SimpleTypeFieldMetadata(database, false, false, false,
                Storage.METADATA_STAGING_ERROR, stringType, Collections.<String>emptyList(),
                Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY);
        errorField.getType().setData(TypeMapping.SQL_TYPE, TypeMapping.SQL_TYPE_TEXT);
        errorField.getType().setData(LongString.PREFER_LONGVARCHAR, true); // ORACLE will use VARCHAR2(4000 CHAR)
        database.addField(errorField);
        // Staging previous values field (useful for rematching)
        SimpleTypeFieldMetadata previousValuesField = new SimpleTypeFieldMetadata(database, false, false, false,
                Storage.METADATA_STAGING_VALUES, stringType, Collections.<String>emptyList(),
                Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY);
        previousValuesField.getType().setData(TypeMapping.SQL_TYPE, TypeMapping.SQL_TYPE_TEXT);
        previousValuesField.getType().setData(LongString.PREFER_LONGVARCHAR, true); // ORACLE will use VARCHAR2(4000 CHAR)
        database.addField(previousValuesField);
    }

    // Register mapping
    internalRepository.addTypeMetadata(typeMapping.getDatabase());
    mappings.addMapping(typeMapping);
    return internalRepository;
}

From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java

public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) {
    boolean validXml = false;
    try {/*from ww  w.  j a  va 2 s.com*/
        // parse an XML document into a DOM tree
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(false);
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xmlContent)));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile());

        Schema schema = schemaFactory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
        validXml = true;
    } catch (FileNotFoundException ex) {
        throw new CaaersSystemException("File Not found Exception", ex);
    } catch (IOException ioe) {
        validationResult.append(ioe.getMessage());
        logger.error(ioe.getMessage());
    } catch (SAXParseException spe) {
        validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
        logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
    } catch (SAXException e) {
        validationResult.append(e.toString());
        logger.error(e.toString());
    } catch (ParserConfigurationException pce) {
        validationResult.append(pce.getMessage());
    }
    return validXml;
}

From source file:com.amalto.core.storage.hibernate.UpdateReportMappingCreator.java

public UpdateReportMappingCreator(TypeMetadata updateReportType, MetadataRepository repository,
        MappingRepository mappings, boolean preferClobUse) {
    this.repository = repository;
    if (updateReportType == null) {
        throw new IllegalStateException("Update report type cannot be null."); //$NON-NLS-1$
    }/* w w w .  jav a  2  s  . com*/
    USER_UPDATE_REPORT_TYPE = (ComplexTypeMetadata) updateReportType;
    this.mappings = mappings;
    ComplexTypeMetadata databaseUpdateReportType = new ComplexTypeMetadataImpl(StringUtils.EMPTY,
            "X_UPDATE_REPORT", true); //$NON-NLS-1$
    TypeMetadata stringType = new SimpleTypeMetadata(XMLConstants.W3C_XML_SCHEMA_NS_URI, Types.STRING);
    TypeMetadata longStringType = new SimpleTypeMetadata(XMLConstants.W3C_XML_SCHEMA_NS_URI, Types.STRING);
    TypeMetadata longType = new SimpleTypeMetadata(XMLConstants.W3C_XML_SCHEMA_NS_URI, Types.LONG);
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_user_name", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, true, false, true,
            "x_source", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, true, false, true,
            "x_time_in_millis", longType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_operation_type", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_revision_id", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_data_cluster", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_data_model", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_concept", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    databaseUpdateReportType.addField(new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false, false,
            "x_key", stringType, Collections.<String>emptyList(), Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), StringUtils.EMPTY));
    SimpleTypeFieldMetadata items_xml = new SimpleTypeFieldMetadata(databaseUpdateReportType, false, false,
            false, "x_items_xml", longStringType, Collections.<String>emptyList(), //$NON-NLS-1$
            Collections.<String>emptyList(), Collections.<String>emptyList(), StringUtils.EMPTY);
    items_xml.getType().setData(TypeMapping.SQL_TYPE,
            preferClobUse ? TypeMapping.SQL_TYPE_CLOB : TypeMapping.SQL_TYPE_TEXT);
    databaseUpdateReportType.addField(items_xml);
    DATABASE_UPDATE_REPORT_TYPE = (ComplexTypeMetadata) databaseUpdateReportType.freeze();
}

From source file:com.spoledge.util.xml.XMLValidator.java

public XMLValidator(File schema) throws SAXException {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    init(sf.newSchema(schema));//from w  w  w.ja v a2  s.  c  om
}