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.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Write a {@link RealtorContactDetails} as XML into an {@link OutputStream}.
 * <p>/* ww w.j  a  va  2  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 contact
 * 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(RealtorContactDetails contact, 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.createRealtorContactDetail(contact), output);
}

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

/**
 * Write a {@link RealEstate} as XML into an {@link OutputStream}.
 * <p>//ww w . ja v 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 realEstate
 * 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(RealEstate realEstate, Marshaller marshaller, OutputStream output)
        throws JAXBException {
    final org.openestate.is24.restapi.xml.realestates.ObjectFactory factory = new org.openestate.is24.restapi.xml.realestates.ObjectFactory();

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

    if (realEstate instanceof ApartmentBuy) {
        marshaller.marshal(factory.createApartmentBuy((ApartmentBuy) realEstate), output);
    } else if (realEstate instanceof ApartmentRent) {
        marshaller.marshal(factory.createApartmentRent((ApartmentRent) realEstate), output);
    } else if (realEstate instanceof AssistedLiving) {
        marshaller.marshal(factory.createAssistedLiving((AssistedLiving) realEstate), output);
    } else if (realEstate instanceof CompulsoryAuction) {
        marshaller.marshal(factory.createCompulsoryAuction((CompulsoryAuction) realEstate), output);
    } else if (realEstate instanceof FlatShareRoom) {
        marshaller.marshal(factory.createFlatShareRoom((FlatShareRoom) realEstate), output);
    } else if (realEstate instanceof GarageBuy) {
        marshaller.marshal(factory.createGarageBuy((GarageBuy) realEstate), output);
    } else if (realEstate instanceof GarageRent) {
        marshaller.marshal(factory.createGarageRent((GarageRent) realEstate), output);
    } else if (realEstate instanceof Gastronomy) {
        marshaller.marshal(factory.createGastronomy((Gastronomy) realEstate), output);
    } else if (realEstate instanceof HouseBuy) {
        marshaller.marshal(factory.createHouseBuy((HouseBuy) realEstate), output);
    } else if (realEstate instanceof HouseRent) {
        marshaller.marshal(factory.createHouseRent((HouseRent) realEstate), output);
    } else if (realEstate instanceof HouseType) {
        marshaller.marshal(factory.createHouseType((HouseType) realEstate), output);
    } else if (realEstate instanceof Industry) {
        marshaller.marshal(factory.createIndustry((Industry) realEstate), output);
    } else if (realEstate instanceof Investment) {
        marshaller.marshal(factory.createInvestment((Investment) realEstate), output);
    } else if (realEstate instanceof LivingBuySite) {
        marshaller.marshal(factory.createLivingBuySite((LivingBuySite) realEstate), output);
    } else if (realEstate instanceof LivingRentSite) {
        marshaller.marshal(factory.createLivingRentSite((LivingRentSite) realEstate), output);
    } else if (realEstate instanceof Office) {
        marshaller.marshal(factory.createOffice((Office) realEstate), output);
    } else if (realEstate instanceof SeniorCare) {
        marshaller.marshal(factory.createSeniorCare((SeniorCare) realEstate), output);
    } else if (realEstate instanceof ShortTermAccommodation) {
        marshaller.marshal(factory.createShortTermAccommodation((ShortTermAccommodation) realEstate), output);
    } else if (realEstate instanceof SpecialPurpose) {
        marshaller.marshal(factory.createSpecialPurpose((SpecialPurpose) realEstate), output);
    } else if (realEstate instanceof Store) {
        marshaller.marshal(factory.createStore((Store) realEstate), output);
    } else if (realEstate instanceof TradeSite) {
        marshaller.marshal(factory.createTradeSite((TradeSite) realEstate), output);
    } else {
        marshaller.marshal(factory.createRealEstate(realEstate), output);
    }
}

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

/**
 * Write a {@link Region} as XML into an {@link OutputStream}.
 * <p>/*from  w ww.j  a v  a  2  s.co  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 region
 * 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(Region region, 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.createRegion(region), output);
}

From source file:org.opennms.core.test.xml.XmlTest.java

@Test
public void validateJaxbXmlAgainstSchema() throws Exception {
    final String schemaFile = getSchemaFile();
    if (schemaFile == null) {
        LOG.warn("Skipping validation.");
        return;//from w w w . jav  a 2 s  .  c  o m
    }
    LOG.debug("Validating against XSD: {}", schemaFile);
    javax.xml.bind.Unmarshaller unmarshaller = JaxbUtils.getUnmarshallerFor(getSampleClass(), null, true);
    final SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final Schema schema = factory.newSchema(new StreamSource(schemaFile));
    unmarshaller.setSchema(schema);
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override
        public boolean handleEvent(final ValidationEvent event) {
            LOG.warn("Received validation event: {}", event, event.getLinkedException());
            return false;
        }
    });
    try {
        final InputSource inputSource = new InputSource(getSampleXmlInputStream());
        final XMLFilter filter = JaxbUtils.getXMLFilterForClass(getSampleClass());
        final SAXSource source = new SAXSource(filter, inputSource);
        @SuppressWarnings("unchecked")
        T obj = (T) unmarshaller.unmarshal(source);
        assertNotNull(obj);
    } finally {
        unmarshaller.setSchema(null);
    }
}

From source file:org.squidy.designer.model.ModelViewHandler.java

/**
 * Parses a given input stream (should contain model and diagram code as xml
 * structure) and returns it as an object structure.
 * //from  www. j ava  2  s . c  o  m
 * @param inputStream
 *            The input stream should contain model and diagram structure.
 * @return The parsed XMI document containing model and diagram in object
 *         representation.
 * @throws SquidyException
 */
