Changeset 1382
- Timestamp:
- 05/27/11 11:08:57 (13 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/pom.xml
r1347 r1382 65 65 <artifactId>commons-collections</artifactId> 66 66 <version>3.2.1</version> 67 </dependency> 68 <dependency> 69 <groupId>xml-resolver</groupId> 70 <artifactId>xml-resolver</artifactId> 71 <version>1.2</version> 67 72 </dependency> 68 73 <dependency> -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/MDMarshaller.java
r1356 r1382 31 31 import net.sf.saxon.event.SaxonOutputKeys; 32 32 33 import org.apache.xml.resolver.tools.CatalogResolver; 33 34 import org.slf4j.Logger; 34 35 import org.slf4j.LoggerFactory; 36 import org.w3c.dom.bootstrap.DOMImplementationRegistry; 37 import org.w3c.dom.ls.DOMImplementationLS; 38 import org.w3c.dom.ls.LSInput; 39 import org.w3c.dom.ls.LSResourceResolver; 40 import org.xml.sax.InputSource; 35 41 import org.xml.sax.SAXException; 36 42 … … 53 59 */ 54 60 public static <T> T unmarshal(Class<T> docClass, File file, Schema schema) { 55 56 57 58 59 60 61 62 63 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; 64 70 } 65 71 … … 73 79 */ 74 80 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; 84 91 } 85 92 … … 88 95 */ 89 96 public static <T> void marshal(T marshallableObject, OutputStream out) throws JAXBException, UnsupportedEncodingException { 90 91 97 String packageName = marshallableObject.getClass().getPackage().getName(); 98 JAXBContext jc = JAXBContext.newInstance(packageName); 92 99 93 94 95 96 97 98 99 100 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); 101 108 } 102 109 103 110 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; 115 146 } 116 147 117 148 public static void generateXsd(CMDComponentSpec spec, OutputStream outputStream) { 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 } 144 175 } 145 176 … … 150 181 */ 151 182 public static <T> String marshalToString(T marshallableObject) { 152 153 154 155 156 157 158 159 160 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(); 161 192 } 162 193 }
Note: See TracChangeset
for help on using the changeset viewer.