Example usage for javax.xml.bind ValidationEventHandler ValidationEventHandler

List of usage examples for javax.xml.bind ValidationEventHandler ValidationEventHandler

Introduction

In this page you can find the example usage for javax.xml.bind ValidationEventHandler ValidationEventHandler.

Prototype

ValidationEventHandler

Source Link

Usage

From source file:org.jvnet.hyperjaxb3.samples.pows.common.BindingUtility.java

public static Unmarshaller getUnmarshaller() throws JAXBException {
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    //unmarshaller.setValidating(true);
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            boolean keepOn = false;

            return keepOn;
        }/*from  w  ww.j a v  a 2s .  c om*/
    });

    return unmarshaller;
}

From source file:org.multicore_association.measure.mem.generate.MemCodeGen.java

/**
 * Get the data which need to make CSource from SHIM file.
 * @return Flag for success judgements//from w  ww.  jav a2  s  .co m
 */
private boolean getDataFromShim() {
    boolean ch = true;

    try {
        JAXBContext context = JAXBContext.newInstance(PACKAGENAME);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        /* validation check setup */
        if (!shimSchemaPath.equals("")) {
            SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(shimSchemaPath));
            unmarshaller.setSchema(schema);
            unmarshaller.setEventHandler(new ValidationEventHandler() {
                @Override
                public boolean handleEvent(ValidationEvent event) {
                    if ((event.getSeverity() == ValidationEvent.FATAL_ERROR)
                            || (event.getSeverity() == ValidationEvent.ERROR)) {
                        ValidationEventLocator loc = event.getLocator();
                        parseErrList.add("    Line[" + loc.getLineNumber() + "] : " + event.getMessage());
                    }
                    return true;
                }
            });
        }

        sysConf = unmarshaller.unmarshal(new StreamSource(shimf), SystemConfiguration.class).getValue();

        if (parseErrList.size() > 0) {
            System.err.println("Error: input SHIM file validation error");

            System.err.println("    Validation error location:");
            for (int i = 0; i < parseErrList.size(); i++) {
                System.err.println(parseErrList.get(i));
            }
            return false;
        }

        //         JAXBElement<SystemConfiguration> root = unmarshaller.unmarshal(new StreamSource(shimf), SystemConfiguration.class);
        //         sysConf = root.getValue();
    } catch (Exception e) {
        System.err.println("Error: failed to parse the SHIM file, please check the contents of the file");
        return false;
    }

    ch = makeCPUListFromShim();
    if (!ch) {
        return ch;
    }
    ch = makeSlaveListFromShim();
    if (!ch) {
        return ch;
    }

    ch = makeAddressSpaceListFromShim();
    if (!ch) {
        return ch;
    }

    ch = makeAccessPatternListFromShim();
    if (!ch) {
        return ch;
    }

    return ch;
}

From source file:org.multicore_association.measure.mem.writeback.SetResultToShim.java

/**
 * Set the value to read the existing SHIM file.
 * @return Flag for success judgements//from w w  w.  j a v a  2  s.  co  m
 * @throws ShimFileFormatException
 * @throws ShimFileGenerateException
 */
