This project has retired. For details please refer to its Attic page.
XmlUtils xref
View Javadoc
1   /*
2    * Copyright 2017 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v3.client.cryptor;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Reader;
22  import java.net.URL;
23  import javax.xml.bind.JAXBContext;
24  import javax.xml.bind.Unmarshaller;
25  import javax.xml.parsers.SAXParserFactory;
26  import javax.xml.transform.Source;
27  import javax.xml.transform.sax.SAXSource;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.xml.sax.InputSource;
31  
32  /**
33   *
34   * @since 3.3.5
35   * @author Alex O'Ree
36   */
37  public class XmlUtils {
38  
39          private static final Log log = LogFactory.getLog(XmlUtils.class);
40  
41          public static Object unmarshal(Reader reader, Class...clazz) {
42  
43                  try {
44                          SAXParserFactory spf = SAXParserFactory.newInstance();
45                          spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
46                          spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
47                          spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
48                          spf.setNamespaceAware(true);
49  
50                          Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader));
51                          JAXBContext jc = JAXBContext.newInstance(clazz);
52                          Unmarshaller um = jc.createUnmarshaller();
53                          return um.unmarshal(xmlSource);
54                  } catch (Exception ex) {
55                          log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 1" + ex.getMessage());
56                          log.debug(ex.getMessage(), ex);
57                  }
58                  return null;
59  
60          }
61  
62          public static Object unmarshal(InputStream reader, Class clazz) {
63                  try {
64                          SAXParserFactory spf = SAXParserFactory.newInstance();
65                          spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
66                          spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
67                          spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
68                          spf.setNamespaceAware(true);
69                          Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader));
70                          JAXBContext jc = JAXBContext.newInstance(clazz);
71                          Unmarshaller um = jc.createUnmarshaller();
72                          return um.unmarshal(xmlSource);
73                  } catch (Exception ex) {
74                          log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 2" + ex.getMessage());
75                          log.debug(ex.getMessage(), ex);
76                  }
77                  return null;
78  
79          }
80  
81          public static Object unmarshal(Reader reader, String packageName) {
82                  try {
83                          SAXParserFactory spf = SAXParserFactory.newInstance();
84                          spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
85                          spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
86                          spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
87                          spf.setNamespaceAware(true);
88                          Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader));
89                          JAXBContext jc = JAXBContext.newInstance(packageName);
90                          
91                          Unmarshaller um = jc.createUnmarshaller();
92                          return ((javax.xml.bind.JAXBElement)um.unmarshal(xmlSource)).getValue();
93                  } catch (Exception ex) {
94                          log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 3" + ex.getMessage());
95                          log.debug(ex.getMessage(), ex);
96                  }
97                  return null;
98  
99          }
100 
101         public static Object unmarshal(URL url, Class clazz) {
102                 InputStream openStream = null;
103                 Object obj = null;
104                 try {
105                         SAXParserFactory spf = SAXParserFactory.newInstance();
106                         spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
107                         spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
108                         spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
109                         spf.setNamespaceAware(true);
110                         openStream = url.openStream();
111                         Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(openStream));
112                         JAXBContext jc = JAXBContext.newInstance(clazz);
113                         Unmarshaller um = jc.createUnmarshaller();
114                         obj = um.unmarshal(xmlSource);
115                 } catch (Exception ex) {
116                         log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 4" + ex.getMessage());
117                         log.debug(ex.getMessage(), ex);
118                 } finally {
119                         if (openStream != null) {
120                                 try {
121                                         openStream.close();
122                                 } catch (IOException ex) {
123                                         log.debug(ex.getMessage(), ex);
124                                 }
125                         }
126                 }
127                 return obj;
128         }
129 
130         public static Object unmarshal(File file, Class clazz)  {
131                 try {
132                         return unmarshal(file.toURI().toURL(), clazz);
133                 } catch (Exception ex) {
134                         log.warn("Failed to unmarshall object. Increase logging to debug for additional information. 5" + ex.getMessage());
135                         log.debug(ex.getMessage(), ex);
136                 }
137                 return null;
138         }
139 }