This project has retired. For details please refer to its Attic page.
JAXBMarshaller xref
View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  package org.apache.juddi.jaxb;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URL;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBElement;
29  import javax.xml.bind.JAXBException;
30  import javax.xml.bind.Marshaller;
31  import javax.xml.bind.Unmarshaller;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.w3c.dom.Element;
36  
37  /**
38   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
39   */
40  public class JAXBMarshaller {
41  	private static Log logger = LogFactory.getLog(JAXBMarshaller.class);
42  	
43  	public static final String PACKAGE_UDDIAPI = "org.uddi.api_v3";
44  	public static final String PACKAGE_SUBSCRIPTION = "org.uddi.sub_v3";
45  	public static final String PACKAGE_SUBSCR_RES = "org.uddi.subr_v3";
46  	public static final String PACKAGE_JUDDIAPI = "org.apache.juddi.api_v3";
47  	public static final String PACKAGE_JUDDI = "org.apache.juddi";
48  	private static final Map<String, JAXBContext> JAXBContexts = new HashMap<String, JAXBContext>();
49  
50  	private static JAXBContext getContext(String packageName) {
51  		if (!JAXBContexts.containsKey(packageName)) {
52  			try {
53  				JAXBContexts.put(packageName, JAXBContext.newInstance(packageName));
54  			} catch (JAXBException e) {
55  				logger.error("Initialization of JAXBMarshaller failed:" + e, e);
56  				throw new ExceptionInInitializerError(e);
57  			}
58  		}
59  		
60  		return JAXBContexts.get(packageName);
61  	}
62  	
63  	@SuppressWarnings("rawtypes")
64  	public static Object unmarshallFromInputStream(InputStream inputStream, String thePackage) throws JAXBException {
65  		Object obj = null;
66  		if (inputStream != null) {
67  			JAXBContext jc = getContext(thePackage);
68  			Unmarshaller unmarshaller = jc.createUnmarshaller();
69  			obj = ((JAXBElement)unmarshaller.unmarshal(inputStream)).getValue();
70  		}
71  		else
72  			logger.error("A null input stream was provided");
73  
74  		return obj;
75  		
76  	}
77  	
78  	public static Object unmarshallFromFileResource(String fileName, String thePackage) throws JAXBException, IOException {
79  		Object obj = null;
80  		URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
81  		if (url==null) {
82  			logger.error("Could not find resource: " + fileName);
83  		} else {
84  			InputStream resourceStream =url.openStream();
85  	
86  			obj = unmarshallFromInputStream(resourceStream, thePackage);
87  		}
88  		return obj;
89  	}
90  
91  	public static Object unmarshallFromString(String rawObject, String thePackage) throws JAXBException {
92  		Object obj = null;
93  		if (rawObject != null && rawObject.length() > 0) {
94  			ByteArrayInputStream bais = new ByteArrayInputStream(rawObject.getBytes());
95  			obj = unmarshallFromInputStream(bais, thePackage);
96  		}
97  		else
98  			logger.error("The raw object provided is null or empty");
99  		return obj;
100 	}
101 
102 	public static String marshallToString(Object object, String thePackage) {
103 		String rawObject = null;
104 
105 		try {
106 			JAXBContext jc = getContext(thePackage);
107 			Marshaller marshaller = jc.createMarshaller();
108 			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
109 			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
110 			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
111 			ByteArrayOutputStream baos = new ByteArrayOutputStream();
112 			marshaller.marshal(object, baos);
113 			rawObject = baos.toString();
114 		} catch (JAXBException e) {
115 			logger.error(e.getMessage(),e);
116 		}
117 		
118 		return rawObject;
119 	}
120 
121 	public static Element marshallToElement(Object object, String thePackage, Element element) throws JAXBException {
122 		
123 		JAXBContext jc = getContext(thePackage);
124 		Marshaller marshaller = jc.createMarshaller();
125 		marshaller.marshal(object, element);	
126 		return element;
127 	}
128 	
129 	public static Object unmarshallFromElement(Element element, String thePackage) throws JAXBException {
130 		JAXBContext jc = getContext(thePackage);
131 		Unmarshaller unmarshaller = jc.createUnmarshaller();
132 		@SuppressWarnings("rawtypes")
133 		Object obj = ((JAXBElement) unmarshaller.unmarshal(element)).getValue();
134 		return obj;
135 	}
136 	
137 }