private static boolean appendToExistingShim() {
    /*
     * append to existing llvm-shim
     */
    SystemConfiguration sysConf = null;

    try {
        JAXBContext context = JAXBContext.newInstance(SystemConfiguration.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        /* validation check setup */
        if (!shimSchemaPath.equals("")) {
            SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(shimSchemaPath));
            unmarshaller.setSchema(schema);
            unmarshaller.setEventHandler(new ValidationEventHandler() {
                @Override
                public boolean handleEvent(ValidationEvent event) {
                    if ((event.getSeverity() == ValidationEvent.FATAL_ERROR)
                            || (event.getSeverity() == ValidationEvent.ERROR)) {
                        ValidationEventLocator loc = event.getLocator();
                        parseErrList.add("    Line[" + loc.getLineNumber() + "] : " + event.getMessage());
                    }
                    return true;
                }
            });
        }

        sysConf = unmarshaller.unmarshal(new StreamSource(shimf), SystemConfiguration.class).getValue();

        if (parseErrList.size() > 0) {
            System.err.println("Error: input SHIM file validation error");

            System.err.println("    Validation error location:");
            for (int i = 0; i < parseErrList.size(); i++) {
                System.err.println(parseErrList.get(i));
            }
            return false;
        }
    } catch (Exception e) {
        System.err.println("Error: failed to parse the SHIM file, please check the contents of the file");
        e.printStackTrace();
        return false;
    }

    AddressSpaceSet addrSpaceSet = sysConf.getAddressSpaceSet();
    if (addrSpaceSet == null) {
        System.err.println("Error: failed to parse the SHIM file, please check the contents of the file");
        return false;
    }

    List<AddressSpace> addrSpaceList = addrSpaceSet.getAddressSpace();
    if (addrSpaceList == null) {
        System.err.println("Error: failed to parse the SHIM file, please check the contents of the file");
        return false;
    }

    masterComponentMap = new HashMap<String, MasterComponent>();
    slaveComponentMap = new HashMap<String, SlaveComponent>();
    accessTypeMap = new HashMap<String, AccessType>();

    List<String> route = new ArrayList<String>();
    ComponentSet cs = sysConf.getComponentSet();
    route.add(cs.getName());

    makeRefMapFromComponentSet(cs, route);

    for (int i = 0; i < measureList.size(); i++) {
        MeasurementsCSVData data = measureList.get(i);

        route.clear();

        /*
         * search AddressSpaceName
         */
        AddressSpace addrSp = null;
        for (Iterator<AddressSpace> j = addrSpaceList.iterator(); j.hasNext();) {
            AddressSpace as = j.next();

            if (as.getName() != null && as.getName().equals(data.getAddressSpaceName())) {
                addrSp = as;
                break;
            }
        }
        if (addrSp == null) {
            System.err.println("Error: Unknown 'Address Space' name (\"" + data.getAddressSpaceName() + "\").");
            return false;
        }

        route.add(addrSp.getName());

        /*
         * search SubSpaceName
         */
        SubSpace subSp = null;
        List<SubSpace> ssList = addrSp.getSubSpace();
        for (Iterator<SubSpace> j = ssList.iterator(); j.hasNext();) {
            SubSpace ss = j.next();

            route.add(ss.getName());
            String path = createPathName(route);
            route.remove(ss.getName());
            if (path != null && path.equals(data.getSubSpaceName())) {
                subSp = ss;
                break;
            }

        }
        if (subSp == null) {
            System.err.println("Error: Unknown 'Sub Space' name (\"" + data.getSubSpaceName() + "\").");
            return false;
        }

        /*
         * search SlaveComponentRef in MasterSlaveBinding
         */
        MasterSlaveBindingSet msBindSet = null;
        msBindSet = subSp.getMasterSlaveBindingSet();
        if (msBindSet == null) {
            continue;
        }

        MasterSlaveBinding msBind = null;
        List<MasterSlaveBinding> msBindList = msBindSet.getMasterSlaveBinding();
        SlaveComponent scComp = slaveComponentMap.get(data.getSlaveComponentName());
        if (scComp == null) {
            System.err.println(
                    "Error: Unknown 'Slave Comonent' name (\"" + data.getSlaveComponentName() + "\").");
            return false;
        }
        for (Iterator<MasterSlaveBinding> j = msBindList.iterator(); j.hasNext();) {
            MasterSlaveBinding msb = j.next();
            SlaveComponent sca = (SlaveComponent) msb.getSlaveComponentRef();

            if (sca != null && sca.getId().equals(scComp.getId())) {
                msBind = msb;
                break;
            }
        }
        if (msBind == null) {
            continue;
        }

        /*
         * search MasterComponentRef in Accessor
         */
        Accessor accessor = null;
        List<Accessor> acList = msBind.getAccessor();
        MasterComponent mcComp = masterComponentMap.get(data.getMasterComponentName());
        if (mcComp == null) {
            System.err.println(
                    "Error: Unknown 'Master Comonent' name (\"" + data.getMasterComponentName() + "\").");
            return false;
        }
        for (Iterator<Accessor> j = acList.iterator(); j.hasNext();) {
            Accessor ac = j.next();
            MasterComponent mc = (MasterComponent) ac.getMasterComponentRef();

            if (mc != null && mc.getId().equals(mcComp.getId())) {
                accessor = ac;
                break;
            }
        }
        if (accessor == null) {
            continue;
        }

        /*
         * search PerformanceSet
         */
        PerformanceSet perfrmSet = null;

        if (accessor.getPerformanceSet().size() != 0) {
            perfrmSet = accessor.getPerformanceSet().get(0);
        }
        if (perfrmSet == null) {
            continue;
        }

        /*
         * search Performance
         */
        List<Performance> pfrmList = perfrmSet.getPerformance();
        AccessType atComp = accessTypeMap.get(data.getAccessTypeName());
        if (atComp == null) {
            System.err.println("Error: Unknown 'Access Type' name (\"" + data.getAccessTypeName() + "\").");
            return false;
        }
        for (Iterator<Performance> j = pfrmList.iterator(); j.hasNext();) {
            Performance pfm = j.next();
            AccessType at = (AccessType) pfm.getAccessTypeRef();

            if (at != null && at.getId().equals(atComp.getId())) {
                Latency latency = new Latency();
                Pitch pitch = new Pitch();

                latency.setBest(data.getBestLatency());
                latency.setWorst(data.getWorstLatency());
                latency.setTypical(data.getTypicalLatency());
                pitch.setBest(data.getBestPitch());
                pitch.setWorst(data.getWorstPitch());
                pitch.setTypical(data.getTypicalPitch());

                pfm.setLatency(latency);
                pfm.setPitch(pitch);
                break;
            }
        }
    }

    try {
        JAXBContext context = JAXBContext.newInstance(SystemConfiguration.class.getPackage().getName());

        Marshaller marshaller = context.createMarshaller();
        /* validation check setup */
        if (!shimSchemaPath.equals("")) {
            SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File(shimSchemaPath));
            marshaller.setSchema(schema);
        }
        QName qname = new QName("", "SystemConfiguration");

        JAXBElement<SystemConfiguration> elem = new JAXBElement<SystemConfiguration>(qname,
                SystemConfiguration.class, sysConf);

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //         marshaller.marshal(elem, new PrintStream(shimf));
        marshaller.marshal(elem, System.out);
    } catch (JAXBException e) {
        e.printStackTrace();
        System.err.println("Error: exception occurs in SHIM file generation"); //$NON-NLS-1$
        return false;
    } catch (SAXException e) {
        e.printStackTrace();
        System.err.println("Error: output SHIM file validation error"); //$NON-NLS-1$
        return false;
    }

    return true;
}

