Mercurial > hg > mpdl-group
diff software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/XmlUtil.java @ 16:257f67be5c00
diverse Fehlerbehebungen
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 27 Sep 2011 16:40:57 +0200 |
parents | 1ec29fdd0db8 |
children |
line wrap: on
line diff
--- a/software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/XmlUtil.java Mon Aug 29 17:40:19 2011 +0200 +++ b/software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/XmlUtil.java Tue Sep 27 16:40:57 2011 +0200 @@ -13,11 +13,14 @@ import java.util.ArrayList; import java.util.Date; import java.util.Iterator; +import java.util.Properties; import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; @@ -25,8 +28,10 @@ import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; @@ -45,6 +50,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; @@ -140,6 +146,17 @@ return root; } + public void parse(File xmlFile) throws ApplicationException { + try { + SAXParserFactory factory = SAXParserFactory.newInstance(); + SAXParser saxParser = factory.newSAXParser(); + DefaultHandler dh = new DefaultHandler(); + saxParser.parse(xmlFile, dh); + } catch (Exception e) { + throw new ApplicationException(e); + } + } + public void validateByRelaxNG(File xmlFile, URL schemaUrl) throws ApplicationException { System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); @@ -649,4 +666,54 @@ } return xmlString; } + + public String transform(String xmlString, String xslFileName, Properties outputProperties) throws ApplicationException { + String resultString = null; + try { + StreamSource xslSource = new StreamSource(xslFileName); + Transformer transformer = TransformerFactory.newInstance(net.sf.saxon.TransformerFactoryImpl.class.getName(), null).newTransformer(xslSource); + if (outputProperties != null) { + String propValue = outputProperties.getProperty("method"); + if (propValue != null) + transformer.setOutputProperty(OutputKeys.METHOD, propValue); + propValue = outputProperties.getProperty("indent"); + if (propValue != null) + transformer.setOutputProperty(OutputKeys.INDENT, propValue); + propValue = outputProperties.getProperty("media-type"); + if (propValue != null) + transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, propValue); + propValue = outputProperties.getProperty("omit-xml-declaration"); + if (propValue != null) + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, propValue); + propValue = outputProperties.getProperty("encoding"); + if (propValue != null) + transformer.setOutputProperty(OutputKeys.ENCODING, propValue); + } + StreamResult result = new StreamResult(new StringWriter()); + StreamSource source = new StreamSource(new StringReader(xmlString)); + transformer.transform(source, result); + resultString = result.getWriter().toString(); + } catch (TransformerConfigurationException e) { + throw new ApplicationException(e); + } catch (TransformerException e) { + throw new ApplicationException(e); + } + return resultString; + } + + public SAXResult transformToSaxResult(String xmlString, String xslString, Properties outputProperties) throws ApplicationException { + SAXResult result = new SAXResult(); + try { + StreamSource xslSource = new StreamSource(new StringReader(xslString)); + Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource); + transformer.setOutputProperties(outputProperties); + StreamSource source = new StreamSource(new StringReader(xmlString)); + transformer.transform(source, result); + } catch (TransformerConfigurationException e) { + throw new ApplicationException(e); + } catch (TransformerException e) { + throw new ApplicationException(e); + } + return result; + } }