Search in sources :

Example 1 with OspModelDescriptionType

use of com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType in project osp-validator by open-simulation-platform.

the class OspModelDescriptionParser method parse.

public OspModelDescriptionType parse(File ospModelDescriptionFile) {
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try (FileInputStream fileInputStream = new FileInputStream(ospModelDescriptionFile)) {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(fileInputStream);
        listener = new LocationListener(reader);
        Schema schema = schemaFactory.newSchema(OspModelDescriptionParser.class.getResource("/OspModelDescription-1.0.0.xsd"));
        JAXBContext context = JAXBContext.newInstance(OspModelDescriptionType.class.getPackage().getName());
        Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setListener(listener);
        return (OspModelDescriptionType) JAXBIntrospector.getValue(unmarshaller.unmarshal(reader));
    } catch (Exception e) {
        String message = "Unable to parse '" + ospModelDescriptionFile.getAbsolutePath() + "'";
        throw new RuntimeException(message, e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) OspModelDescriptionType(com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) FileInputStream(java.io.FileInputStream) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with OspModelDescriptionType

use of com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType in project osp-validator by open-simulation-platform.

the class OspModelDescriptionTypeFactoryTest method canCreate.

@Test
public void canCreate() throws Exception {
    ModelDescriptionFactory factory = new ModelDescriptionFactory();
    ModelDescription modelDescription = factory.create(TestResources.CRANE_CONTROLLER_XML, TestResources.CRANE_CONTROLLER_FMU.toURI());
    OspModelDescriptionTypeFactory typeFactory = new OspModelDescriptionTypeFactory();
    OspModelDescriptionType ospModelDescriptionType = typeFactory.create(modelDescription);
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBContext jc = JAXBContext.newInstance(ospModelDescriptionType.getClass());
    JAXBSource source = new JAXBSource(jc, objectFactory.createOspModelDescription(ospModelDescriptionType));
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(OspModelDescriptionParser.class.getResource("/OspModelDescription-1.0.0.xsd"));
    Validator validator = schema.newValidator();
    validator.setErrorHandler(new ErrorHandler() {

        @Override
        public void warning(SAXParseException exception) {
            fail(exception.toString());
        }

        @Override
        public void error(SAXParseException exception) {
            fail(exception.toString());
        }

        @Override
        public void fatalError(SAXParseException exception) {
            fail(exception.toString());
        }
    });
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) ErrorHandler(org.xml.sax.ErrorHandler) OspModelDescriptionType(com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) OspModelDescriptionParser(com.opensimulationplatform.modeldescription.xml.parser.OspModelDescriptionParser) JAXBSource(javax.xml.bind.util.JAXBSource) ObjectFactory(com.opensimulationplatform.modeldescription.xml.model.ObjectFactory) SAXParseException(org.xml.sax.SAXParseException) ModelDescription(com.opensimulationplatform.core.model.modeldescription.ModelDescription) Validator(javax.xml.validation.Validator) Test(org.junit.Test)

Example 3 with OspModelDescriptionType

use of com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType in project osp-validator by open-simulation-platform.

the class ModelDescriptionFactory method create.

public ModelDescription create(File ospModelDescription, URI fmu) {
    OspModelDescriptionParser parser = new OspModelDescriptionParser();
    OspModelDescriptionType ospModelDescriptionType = parser.parse(ospModelDescription);
    OspModelDescriptionTypeConverter converter = new OspModelDescriptionTypeConverter(fmu);
    return converter.convert(ospModelDescriptionType);
}
Also used : OspModelDescriptionType(com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType) OspModelDescriptionTypeConverter(com.opensimulationplatform.modeldescription.xml.converter.ospmodeldescriptiontype.OspModelDescriptionTypeConverter) OspModelDescriptionParser(com.opensimulationplatform.modeldescription.xml.parser.OspModelDescriptionParser)

Example 4 with OspModelDescriptionType

use of com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType in project osp-validator by open-simulation-platform.

the class ModelDescriptionFactory method create.

public ModelDescription create(URI fmu) {
    OspModelDescriptionType ospModelDescriptionType = new OspModelDescriptionType();
    OspModelDescriptionTypeConverter converter = new OspModelDescriptionTypeConverter(fmu);
    return converter.convert(ospModelDescriptionType);
}
Also used : OspModelDescriptionType(com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType) OspModelDescriptionTypeConverter(com.opensimulationplatform.modeldescription.xml.converter.ospmodeldescriptiontype.OspModelDescriptionTypeConverter)

Aggregations

OspModelDescriptionType (com.opensimulationplatform.modeldescription.xml.model.OspModelDescriptionType)4 OspModelDescriptionTypeConverter (com.opensimulationplatform.modeldescription.xml.converter.ospmodeldescriptiontype.OspModelDescriptionTypeConverter)2 OspModelDescriptionParser (com.opensimulationplatform.modeldescription.xml.parser.OspModelDescriptionParser)2 JAXBContext (javax.xml.bind.JAXBContext)2 Schema (javax.xml.validation.Schema)2 SchemaFactory (javax.xml.validation.SchemaFactory)2 ModelDescription (com.opensimulationplatform.core.model.modeldescription.ModelDescription)1 ObjectFactory (com.opensimulationplatform.modeldescription.xml.model.ObjectFactory)1 FileInputStream (java.io.FileInputStream)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 JAXBSource (javax.xml.bind.util.JAXBSource)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Validator (javax.xml.validation.Validator)1 Test (org.junit.Test)1 ErrorHandler (org.xml.sax.ErrorHandler)1 SAXParseException (org.xml.sax.SAXParseException)1