This project has retired. For details please refer to its Attic page.
PrintJUDDI xref
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  import java.io.StringWriter;
18  
19  import javax.xml.bind.JAXBContext;
20  import javax.xml.bind.JAXBElement;
21  import javax.xml.bind.JAXBException;
22  import javax.xml.bind.Marshaller;
23  import javax.xml.namespace.QName;
24  
25  /**
26   * This is for printing jUDDI specific data objects only
27   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
28   * @param <T>
29   */
30  public class PrintJUDDI<T> {
31  
32  	static JAXBContext jaxbContext = null;
33  	
34  	private Marshaller getUDDIMarshaller() throws JAXBException {
35  		if (jaxbContext==null) {
36  			jaxbContext=JAXBContext.newInstance("org.apache.juddi.api_v3");
37  		}
38  		Marshaller marshaller = jaxbContext.createMarshaller();
39  		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
40  		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
41  		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
42  		
43  		return marshaller;
44  	}
45  	
46  	public String print(T UDDIEntity) {
47  		String xml = "";
48  		@SuppressWarnings("unchecked")
49  		Class<T> type = (Class<T>) UDDIEntity.getClass();
50  		try {
51  			StringWriter writer = new StringWriter();
52  			JAXBElement<T> element = new JAXBElement<T>(new QName("",UDDIEntity.getClass().getName()),type,UDDIEntity);
53  			getUDDIMarshaller().marshal(element,writer);
54  			xml=writer.toString();
55  		} catch (JAXBException je) {
56  			
57  		}
58  		return xml;
59  	}
60  
61  }