1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.encryption; 24 25 import java.io.ByteArrayInputStream; 26 import java.io.IOException; 27 import java.io.StringReader; 28 29 import javax.xml.XMLConstants; 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.ParserConfigurationException; 33 34 import org.w3c.dom.Document; 35 import org.w3c.dom.DocumentFragment; 36 import org.w3c.dom.Element; 37 import org.w3c.dom.Node; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.SAXException; 40 41 /** 42 * Converts <code>String</code>s into <code>Node</code>s and visa versa. 43 */ 44 public class DocumentSerializer extends AbstractSerializer { 45 46 protected DocumentBuilderFactory dbf; 47 48 /** 49 * @param source 50 * @param ctx 51 * @return the Node resulting from the parse of the source 52 * @throws XMLEncryptionException 53 */ 54 public Node deserialize(byte[] source, Node ctx) throws XMLEncryptionException { 55 byte[] fragment = createContext(source, ctx); 56 return deserialize(ctx, new InputSource(new ByteArrayInputStream(fragment))); 57 } 58 59 /** 60 * @param source 61 * @param ctx 62 * @return the Node resulting from the parse of the source 63 * @throws XMLEncryptionException 64 */ 65 public Node deserialize(String source, Node ctx) throws XMLEncryptionException { 66 String fragment = createContext(source, ctx); 67 return deserialize(ctx, new InputSource(new StringReader(fragment))); 68 } 69 70 /** 71 * @param ctx 72 * @param inputSource 73 * @return the Node resulting from the parse of the source 74 * @throws XMLEncryptionException 75 */ 76 private Node deserialize(Node ctx, InputSource inputSource) throws XMLEncryptionException { 77 try { 78 if (dbf == null) { 79 dbf = DocumentBuilderFactory.newInstance(); 80 dbf.setNamespaceAware(true); 81 dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); 82 dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE); 83 dbf.setValidating(false); 84 } 85 DocumentBuilder db = dbf.newDocumentBuilder(); 86 Document d = db.parse(inputSource); 87 88 Document contextDocument = null; 89 if (Node.DOCUMENT_NODE == ctx.getNodeType()) { 90 contextDocument = (Document)ctx; 91 } else { 92 contextDocument = ctx.getOwnerDocument(); 93 } 94 95 Element fragElt = 96 (Element) contextDocument.importNode(d.getDocumentElement(), true); 97 DocumentFragment result = contextDocument.createDocumentFragment(); 98 Node child = fragElt.getFirstChild(); 99 while (child != null) { 100 fragElt.removeChild(child); 101 result.appendChild(child); 102 child = fragElt.getFirstChild(); 103 } 104 return result; 105 } catch (SAXException se) { 106 throw new XMLEncryptionException("empty", se); 107 } catch (ParserConfigurationException pce) { 108 throw new XMLEncryptionException("empty", pce); 109 } catch (IOException ioe) { 110 throw new XMLEncryptionException("empty", ioe); 111 } 112 } 113 114 }