Coverage Report - org.seasar.uruma.rcp.configuration.writer.GenericConfigurationWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericConfigurationWriter
66%
47/71
72%
18/25
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 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  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.uruma.rcp.configuration.writer;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.Writer;
 20  
 import java.lang.reflect.Field;
 21  
 import java.util.List;
 22  
 
 23  
 import org.seasar.framework.beans.BeanDesc;
 24  
 import org.seasar.framework.beans.PropertyDesc;
 25  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 26  
 import org.seasar.framework.exception.IORuntimeException;
 27  
 import org.seasar.framework.util.FieldUtil;
 28  
 import org.seasar.framework.util.StringUtil;
 29  
 import org.seasar.uruma.annotation.ConfigurationAttribute;
 30  
 import org.seasar.uruma.core.UrumaConstants;
 31  
 import org.seasar.uruma.rcp.configuration.ConfigurationElement;
 32  
 import org.seasar.uruma.rcp.configuration.ConfigurationWriter;
 33  
 import org.seasar.uruma.rcp.configuration.elements.AbstractConfigurationElementContainer;
 34  
 import org.seasar.uruma.util.AnnotationUtil;
 35  
 
 36  
 /**
 37  
  * 汎用的な {@link ConfigurationWriter} です。<br />
 38  
  * 
 39  
  * @author y-komori
 40  
  */
 41  
 public class GenericConfigurationWriter implements ConfigurationWriter,
 42  
         UrumaConstants {
 43  
 
 44  
     private Class<? extends ConfigurationElement> supportType;
 45  
 
 46  
     private String elementName;
 47  
 
 48  
     private boolean startTagOnly;
 49  
 
 50  
     private static final String ELEMENT_NAME = "ELEMENT_NAME";
 51  
 
 52  
     /**
 53  
      * {@link GenericConfigurationWriter} を構築します。<br />
 54  
      * 
 55  
      * @param supportType
 56  
      *            対応する {@link ConfigurationElement} の型
 57  
      */
 58  
     public GenericConfigurationWriter(
 59  168
             final Class<? extends ConfigurationElement> supportType) {
 60  168
         this.supportType = supportType;
 61  168
         this.startTagOnly = AbstractConfigurationElementContainer.class
 62  
                 .isAssignableFrom(supportType) ? false : true;
 63  168
         BeanDesc desc = BeanDescFactory.getBeanDesc(supportType);
 64  168
         Field field = desc.getField(ELEMENT_NAME);
 65  168
         this.elementName = FieldUtil.getString(field);
 66  168
     }
 67  
 
 68  
     /**
 69  
      * {@link GenericConfigurationWriter} を構築します。<br />
 70  
      * 
 71  
      * @param supportType
 72  
      *            対応する {@link ConfigurationElement} の型
 73  
      * @param elementName
 74  
      *            要素名
 75  
      * @param startTagOnly
 76  
      *            <code>true</code> の場合、開始タグのみを出力する
 77  
      */
 78  
     public GenericConfigurationWriter(
 79  
             final Class<? extends ConfigurationElement> supportType,
 80  0
             final String elementName, final boolean startTagOnly) {
 81  0
         this.supportType = supportType;
 82  0
         this.elementName = elementName;
 83  0
         this.startTagOnly = startTagOnly;
 84  0
     }
 85  
 
 86  
     /*
 87  
      * @see org.seasar.uruma.rcp.configuration.ConfigurationWriter#getSupportType()
 88  
      */
 89  
     public Class<? extends ConfigurationElement> getSupportType() {
 90  168
         return this.supportType;
 91  
     }
 92  
 
 93  
     /*
 94  
      * @see org.seasar.uruma.rcp.configuration.ConfigurationWriter#writeStartTag(org.seasar.uruma.rcp.configuration.ConfigurationElement,
 95  
      *      java.io.Writer)
 96  
      */
 97  
     public void writeStartTag(final ConfigurationElement element,
 98  
             final Writer writer) {
 99  0
         writeStartTag(element, writer, 0);
 100  0
     }
 101  
 
 102  
     /*
 103  
      * @see org.seasar.uruma.rcp.configuration.ConfigurationWriter#writeStartTag(org.seasar.uruma.rcp.configuration.ConfigurationElement,
 104  
      *      java.io.Writer, int)
 105  
      */
 106  
     public void writeStartTag(final ConfigurationElement element,
 107  
             final Writer writer, final int level) {
 108  
         try {
 109  24
             writer.write(createIndent(level));
 110  24
             writer.write("<");
 111  24
             writer.write(elementName);
 112  24
             writer.write(WHITE_SPACE);
 113  
 
 114  24
             List<PropertyDesc> pdLists = AnnotationUtil
 115  
                     .getAnnotatedPropertyDescs(supportType,
 116  
                             ConfigurationAttribute.class);
 117  24
             for (PropertyDesc pd : pdLists) {
 118  56
                 Field field = pd.getField();
 119  56
                 ConfigurationAttribute anno = field
 120  
                         .getAnnotation(ConfigurationAttribute.class);
 121  56
                 String name = anno.name();
 122  56
                 if (StringUtil.isEmpty(name)) {
 123  40
                     name = field.getName();
 124  
                 }
 125  
 
 126  56
                 Object value = pd.getValue(element);
 127  
 
 128  56
                 if (value != null) {
 129  56
                     writer.write(name);
 130  56
                     writer.write("=\"");
 131  56
                     escapeChars(value.toString(), writer);
 132  56
                     writer.write("\" ");
 133  0
                 } else if (anno.required()) {
 134  0
                     writer.write(name);
 135  0
                     writer.write("=\"\" ");
 136  
                 }
 137  56
             }
 138  
 
 139  24
             if (startTagOnly) {
 140  12
                 writer.write("/>\n");
 141  
             } else {
 142  12
                 writer.write(">\n");
 143  
             }
 144  0
         } catch (IOException ex) {
 145  0
             throw new IORuntimeException(ex);
 146  24
         }
 147  24
     }
 148  
 
 149  
     /*
 150  
      * @see org.seasar.uruma.rcp.configuration.ConfigurationWriter#writeEndTag(org.seasar.uruma.rcp.configuration.ConfigurationElement,
 151  
      *      java.io.Writer)
 152  
      */
 153  
     public void writeEndTag(final ConfigurationElement element,
 154  
             final Writer writer) {
 155  0
         writeEndTag(element, writer, 0);
 156  0
     }
 157  
 
 158  
     /*
 159  
      * @see org.seasar.uruma.rcp.configuration.ConfigurationWriter#writeEndTag(org.seasar.uruma.rcp.configuration.ConfigurationElement,
 160  
      *      java.io.Writer, int)
 161  
      */
 162  
     public void writeEndTag(final ConfigurationElement element,
 163  
             final Writer writer, final int level) {
 164  24
         if (!startTagOnly) {
 165  
             try {
 166  12
                 writer.write(createIndent(level));
 167  12
                 writer.write("</" + elementName + ">\n");
 168  0
             } catch (IOException ex) {
 169  0
                 throw new IORuntimeException(ex);
 170  12
             }
 171  
         }
 172  24
     }
 173  
 
 174  
     protected void escapeChars(final String str, final Writer writer)
 175  
             throws IOException {
 176  248
         for (int i = 0; i < str.length(); i++) {
 177  192
             int chr = str.charAt(i);
 178  
 
 179  192
             switch (chr) {
 180  
             case '&':
 181  0
                 writer.write("&amp;");
 182  0
                 break;
 183  
 
 184  
             case '<':
 185  0
                 writer.write("&lt;");
 186  0
                 break;
 187  
 
 188  
             case '>':
 189  0
                 writer.write("&gt;");
 190  0
                 break;
 191  
 
 192  
             case '"':
 193  0
                 writer.write("&quot;");
 194  0
                 break;
 195  
 
 196  
             default:
 197  192
                 writer.write(chr);
 198  
             }
 199  
         }
 200  56
     }
 201  
 
 202  
     protected String createIndent(final int level) {
 203  36
         if (level > 0) {
 204  8
             StringBuilder builder = new StringBuilder(level);
 205  16
             for (int i = 0; i < level; i++) {
 206  8
                 builder.append(WHITE_SPACE);
 207  
             }
 208  8
             return builder.toString();
 209  
         } else {
 210  28
             return NULL_STRING;
 211  
         }
 212  
     }
 213  
 }