|
| 1 | +package ru.javaops.masterjava.xml.util; |
| 2 | + |
| 3 | +import org.xml.sax.SAXException; |
| 4 | + |
| 5 | +import javax.xml.bind.JAXBContext; |
| 6 | +import javax.xml.bind.JAXBException; |
| 7 | +import javax.xml.bind.PropertyException; |
| 8 | +import javax.xml.transform.stream.StreamSource; |
| 9 | +import javax.xml.validation.Schema; |
| 10 | +import java.io.*; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Marshalling/Unmarshalling JAXB helper |
| 15 | + * XML Facade |
| 16 | + * @author Grigory Kislin |
| 17 | + */ |
| 18 | +public class JaxbParser { |
| 19 | + |
| 20 | + protected JaxbMarshaller jaxbMarshaller; |
| 21 | + protected JaxbUnmarshaller jaxbUnmarshaller; |
| 22 | + protected Schema schema; |
| 23 | + |
| 24 | + public JaxbParser(Class... classesToBeBound) { |
| 25 | + try { |
| 26 | + init(JAXBContext.newInstance(classesToBeBound)); |
| 27 | + } catch (JAXBException e) { |
| 28 | + throw new IllegalArgumentException(e); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath |
| 33 | + public JaxbParser(String context) { |
| 34 | + try { |
| 35 | + init(JAXBContext.newInstance(context)); |
| 36 | + } catch (JAXBException e) { |
| 37 | + throw new IllegalArgumentException(e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void init(JAXBContext ctx) throws JAXBException { |
| 42 | + jaxbMarshaller = new JaxbMarshaller(ctx); |
| 43 | + jaxbUnmarshaller = new JaxbUnmarshaller(ctx); |
| 44 | + } |
| 45 | + |
| 46 | + // Unmarshaller |
| 47 | + public <T> T unmarshal(InputStream is) throws JAXBException { |
| 48 | + return (T) jaxbUnmarshaller.unmarshal(is); |
| 49 | + } |
| 50 | + |
| 51 | + public <T> T unmarshal(Reader reader) throws JAXBException { |
| 52 | + return (T) jaxbUnmarshaller.unmarshal(reader); |
| 53 | + } |
| 54 | + |
| 55 | + public <T> T unmarshal(String str) throws JAXBException { |
| 56 | + return (T) jaxbUnmarshaller.unmarshal(str); |
| 57 | + } |
| 58 | + |
| 59 | + // Marshaller |
| 60 | + public void setMarshallerProperty(String prop, Object value) { |
| 61 | + try { |
| 62 | + jaxbMarshaller.setProperty(prop, value); |
| 63 | + } catch (PropertyException e) { |
| 64 | + throw new IllegalArgumentException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public synchronized String marshal(Object instance) throws JAXBException { |
| 69 | + return jaxbMarshaller.marshal(instance); |
| 70 | + } |
| 71 | + |
| 72 | + public void marshal(Object instance, Writer writer) throws JAXBException { |
| 73 | + jaxbMarshaller.marshal(instance, writer); |
| 74 | + } |
| 75 | + |
| 76 | + public void setSchema(Schema schema) { |
| 77 | + this.schema = schema; |
| 78 | + jaxbUnmarshaller.setSchema(schema); |
| 79 | + jaxbMarshaller.setSchema(schema); |
| 80 | + } |
| 81 | + |
| 82 | + public void validate(String str) throws IOException, SAXException { |
| 83 | + validate(new StringReader(str)); |
| 84 | + } |
| 85 | + |
| 86 | + public void validate(Reader reader) throws IOException, SAXException { |
| 87 | + schema.newValidator().validate(new StreamSource(reader)); |
| 88 | + } |
| 89 | +} |
0 commit comments