/* * Copyright (C) 2014 Jan Pokorsky * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package cz.cas.lib.proarc.nsesss2; import java.io.StringReader; import java.io.StringWriter; import java.net.URL; import javax.xml.bind.DataBindingException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; /** * The helper class for NSESSS2 format. * * @author Jan Pokorsky */ public final class NsesssUtils { private static JAXBContext defaultJaxbContext; private static ThreadLocal<Marshaller> defaultMarshaller = new ThreadLocal<Marshaller>(); private static ThreadLocal<Unmarshaller> defaultUnmarshaller = new ThreadLocal<Unmarshaller>(); /** * Default NSESSS2 context. Oracle JAXB RI's context should be thread safe. * @see <a href='http://jaxb.java.net/faq/index.html#threadSafety'>Are the JAXB runtime API's thread safe?</a> */ public static JAXBContext defaultJaxbContext() throws JAXBException { if (defaultJaxbContext == null) { defaultJaxbContext = JAXBContext.newInstance(ObjectFactory.class); } return defaultJaxbContext; } /** * Default NSESSS2 marshaller for current thread. */ public static Marshaller defaultMarshaller(boolean indent) throws JAXBException { Marshaller m = defaultMarshaller.get(); if (m == null) { // later we could use a pool to minimize Marshaller instances m = defaultJaxbContext().createMarshaller(); m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); defaultMarshaller.set(m); } m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, indent); return m; } /** * Default NSESSS2 marshaller for current thread. */ public static Unmarshaller defaultUnmarshaller() throws JAXBException { Unmarshaller m = defaultUnmarshaller.get(); if (m == null) { m = defaultJaxbContext().createUnmarshaller(); defaultUnmarshaller.set(m); } return m; } /** * Dumps object to XML string. */ public static String toXml(Object object, boolean indent) { StringWriter dump = new StringWriter(); marshal(new StreamResult(dump), object, indent); return dump.toString(); } public static void marshal(Result target, Object object, boolean indent) { try { Marshaller m = defaultMarshaller(indent); m.marshal(object, target); } catch (JAXBException ex) { throw new DataBindingException(ex); } } public static <T> T unmarshal(String source, Class<T> type) { return unmarshal(new StreamSource(new StringReader(source)), type); } public static <T> T unmarshal(URL source, Class<T> type) { return unmarshal(new StreamSource(source.toExternalForm()), type); } public static <T> T unmarshal(Source source, Class<T> type) { try { JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type); return item.getValue(); } catch (JAXBException ex) { throw new DataBindingException(ex); } } public static Spis defaultSpis() { URL template = NsesssUtils.class.getResource("model_desFolder_default.xml"); Spis spis = unmarshal(template, Spis.class); return spis; } public static Dokument defaultInternalDokument() { URL template = NsesssUtils.class.getResource("model_desInternalRecord_default.xml"); Dokument d = unmarshal(template, Dokument.class); return d; } public static Dokument defaultExternalDocument() { URL template = NsesssUtils.class.getResource("model_desExternalRecord_default.xml"); Dokument d = unmarshal(template, Dokument.class); return d; } }