From source file:org.onebusaway.transit_data_federation.siri.SiriXmlSerializerV2.java

public String getXml(Siri siri) throws Exception {
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    marshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            _log.error(event.getMessage(), event.getLinkedException());
            throw new RuntimeException(event.getMessage(), event.getLinkedException());
        }//from   www.  ja  va 2 s  .co  m
    });

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);

    Writer output = new StringWriter();
    marshaller.marshal(siri, output);

    // FIXME: strip off ns6 namespaces on siri root namespace. super hack, please fix me!
    String outputAsString = output.toString();

    /*outputAsString = outputAsString.replaceAll("<ns6:", "<");
    outputAsString = outputAsString.replaceAll("</ns6:", "</");
    outputAsString = outputAsString.replaceAll("xmlns:ns6", "xmlns");
    */

    String[] searchList = { "<siriExtensionWrapper>", "</siriExtensionWrapper>",
            "<siriUpcomingServiceExtension>", "</siriUpcomingServiceExtension>", "<siriPolyLinesExtension>",
            "</siriPolyLinesExtension>" };

    String[] replacementList = { "", "", "", "", "", "" };

    outputAsString = StringUtils.replaceEach(outputAsString, searchList, replacementList);

    return outputAsString;
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write an {@link Attachment} as XML into an {@link OutputStream}.
 * <p>/*from w  ww  .  java2 s  .c om*/
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param attachment
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(Attachment attachment, Marshaller marshaller, OutputStream output)
        throws JAXBException {
    final org.openestate.is24.restapi.xml.common.ObjectFactory factory = new org.openestate.is24.restapi.xml.common.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createAttachment(attachment), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link City} as XML into an {@link OutputStream}.
 * <p>/*ww  w . j  ava  2  s. c o  m*/
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param city
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(City city, Marshaller marshaller, OutputStream output) throws JAXBException {
    final org.openestate.is24.restapi.xml.gis.ObjectFactory factory = new org.openestate.is24.restapi.xml.gis.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createCity(city), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link Continent} as XML into an {@link OutputStream}.
 * <p>//  www.  j a  va2s .  c o  m
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param continent
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(Continent continent, Marshaller marshaller, OutputStream output)
        throws JAXBException {
    final org.openestate.is24.restapi.xml.gis.ObjectFactory factory = new org.openestate.is24.restapi.xml.gis.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createContinent(continent), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link Country} as XML into an {@link OutputStream}.
 * <p>//from w  ww  .  j  a v  a 2  s.com
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param country
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(Country country, Marshaller marshaller, OutputStream output) throws JAXBException {
    final org.openestate.is24.restapi.xml.gis.ObjectFactory factory = new org.openestate.is24.restapi.xml.gis.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createCountry(country), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link PublishObject} as XML into an {@link OutputStream}.
 * <p>/*  www.jav  a2  s.c  o m*/
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param publishing
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(PublishObject publishing, Marshaller marshaller, OutputStream output)
        throws JAXBException {
    final org.openestate.is24.restapi.xml.common.ObjectFactory factory = new org.openestate.is24.restapi.xml.common.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createPublishObject(publishing), output);
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link Quarter} as XML into an {@link OutputStream}.
 * <p>//from www .  j  a v a2s.c  o  m
 * The provided object is wrapped into a {@link JAXBElement} before XML
 * creation in order to match the requirements of the schema.
 * <p>
 * This method makes sure, that validation errors do not break XML creation.
 * Invalid entries are not written into XML.
 *
 * @param quarter
 * object to write
 *
 * @param marshaller
 * marshaller, that is used for XML generation
 *
 * @param output
 * stream, where the XML is written to
 *
 * @throws JAXBException
 * if an error occured during XML creation
 */
public static void writeXml(Quarter quarter, Marshaller marshaller, OutputStream output) throws JAXBException {
    final org.openestate.is24.restapi.xml.gis.ObjectFactory factory = new org.openestate.is24.restapi.xml.gis.ObjectFactory();

    marshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(ValidationEvent ve) {
            return true;
        }
    });

    marshaller.marshal(factory.createQuarter(quarter), output);
}