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 at7 * http://www.apache.org/licenses/LICENSE-2.08 * 9 * Unless required by applicable law or agreed to in writing, software10 * 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 and13 * limitations under the License.14 */15package org.apache.juddi.jaxb;
1617import java.io.StringWriter;
1819import javax.xml.bind.JAXBContext;
20import javax.xml.bind.JAXBElement;
21import javax.xml.bind.JAXBException;
22import javax.xml.bind.Marshaller;
23import javax.xml.namespace.QName;
2425/**26 * This is for printing jUDDI specific data objects only27 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>28 * @param <T>29 */30publicclass PrintJUDDI<T> {
3132static JAXBContext jaxbContext = null;
3334private Marshaller getUDDIMarshaller() throws JAXBException {
35if (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");
4243return marshaller;
44 }
4546public String print(T UDDIEntity) {
47 String xml = "";
48 @SuppressWarnings("unchecked")
49 Class<T> type = (Class<T>) UDDIEntity.getClass();
50try {
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) {
5657 }
58return xml;
59 }
6061 }