Changeset 1382


Ignore:
Timestamp:
05/27/11 11:08:57 (13 years ago)
Author:
patdui
Message:
Location:
ComponentRegistry/trunk/ComponentRegistry
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/pom.xml

    r1347 r1382  
    6565            <artifactId>commons-collections</artifactId>
    6666            <version>3.2.1</version>
     67        </dependency>
     68        <dependency>
     69            <groupId>xml-resolver</groupId>
     70            <artifactId>xml-resolver</artifactId>
     71            <version>1.2</version>
    6772        </dependency>
    6873        <dependency>
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/MDMarshaller.java

    r1356 r1382  
    3131import net.sf.saxon.event.SaxonOutputKeys;
    3232
     33import org.apache.xml.resolver.tools.CatalogResolver;
    3334import org.slf4j.Logger;
    3435import org.slf4j.LoggerFactory;
     36import org.w3c.dom.bootstrap.DOMImplementationRegistry;
     37import org.w3c.dom.ls.DOMImplementationLS;
     38import org.w3c.dom.ls.LSInput;
     39import org.w3c.dom.ls.LSResourceResolver;
     40import org.xml.sax.InputSource;
    3541import org.xml.sax.SAXException;
    3642
     
    5359     */
    5460    public static <T> T unmarshal(Class<T> docClass, File file, Schema schema) {
    55         T result = null;
    56         try {
    57             result = unmarshal(docClass, new FileInputStream(file), schema);
    58         } catch (JAXBException e) {
    59             LOG.error("Cannot unmarshal xml file: " + file, e);
    60         } catch (IOException e) {
    61             LOG.error("Cannot retrieve content from file: " + file, e);
    62         }
    63         return result;
     61        T result = null;
     62        try {
     63            result = unmarshal(docClass, new FileInputStream(file), schema);
     64        } catch (JAXBException e) {
     65            LOG.error("Cannot unmarshal xml file: " + file, e);
     66        } catch (IOException e) {
     67            LOG.error("Cannot retrieve content from file: " + file, e);
     68        }
     69        return result;
    6470    }
    6571
     
    7379     */
    7480    public static <T> T unmarshal(Class<T> docClass, InputStream inputStream, Schema schema) throws JAXBException {
    75         String packageName = docClass.getPackage().getName();
    76         JAXBContext jc = JAXBContext.newInstance(packageName);
    77         Unmarshaller u = jc.createUnmarshaller();
    78         if (schema != null) {
    79             u.setSchema(schema);
    80         }
    81         Object unmarshal = u.unmarshal(inputStream);
    82         T doc = (T) unmarshal;
    83         return doc;
     81        String packageName = docClass.getPackage().getName();
     82        JAXBContext jc = JAXBContext.newInstance(packageName);
     83        Unmarshaller u = jc.createUnmarshaller();
     84
     85        if (schema != null) {
     86            u.setSchema(schema);
     87        }
     88        Object unmarshal = u.unmarshal(inputStream);
     89        T doc = (T) unmarshal;
     90        return doc;
    8491    }
    8592
     
    8895     */
    8996    public static <T> void marshal(T marshallableObject, OutputStream out) throws JAXBException, UnsupportedEncodingException {
    90         String packageName = marshallableObject.getClass().getPackage().getName();
    91         JAXBContext jc = JAXBContext.newInstance(packageName);
     97        String packageName = marshallableObject.getClass().getPackage().getName();
     98        JAXBContext jc = JAXBContext.newInstance(packageName);
    9299
    93         Marshaller m = jc.createMarshaller();
    94         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    95         String schemaLocation = Configuration.getInstance().getSchemaLocation(marshallableObject.getClass().getName());
    96         if (schemaLocation != null) {
    97             m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
    98         }
    99         Writer writer = new OutputStreamWriter(out, "UTF-8");
    100         m.marshal(marshallableObject, writer);
     100        Marshaller m = jc.createMarshaller();
     101        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
     102        String schemaLocation = Configuration.getInstance().getSchemaLocation(marshallableObject.getClass().getName());
     103        if (schemaLocation != null) {
     104            m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
     105        }
     106        Writer writer = new OutputStreamWriter(out, "UTF-8");
     107        m.marshal(marshallableObject, writer);
    101108    }
    102109
    103110    public static Schema getCMDComponentSchema() {
    104         if (generalComponentSchema == null) {
    105             try {
    106                 generalComponentSchema = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI).newSchema(
    107                         new URL(Configuration.getInstance().getGeneralComponentSchema()));
    108             } catch (MalformedURLException e) {
    109                 LOG.error("Cannot instantiate schema", e);
    110             } catch (SAXException e) {
    111                 LOG.error("Cannot instantiate schema", e);
    112             }
    113         }
    114         return generalComponentSchema;
     111        if (generalComponentSchema == null) {
     112            try {
     113                SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
     114                schemaFactory.setResourceResolver(new LSResourceResolver() {
     115                    private CatalogResolver catRes = new CatalogResolver();
     116
     117                    @Override
     118                    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
     119                        InputSource resolveEntity = catRes.resolveEntity(publicId, systemId);
     120                        DOMImplementationLS domImplementation;
     121                        try {
     122                            domImplementation = (DOMImplementationLS) DOMImplementationRegistry.newInstance().getDOMImplementation("LS");
     123                        } catch (ClassCastException e) {
     124                            throw new RuntimeException(e);
     125                        } catch (ClassNotFoundException e) {
     126                            throw new RuntimeException(e);
     127                        } catch (InstantiationException e) {
     128                            throw new RuntimeException(e);
     129                        } catch (IllegalAccessException e) {
     130                            throw new RuntimeException(e);
     131                        }
     132                        LSInput lsInput = domImplementation.createLSInput();
     133                        lsInput.setByteStream(resolveEntity.getByteStream());
     134                        lsInput.setCharacterStream(resolveEntity.getCharacterStream());
     135                        return lsInput;
     136                    }
     137                });
     138                generalComponentSchema = schemaFactory.newSchema(new URL(Configuration.getInstance().getGeneralComponentSchema()));
     139            } catch (MalformedURLException e) {
     140                LOG.error("Cannot instantiate schema", e);
     141            } catch (SAXException e) {
     142                LOG.error("Cannot instantiate schema", e);
     143            }
     144        }
     145        return generalComponentSchema;
    115146    }
    116147
    117148    public static void generateXsd(CMDComponentSpec spec, OutputStream outputStream) {
    118         Templates componentToSchemaTemplates;
    119         try {
    120             System.setProperty("javax.xml.transform.TransformerFactory", net.sf.saxon.TransformerFactoryImpl.class.getName());
    121             componentToSchemaTemplates = TransformerFactory.newInstance().newTemplates(
    122                     new StreamSource(Configuration.getInstance().getComponent2SchemaXsl()));
    123         } catch (TransformerConfigurationException e) {
    124             LOG.error("Cannot create Template", e);
    125             return;
    126         }
    127         try {
    128             Transformer transformer = componentToSchemaTemplates.newTransformer();
    129             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    130             transformer.setOutputProperty(SaxonOutputKeys.INDENT_SPACES, "1"); //Keeps the downloads a lot smaller.
    131             ByteArrayOutputStream out = new ByteArrayOutputStream();
    132             MDMarshaller.marshal(spec, out);
    133             ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
    134             transformer.transform(new StreamSource(input), new StreamResult(outputStream));
    135         } catch (TransformerConfigurationException e) {
    136             LOG.error("Cannot create Transformer", e);
    137         } catch (TransformerException e) {
    138             LOG.error("Cannot transform xml file: " + spec, e);
    139         } catch (UnsupportedEncodingException e) {
    140             LOG.error("Error in encoding: ", e);
    141         } catch (JAXBException e) {
    142             LOG.error("Cannot marshall spec: " + spec, e);
    143         }
     149        Templates componentToSchemaTemplates;
     150        try {
     151            System.setProperty("javax.xml.transform.TransformerFactory", net.sf.saxon.TransformerFactoryImpl.class.getName());
     152            componentToSchemaTemplates = TransformerFactory.newInstance().newTemplates(
     153                    new StreamSource(Configuration.getInstance().getComponent2SchemaXsl()));
     154        } catch (TransformerConfigurationException e) {
     155            LOG.error("Cannot create Template", e);
     156            return;
     157        }
     158        try {
     159            Transformer transformer = componentToSchemaTemplates.newTransformer();
     160            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     161            transformer.setOutputProperty(SaxonOutputKeys.INDENT_SPACES, "1"); //Keeps the downloads a lot smaller.
     162            ByteArrayOutputStream out = new ByteArrayOutputStream();
     163            MDMarshaller.marshal(spec, out);
     164            ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
     165            transformer.transform(new StreamSource(input), new StreamResult(outputStream));
     166        } catch (TransformerConfigurationException e) {
     167            LOG.error("Cannot create Transformer", e);
     168        } catch (TransformerException e) {
     169            LOG.error("Cannot transform xml file: " + spec, e);
     170        } catch (UnsupportedEncodingException e) {
     171            LOG.error("Error in encoding: ", e);
     172        } catch (JAXBException e) {
     173            LOG.error("Cannot marshall spec: " + spec, e);
     174        }
    144175    }
    145176
     
    150181     */
    151182    public static <T> String marshalToString(T marshallableObject) {
    152         ByteArrayOutputStream out = new ByteArrayOutputStream();
    153         try {
    154             marshal(marshallableObject, out);
    155         } catch (UnsupportedEncodingException e) {
    156             throw new RuntimeException(e);
    157         } catch (JAXBException e) {
    158             throw new RuntimeException(e);
    159         }
    160         return out.toString();
     183        ByteArrayOutputStream out = new ByteArrayOutputStream();
     184        try {
     185            marshal(marshallableObject, out);
     186        } catch (UnsupportedEncodingException e) {
     187            throw new RuntimeException(e);
     188        } catch (JAXBException e) {
     189            throw new RuntimeException(e);
     190        }
     191        return out.toString();
    161192    }
    162193}
Note: See TracChangeset for help on using the changeset viewer.