1 2 18 package com.sun.org.apache.xml.internal.security.transforms.implementations; 19 20 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 27 import javax.xml.XMLConstants ; 28 import javax.xml.transform.Source ; 29 import javax.xml.transform.Transformer ; 30 import javax.xml.transform.TransformerConfigurationException ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 import javax.xml.transform.stream.StreamSource ; 36 37 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 38 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; 39 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi; 40 import com.sun.org.apache.xml.internal.security.transforms.TransformationException; 41 import com.sun.org.apache.xml.internal.security.transforms.Transforms; 42 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 43 import org.w3c.dom.Element ; 44 45 46 54 public class TransformXSLT extends TransformSpi { 55 56 57 public static final String implementedTransformURI = 58 Transforms.TRANSFORM_XSLT; 59 static final String XSLTSpecNS = "http://www.w3.org/1999/XSL/Transform"; 61 static final String defaultXSLTSpecNSprefix = "xslt"; 62 static final String XSLTSTYLESHEET = "stylesheet"; 63 64 65 70 protected String engineGetURI() { 71 return implementedTransformURI; 72 } 73 74 82 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input) 83 throws IOException , 84 TransformationException { 85 return enginePerformTransform(input,null); 86 } 87 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input,OutputStream baos) 88 throws IOException , 89 TransformationException { 90 try { 91 Element transformElement = this._transformObject.getElement(); 92 93 Element _xsltElement = 94 XMLUtils.selectNode(transformElement.getFirstChild(), 95 XSLTSpecNS,"stylesheet", 0); 96 97 if (_xsltElement == null) { 98 Object exArgs[] = { "xslt:stylesheet", "Transform" }; 99 100 throw new TransformationException("xml.WrongContent", exArgs); 101 } 102 103 TransformerFactory tFactory = TransformerFactory.newInstance(); 104 tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); 106 107 113 Source xmlSource = 114 new StreamSource (new ByteArrayInputStream (input.getBytes())); 115 Source stylesheet; 116 117 125 { 126 ByteArrayOutputStream os = new ByteArrayOutputStream (); 127 Transformer transformer = tFactory.newTransformer(); 128 DOMSource source = new DOMSource (_xsltElement); 129 StreamResult result = new StreamResult (os); 130 131 transformer.transform(source, result); 132 133 stylesheet = 134 new StreamSource (new ByteArrayInputStream (os.toByteArray())); 135 } 136 137 Transformer transformer = tFactory.newTransformer(stylesheet); 138 if (baos==null) { 139 ByteArrayOutputStream baos1 = new ByteArrayOutputStream (); 140 StreamResult outputTarget = new StreamResult (baos1); 141 transformer.transform(xmlSource, outputTarget); 142 return new XMLSignatureInput(baos1.toByteArray()); 143 144 } 145 StreamResult outputTarget = new StreamResult (baos); 146 147 transformer.transform(xmlSource, outputTarget); 148 XMLSignatureInput output=new XMLSignatureInput((byte[])null); 149 output.setOutputStream(baos); 150 return output; 151 } catch (XMLSecurityException ex) { 152 Object exArgs[] = { ex.getMessage() }; 153 154 throw new TransformationException("generic.EmptyMessage", exArgs, ex); 155 } catch (TransformerConfigurationException ex) { 156 Object exArgs[] = { ex.getMessage() }; 157 158 throw new TransformationException("generic.EmptyMessage", exArgs, ex); 159 } catch (TransformerException ex) { 160 Object exArgs[] = { ex.getMessage() }; 161 162 throw new TransformationException("generic.EmptyMessage", exArgs, ex); 163 } 164 } 165 } 166 | Popular Tags |