public Data load(InputStream inputStream) throws SquidyException {
    try {
        Unmarshaller unmarshaller = getContext().createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            /* (non-Javadoc)
             * @see javax.xml.bind.ValidationEventHandler#handleEvent(javax.xml.bind.ValidationEvent)
             */
            public boolean handleEvent(ValidationEvent e) {
                ValidationEventLocator locator = e.getLocator();
                if (LOG.isErrorEnabled()) {
                    LOG.error("Error while reading input stream: \"" + e.getMessage() + "\" [line="
                            + locator.getLineNumber() + ", column=" + locator.getColumnNumber() + "]");
                }
                return true;
            }
        });

        return (Data) unmarshaller.unmarshal(inputStream);
    } catch (JAXBException e) {
        throw new SquidyException("Could not unmarshal input stream.", e);
    }
}

From source file:org.squidy.manager.parser.ModelHandler.java

/**
 * Parses a given input stream (should contain model and diagram code as xml
 * structure) and returns it as an object structure.
 * //from   w w  w  . ja v a 2  s  .c o  m
 * @param inputStream
 *            The input stream should contain model and diagram structure.
 * @return The parsed XMI document containing model and diagram in object
 *         representation.
 */
@SuppressWarnings("restriction")
public Data load(InputStream inputStream) {
    try {
        Unmarshaller unmarshaller = getContext().createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            /*
             * (non-Javadoc)
             * 
             * @see
             * javax.xml.bind.ValidationEventHandler#handleEvent(javax.xml
             * .bind.ValidationEvent)
             */
            public boolean handleEvent(ValidationEvent e) {
                // Ignore this as workspace-shape is located in not
                // referenced squidy-designer module
                if (e.getMessage().startsWith("unexpected element (uri:\"\", local:\"workspace-shape\")")) {
                    return true;
                }

                ValidationEventLocator locator = e.getLocator();
                if (LOG.isErrorEnabled()) {
                    LOG.error("Error while reading input stream: \"" + e.getMessage() + "\" [line="
                            + locator.getLineNumber() + ", column=" + locator.getColumnNumber() + "]"
                            + e.getLinkedException());
                }
                return true;
            }
        });

        return (Data) unmarshaller.unmarshal(inputStream);
    } catch (JAXBException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
        return null;
    }
}

From source file:org.sweble.wikitext.dumpreader.DumpReader.java

private void setSchema(URL schemaUrl) throws Exception {
    SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

    sf.setResourceResolver(new LSResourceResolverImplementation());

    Schema schema = sf.newSchema(schemaUrl);

    unmarshaller.setSchema(schema);//from w  w w . jav a 2  s  .  co m

    unmarshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent ve) {
            try {
                return DumpReader.this.handleEvent(ve, ve.getLocator());
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new WrappedException(e);
            }
        }
    });
}