View Javadoc

1   /*
2    * Copyright (C) The MetaClass Group. All rights reserved.
3    *
4    * This software is published under the terms of the Spice
5    * Software License version 1.1, a copy of which has been included
6    * with this distribution in the LICENSE.txt file.
7    */
8   package org.codehaus.metaclass.io;
9   
10  import java.io.File;
11  import java.io.InputStream;
12  import java.io.OutputStream;
13  import java.util.Properties;
14  import javax.xml.parsers.DocumentBuilder;
15  import javax.xml.parsers.DocumentBuilderFactory;
16  import javax.xml.transform.OutputKeys;
17  import javax.xml.transform.TransformerFactory;
18  import javax.xml.transform.sax.SAXTransformerFactory;
19  import javax.xml.transform.sax.TransformerHandler;
20  import javax.xml.transform.stream.StreamResult;
21  import org.codehaus.metaclass.model.ClassDescriptor;
22  import org.w3c.dom.Document;
23  
24  /***
25   * This is a utility class that writes out the ClassDescriptor to a stream using
26   * the xml format outlined in documentation.
27   *
28   * @author Peter Donald
29   * @version $Revision: 1.12 $ $Date: 2003/12/11 08:41:50 $
30   */
31  public class MetaClassIOXml
32      extends AbstractMetaClassIO
33  {
34      /*** Constant with instance of MetaClassIO. */
35      public static final MetaClassIOXml IO = new MetaClassIOXml();
36  
37      /*** Extension of metadata files that are in xml format. */
38      public static final String EXTENSION = "-meta.xml";
39  
40      /*** The current version of ClassDescriptor XML format. */
41      static final String VERSION = "1.0";
42  
43      /*** Constant for name of class element. */
44      static final String CLASS_ELEMENT = "class";
45  
46      /*** Constant for name of fields element. */
47      static final String FIELDS_ELEMENT = "fields";
48  
49      /*** Constant for name of field element. */
50      static final String FIELD_ELEMENT = "field";
51  
52      /*** Constant for name of methods element. */
53      static final String METHODS_ELEMENT = "methods";
54  
55      /*** Constant for name of method element. */
56      static final String METHOD_ELEMENT = "method";
57  
58      /*** Constant for name of method parameters group element. */
59      static final String PARAMETERS_ELEMENT = "parameters";
60  
61      /*** Constant for name of method parameters element. */
62      static final String PARAMETER_ELEMENT = "parameter";
63  
64      /*** Constant for name of attributes element. */
65      static final String ATTRIBUTES_ELEMENT = "attributes";
66  
67      /*** Constant for name of attribute element. */
68      static final String ATTRIBUTE_ELEMENT = "attribute";
69  
70      /*** Constant for name of attribute parameter element. */
71      static final String PARAM_ELEMENT = "param";
72  
73      /*** Constant for name of name attribute. */
74      static final String NAME_ATTRIBUTE = "name";
75  
76      /*** Constant for name of type attribute. */
77      static final String TYPE_ATTRIBUTE = "type";
78  
79      /*** Constant for name of value attribute. */
80      static final String VALUE_ATTRIBUTE = "value";
81  
82      /*** Constant for name of version attribute. */
83      static final String VERSION_ATTRIBUTE = "version";
84  
85      /***
86       * @see MetaClassIO#deserializeClass(InputStream)
87       */
88      public ClassDescriptor deserializeClass( final InputStream input )
89          throws Exception
90      {
91          final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
92          final DocumentBuilder builder = factory.newDocumentBuilder();
93          final Document document = builder.parse( input );
94          final DOMMetaClassDeserializer deserializer = new DOMMetaClassDeserializer();
95          return deserializer.buildClassDescriptor( document );
96      }
97  
98      /***
99       * @see AbstractMetaClassIO#serializeClass(OutputStream, ClassDescriptor)
100      */
101     public void serializeClass( final OutputStream output,
102                                 final ClassDescriptor descriptor )
103         throws Exception
104     {
105         final StreamResult result = new StreamResult( output );
106         final SAXTransformerFactory factory =
107             (SAXTransformerFactory)TransformerFactory.newInstance();
108         final TransformerHandler handler = factory.newTransformerHandler();
109 
110         final Properties format = new Properties();
111         format.put( OutputKeys.METHOD, "xml" );
112         format.put( OutputKeys.INDENT, "yes" );
113         handler.setResult( result );
114         handler.getTransformer().setOutputProperties( format );
115 
116         final SAXMetaClassSerializer serializer = new SAXMetaClassSerializer();
117         try
118         {
119             serializer.serialize( handler, descriptor );
120         }
121         finally
122         {
123             output.flush();
124         }
125     }
126 
127     /***
128      * @see MetaClassIO#getResourceName(String)
129      */
130     public String getResourceName( final String classname )
131     {
132         return classname.replace( '.', File.separatorChar ) + EXTENSION;
133     }
134 }