Clover coverage report - MetaClass - 1.1
Coverage timestamp: Tue Apr 27 2004 10:46:24 EST
file stats: LOC: 135   Methods: 3
NCLOC: 71   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
MetaClassIOXml.java - 100% 100% 100%
coverage
 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  4
     public ClassDescriptor deserializeClass( final InputStream input )
 89   
         throws Exception
 90   
     {
 91  4
         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 92  4
         final DocumentBuilder builder = factory.newDocumentBuilder();
 93  4
         final Document document = builder.parse( input );
 94  4
         final DOMMetaClassDeserializer deserializer = new DOMMetaClassDeserializer();
 95  4
         return deserializer.buildClassDescriptor( document );
 96   
     }
 97   
 
 98   
     /**
 99   
      * @see AbstractMetaClassIO#serializeClass(OutputStream, ClassDescriptor)
 100   
      */
 101  2
     public void serializeClass( final OutputStream output,
 102   
                                 final ClassDescriptor descriptor )
 103   
         throws Exception
 104   
     {
 105  2
         final StreamResult result = new StreamResult( output );
 106  2
         final SAXTransformerFactory factory =
 107   
             (SAXTransformerFactory)TransformerFactory.newInstance();
 108  2
         final TransformerHandler handler = factory.newTransformerHandler();
 109   
 
 110  2
         final Properties format = new Properties();
 111  2
         format.put( OutputKeys.METHOD, "xml" );
 112  2
         format.put( OutputKeys.INDENT, "yes" );
 113  2
         handler.setResult( result );
 114  2
         handler.getTransformer().setOutputProperties( format );
 115   
 
 116  2
         final SAXMetaClassSerializer serializer = new SAXMetaClassSerializer();
 117  2
         try
 118   
         {
 119  2
             serializer.serialize( handler, descriptor );
 120   
         }
 121   
         finally
 122   
         {
 123  2
             output.flush();
 124   
         }
 125   
     }
 126   
 
 127   
     /**
 128   
      * @see MetaClassIO#getResourceName(String)
 129   
      */
 130  12
     public String getResourceName( final String classname )
 131   
     {
 132  12
         return classname.replace( '.', File.separatorChar ) + EXTENSION;
 133   
     }
 134   
 }
 135