View Javadoc
1   /*
2    * Copyright 2001-2009 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    *      http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.apache.juddi.jaxb;
16  
17  /**
18   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
19   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
20   */
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.StringReader;
25  import java.net.URL;
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  
36  /**
37   * This class is the logical opposite of PrintUDDI. It will create
38   * UDDI objects from a string or file.
39   * @see PrintUDDI
40   * @author unknown, but probably Kurt Stam
41   */
42  public class EntityCreator {
43  
44      public static final String UDDIv3_Package="org.uddi.api_v3";
45      public static final String JUDDIv3_Package="org.apache.juddi.api_v3";
46      
47  	private static Log logger = LogFactory.getLog(EntityCreator.class);
48  	
49          /**
50           * Builds UDDI data from a document, URL, file, etc
51           * @param fileName
52           * @param thePackage
53           * @return UDDI entity from the file or null
54           * @throws JAXBException
55           * @throws IOException 
56           */
57  	@SuppressWarnings("rawtypes")
58  	public static Object buildFromDoc(String fileName, String thePackage) throws JAXBException, IOException {
59  		Object obj = null;
60                  File f = new File(fileName);
61                  URL url=null;
62                  if (f.exists()){
63                      url = f.toURI().toURL();
64                  }
65                  if (url==null) {
66  		    url = Thread.currentThread().getContextClassLoader().getResource(fileName);
67                  }
68  		if (url==null) {
69  			logger.error("Could not find resource: " + fileName);
70  		} else {
71  			InputStream resourceStream =url.openStream();
72  	
73  			JAXBContext jc = JAXBContext.newInstance(thePackage);
74  			Unmarshaller unmarshaller = jc.createUnmarshaller();
75  			obj = ((JAXBElement)unmarshaller.unmarshal(resourceStream)).getValue();
76  		}
77  		return obj;
78  	}
79  
80          /**
81           * converts a XML in a String to a UDDI entity
82           * @param source
83           * @param thePackage
84           * @return UDDI entity from the file
85           * @throws JAXBException
86           * @throws IOException 
87           */
88  	@SuppressWarnings("rawtypes")
89  	public static Object buildFromString(String source, String thePackage) throws JAXBException, IOException {
90  		Object obj = null;
91  		JAXBContext jc = JAXBContext.newInstance(thePackage);
92  		Unmarshaller unmarshaller = jc.createUnmarshaller();
93  		obj = ((JAXBElement)unmarshaller.unmarshal(new StringReader(source)));
94  		return obj;
95  	}
96  		
97          /**
98           * Only use this class for debugging purposes. Output may not be valid XML
99           * @param obj
100          * @param thePackage
101          * @throws JAXBException
102          * @deprecated
103          */
104         @Deprecated
105 	public static void outputEntity(Object obj, String thePackage) throws JAXBException {
106 		JAXBContext jc = JAXBContext.newInstance(thePackage);
107 		Marshaller marshaller = jc.createMarshaller();
108 		marshaller.marshal( new JAXBElement<Object>(new javax.xml.namespace.QName("uri","local"), Object.class, obj), System.out);
109 		
110 	}
111 	
112 }