Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/Rest/RestfulDTOs/DTOsupport/src/org/litesoft/rest/adapters/JAXB_Helper.java

Diff revisions: vs.
  @@ -1,74 +1,74 @@
1 - package org.litesoft.rest.adapters;
2 -
3 - import java.io.*;
4 - import javax.xml.bind.*;
5 -
6 - import org.litesoft.rest.utils.*;
7 -
8 - @SuppressWarnings({"UnusedDeclaration"})
9 - public class JAXB_Helper
10 - {
11 - private static final String OBJECT_FACTORY_NAME_SUFFIX = ".ObjectFactory";
12 -
13 - private JAXBContext mContext;
14 -
15 - public JAXB_Helper( Class pObjectFactoryClass )
16 - throws JAXBException
17 - {
18 - notNull( "Object Factory Class", pObjectFactoryClass );
19 - String zName = pObjectFactoryClass.getName();
20 - if ( !zName.endsWith( OBJECT_FACTORY_NAME_SUFFIX ) )
21 - {
22 - throw new IllegalArgumentException( "Object Factory Class Name did NOT end with: " + OBJECT_FACTORY_NAME_SUFFIX );
23 - }
24 - mContext = JAXBContext.newInstance( zName.substring( 0, zName.length() - OBJECT_FACTORY_NAME_SUFFIX.length() ) );
25 - }
26 -
27 - public String toXML( JAXBgeneratedClassSuperClass pInstance )
28 - throws JAXBException
29 - {
30 - StringWriter zWriter = new StringWriter();
31 - toXML( pInstance, zWriter );
32 - return zWriter.toString();
33 - }
34 -
35 - public void toXML( JAXBgeneratedClassSuperClass pInstance, Writer pWriter )
36 - throws JAXBException
37 - {
38 - notNull( "Instance to convert to XML", pInstance );
39 - notNull( "Writer", pWriter );
40 -
41 - // Currently we are creating a new Marshaller each time as the spec does specify reentrancy
42 - Marshaller zMarshaller = mContext.createMarshaller();
43 -
44 - zMarshaller.marshal( pInstance, pWriter );
45 - }
46 -
47 - public <T extends JAXBgeneratedClassSuperClass> T fromXML( Class<T> pType, String pXML )
48 - throws JAXBException
49 - {
50 - notNull( "XML", pXML );
51 - return fromXML( pType, new StringReader( pXML ) );
52 - }
53 -
54 - public <T extends JAXBgeneratedClassSuperClass> T fromXML( Class<T> pType, Reader pReader )
55 - throws JAXBException
56 - {
57 - notNull( "Type create from XML", pType );
58 - notNull( "Reader", pReader );
59 -
60 - // Currently we are creating a new Unmarshaller each time as the spec does specify reentrancy
61 - Unmarshaller zUnmarshaller = mContext.createUnmarshaller();
62 -
63 - Object o = zUnmarshaller.unmarshal( pReader );
64 - return Unchecked.cast( pType, o );
65 - }
66 -
67 - private static void notNull( String pWhat, Object pToCheck )
68 - {
69 - if ( pToCheck == null )
70 - {
71 - throw new IllegalArgumentException( pWhat + " was Null" );
72 - }
73 - }
74 - }
1 + package org.litesoft.rest.adapters;
2 +
3 + import java.io.*;
4 + import javax.xml.bind.*;
5 +
6 + import org.litesoft.rest.utils.*;
7 +
8 + @SuppressWarnings({"UnusedDeclaration"})
9 + public class JAXB_Helper
10 + {
11 + private static final String OBJECT_FACTORY_NAME_SUFFIX = ".ObjectFactory";
12 +
13 + private JAXBContext mContext;
14 +
15 + public JAXB_Helper( Class pObjectFactoryClass )
16 + throws JAXBException
17 + {
18 + notNull( "Object Factory Class", pObjectFactoryClass );
19 + String zName = pObjectFactoryClass.getName();
20 + if ( !zName.endsWith( OBJECT_FACTORY_NAME_SUFFIX ) )
21 + {
22 + throw new IllegalArgumentException( "Object Factory Class Name did NOT end with: " + OBJECT_FACTORY_NAME_SUFFIX );
23 + }
24 + mContext = JAXBContext.newInstance( zName.substring( 0, zName.length() - OBJECT_FACTORY_NAME_SUFFIX.length() ) );
25 + }
26 +
27 + public String toXML( JAXBgeneratedClassSuperClass pInstance )
28 + throws JAXBException
29 + {
30 + StringWriter zWriter = new StringWriter();
31 + toXML( pInstance, zWriter );
32 + return zWriter.toString();
33 + }
34 +
35 + public void toXML( JAXBgeneratedClassSuperClass pInstance, Writer pWriter )
36 + throws JAXBException
37 + {
38 + notNull( "Instance to convert to XML", pInstance );
39 + notNull( "Writer", pWriter );
40 +
41 + // Currently we are creating a new Marshaller each time as the spec does specify reentrancy
42 + Marshaller zMarshaller = mContext.createMarshaller();
43 +
44 + zMarshaller.marshal( pInstance, pWriter );
45 + }
46 +
47 + public <T extends JAXBgeneratedClassSuperClass> T fromXML( Class<T> pType, String pXML )
48 + throws JAXBException
49 + {
50 + notNull( "XML", pXML );
51 + return fromXML( pType, new StringReader( pXML ) );
52 + }
53 +
54 + public <T extends JAXBgeneratedClassSuperClass> T fromXML( Class<T> pType, Reader pReader )
55 + throws JAXBException
56 + {
57 + notNull( "Type create from XML", pType );
58 + notNull( "Reader", pReader );
59 +
60 + // Currently we are creating a new Unmarshaller each time as the spec does specify reentrancy
61 + Unmarshaller zUnmarshaller = mContext.createUnmarshaller();
62 +
63 + Object o = zUnmarshaller.unmarshal( pReader );
64 + return Unchecked.cast( pType, o );
65 + }
66 +
67 + private static void notNull( String pWhat, Object pToCheck )
68 + {
69 + if ( pToCheck == null )
70 + {
71 + throw new IllegalArgumentException( pWhat + " was Null" );
72 + }
73 + }
74 + }