This project has retired. For details please refer to its Attic page.
EntityCreator.java

EntityCreator.java

  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.  * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
  18.  * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
  19.  */
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.StringReader;
  24. import java.net.URL;

  25. import javax.xml.bind.JAXBContext;
  26. import javax.xml.bind.JAXBElement;
  27. import javax.xml.bind.JAXBException;
  28. import javax.xml.bind.Marshaller;
  29. import javax.xml.bind.Unmarshaller;

  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;

  32. /**
  33.  * This class is the logical opposite of PrintUDDI. It will create
  34.  * UDDI objects from a string or file.
  35.  * @see PrintUDDI
  36.  * @author unknown, but probably Kurt Stam
  37.  */
  38. public class EntityCreator {

  39.     public static final String UDDIv3_Package="org.uddi.api_v3";
  40.     public static final String JUDDIv3_Package="org.apache.juddi.api_v3";
  41.    
  42.     private static Log logger = LogFactory.getLog(EntityCreator.class);
  43.    
  44.         /**
  45.          * Builds UDDI data from a document, URL, file, etc
  46.          * @param fileName
  47.          * @param thePackage
  48.          * @return UDDI entity from the file or null
  49.          * @throws JAXBException
  50.          * @throws IOException
  51.          */
  52.     @SuppressWarnings("rawtypes")
  53.     public static Object buildFromDoc(String fileName, String thePackage) throws JAXBException, IOException {
  54.         Object obj = null;
  55.                 File f = new File(fileName);
  56.                 URL url=null;
  57.                 if (f.exists()){
  58.                     url = f.toURI().toURL();
  59.                 }
  60.                 if (url==null) {
  61.             url = Thread.currentThread().getContextClassLoader().getResource(fileName);
  62.                 }
  63.         if (url==null) {
  64.             logger.error("Could not find resource: " + fileName);
  65.         } else {
  66.             InputStream resourceStream =url.openStream();
  67.    
  68.             JAXBContext jc = JAXBContext.newInstance(thePackage);
  69.             Unmarshaller unmarshaller = jc.createUnmarshaller();
  70.             obj = ((JAXBElement)unmarshaller.unmarshal(resourceStream)).getValue();
  71.         }
  72.         return obj;
  73.     }

  74.         /**
  75.          * converts a XML in a String to a UDDI entity
  76.          * @param source
  77.          * @param thePackage
  78.          * @return UDDI entity from the file
  79.          * @throws JAXBException
  80.          * @throws IOException
  81.          */
  82.     @SuppressWarnings("rawtypes")
  83.     public static Object buildFromString(String source, String thePackage) throws JAXBException, IOException {
  84.         Object obj = null;
  85.         JAXBContext jc = JAXBContext.newInstance(thePackage);
  86.         Unmarshaller unmarshaller = jc.createUnmarshaller();
  87.         obj = ((JAXBElement)unmarshaller.unmarshal(new StringReader(source)));
  88.         return obj;
  89.     }
  90.        
  91.         /**
  92.          * Only use this class for debugging purposes. Output may not be valid XML
  93.          * @param obj
  94.          * @param thePackage
  95.          * @throws JAXBException
  96.          * @deprecated
  97.          */
  98.         @Deprecated
  99.     public static void outputEntity(Object obj, String thePackage) throws JAXBException {
  100.         JAXBContext jc = JAXBContext.newInstance(thePackage);
  101.         Marshaller marshaller = jc.createMarshaller();
  102.         marshaller.marshal( new JAXBElement<Object>(new javax.xml.namespace.QName("uri","local"), Object.class, obj), System.out);
  103.        
  104.     }
  105.    
  106. }