1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.nutch.util;
18
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.UnsupportedEncodingException;
24
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerConfigurationException;
27 import javax.xml.transform.TransformerException;
28 import javax.xml.transform.TransformerFactory;
29 import javax.xml.transform.dom.DOMSource;
30 import javax.xml.transform.stream.StreamResult;
31
32 import org.apache.xerces.parsers.DOMParser;
33 import org.w3c.dom.Element;
34 import org.xml.sax.InputSource;
35 import org.xml.sax.SAXException;
36
37 // Commons Logging imports
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41
42 public class DomUtil {
43
44 private final static Log LOG = LogFactory.getLog(DomUtil.class);
45
46 /**
47 * Returns parsed dom tree or null if any error
48 *
49 * @param is
50 * @return A parsed DOM tree from the given {@link InputStream}.
51 */
52 public static Element getDom(InputStream is) {
53
54 Element element = null;
55
56 DOMParser parser = new DOMParser();
57
58 InputSource input;
59 try {
60 input = new InputSource(is);
61 input.setEncoding("UTF-8");
62 parser.parse(input);
63 int i = 0;
64 while (! (parser.getDocument().getChildNodes().item(i) instanceof Element)) {
65 i++;
66 }
67 element = (Element)parser.getDocument().getChildNodes().item(i);
68 } catch (FileNotFoundException e) {
69 e.printStackTrace(LogUtil.getWarnStream(LOG));
70 } catch (SAXException e) {
71 e.printStackTrace(LogUtil.getWarnStream(LOG));
72 } catch (IOException e) {
73 e.printStackTrace(LogUtil.getWarnStream(LOG));
74 }
75 return element;
76 }
77
78 /**
79 * save dom into ouputstream
80 *
81 * @param os
82 * @param e
83 */
84 public static void saveDom(OutputStream os, Element e) {
85
86 DOMSource source = new DOMSource(e);
87 TransformerFactory transFactory = TransformerFactory.newInstance();
88 Transformer transformer;
89 try {
90 transformer = transFactory.newTransformer();
91 transformer.setOutputProperty("indent", "yes");
92 StreamResult result = new StreamResult(os);
93 transformer.transform(source, result);
94 os.flush();
95 } catch (UnsupportedEncodingException e1) {
96 e1.printStackTrace(LogUtil.getWarnStream(LOG));
97 } catch (IOException e1) {
98 e1.printStackTrace(LogUtil.getWarnStream(LOG));
99 } catch (TransformerConfigurationException e2) {
100 e2.printStackTrace(LogUtil.getWarnStream(LOG));
101 } catch (TransformerException ex) {
102 ex.printStackTrace(LogUtil.getWarnStream(LOG));
103 }
104 }
105 }