Coverage Report - org.seasar.uruma.component.writer.UIElementWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
UIElementWriter
0%
0/98
0%
0/30
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.component.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.util.FieldUtil;
 24  
 import org.seasar.framework.util.StringUtil;
 25  
 import org.seasar.uruma.annotation.ComponentAttribute;
 26  
 import org.seasar.uruma.annotation.ComponentElement;
 27  
 import org.seasar.uruma.component.Template;
 28  
 import org.seasar.uruma.component.UIComponentContainer;
 29  
 import org.seasar.uruma.component.UIElement;
 30  
 import org.seasar.uruma.component.UIElementContainer;
 31  
 import org.seasar.uruma.component.UIElementVisitor;
 32  
 import org.seasar.uruma.core.UrumaConstants;
 33  
 import org.seasar.uruma.util.AnnotationUtil;
 34  
 import org.seasar.uruma.util.AssertionUtil;
 35  
 
 36  
 /**
 37  
  * {@link UIElement} を XML ファイルへ出力するためのクラスです。<br />
 38  
  * 
 39  
  * @author y-komori
 40  
  */
 41  
 public class UIElementWriter implements UIElementVisitor, UrumaConstants {
 42  
     protected Writer writer;
 43  
 
 44  
     private int indentLevel;
 45  
 
 46  
     private static final String INDENT = "  ";
 47  
 
 48  
     /**
 49  
      * {@link UIElementWriter} を構築します。<br />
 50  
      * 
 51  
      * @param writer
 52  
      *            XML ファイルの出力先 {@link Writer} オブジェクト。
 53  
      */
 54  0
     public UIElementWriter(final Writer writer) {
 55  0
         AssertionUtil.assertNotNull("writer", writer);
 56  0
         this.writer = writer;
 57  0
     }
 58  
 
 59  
     /*
 60  
      * @see org.seasar.uruma.component.UIElementVisitor#visit(org.seasar.uruma.component.UIElement)
 61  
      */
 62  
     public void visit(final UIElement element) {
 63  0
         AssertionUtil.assertNotNull("element", element);
 64  
 
 65  
         try {
 66  0
             writeStartTag(element, true);
 67  0
         } catch (IOException ex) {
 68  
             // TODO 自動生成された catch ブロック
 69  0
             ex.printStackTrace();
 70  0
         }
 71  0
     }
 72  
 
 73  
     /*
 74  
      * @see org.seasar.uruma.component.UIElementVisitor#visit(org.seasar.uruma.component.UIElementContainer)
 75  
      */
 76  
     public void visit(final UIElementContainer container) {
 77  0
         AssertionUtil.assertNotNull("container", container);
 78  
 
 79  
         try {
 80  0
             writeStartTag(container, false);
 81  
 
 82  0
             indent();
 83  0
             for (UIElement element : container.getChildren()) {
 84  0
                 element.accept(this);
 85  
             }
 86  0
             deindent();
 87  
 
 88  0
             writeEndTag(container);
 89  0
         } catch (IOException ex) {
 90  
             // TODO 自動生成された catch ブロック
 91  0
             ex.printStackTrace();
 92  0
         }
 93  0
     }
 94  
 
 95  
     /*
 96  
      * @see org.seasar.uruma.component.UIElementVisitor#visit(org.seasar.uruma.component.Template)
 97  
      */
 98  
     public void visit(final Template template) {
 99  0
         AssertionUtil.assertNotNull("template", template);
 100  
         try {
 101  0
             writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
 102  0
             writer
 103  
                     .write("<template xmlns=\"http://uruma.sandbox.seasar.org\">\n");
 104  0
             indent();
 105  
 
 106  0
             UIComponentContainer root = template.getRootComponent();
 107  0
             if (root != null) {
 108  0
                 root.accept(this);
 109  
             }
 110  
 
 111  0
             deindent();
 112  0
             writer.write("</template>\n");
 113  0
             writer.flush();
 114  0
         } catch (IOException ex) {
 115  
             // TODO: handle exception
 116  0
         }
 117  0
     }
 118  
 
 119  
     protected void writeStartTag(final UIElement element,
 120  
             final boolean startTagOnly) throws IOException {
 121  0
         String elementName = getElementName(element.getClass());
 122  0
         if (elementName == null) {
 123  
             // TODO Warning 出力
 124  0
             return;
 125  
         }
 126  
 
 127  0
         writeIndent();
 128  0
         writer.write('<');
 129  0
         writer.write(elementName);
 130  
 
 131  0
         List<Field> fields = AnnotationUtil.getAnnotatedFields(element
 132  
                 .getClass(), ComponentAttribute.class);
 133  
 
 134  0
         for (Field field : fields) {
 135  0
             String attrName = getAttributeName(field);
 136  0
             String value = FieldUtil.getString(field, element);
 137  0
             if (value == null) {
 138  0
                 ComponentAttribute attr = field
 139  
                         .getAnnotation(ComponentAttribute.class);
 140  0
                 if (attr.required()) {
 141  0
                     value = NULL_STRING;
 142  
                 }
 143  
             }
 144  
 
 145  0
             if (value != null) {
 146  0
                 writer.write(' ');
 147  0
                 writer.write(attrName);
 148  0
                 writer.write("=\"");
 149  0
                 writer.write(value);
 150  0
                 writer.write('"');
 151  
             }
 152  0
         }
 153  
 
 154  0
         if (startTagOnly) {
 155  0
             writer.write(" />\n");
 156  
         } else {
 157  0
             writer.write(" >\n");
 158  
         }
 159  0
         writer.flush();
 160  0
     }
 161  
 
 162  
     protected void writeEndTag(final UIElement element) throws IOException {
 163  0
         String elementName = getElementName(element.getClass());
 164  0
         if (elementName == null) {
 165  
             // TODO Warning 出力
 166  0
             return;
 167  
         }
 168  
 
 169  0
         writeIndent();
 170  0
         writer.write("</");
 171  0
         writer.write(elementName);
 172  0
         writer.write(">\n");
 173  0
         writer.flush();
 174  0
     }
 175  
 
 176  
     /**
 177  
      * クラスオブジェクトから要素名を取得します。<br />
 178  
      * {@link ComponentElement} アノテーションを読み取り、その値を要素名として返します。<br />
 179  
      * {@link ComponentElement} アノテーションの値がない場合、クラス名のサフィックス
 180  
      * <code>Component</code> を除いた名称をデキャピタライズしたものを要素名とします。<br />
 181  
      * 【例】クラス名が ButtonComponent の場合、button が要素名になります。<br />
 182  
      * 
 183  
      * @param clazz
 184  
      *            要素名を取得する {@link Class} オブジェクト
 185  
      * @return 要素名。取得に失敗した場合は <code>null</code>。
 186  
      */
 187  
     protected String getElementName(final Class<?> clazz) {
 188  0
         ComponentElement anno = clazz.getAnnotation(ComponentElement.class);
 189  0
         if (anno == null) {
 190  0
             return null;
 191  
         }
 192  
 
 193  0
         String elementName = anno.value();
 194  0
         if (NULL_STRING.equals(elementName)) {
 195  0
             String className = clazz.getSimpleName();
 196  0
             if (className.endsWith("Component")) {
 197  0
                 elementName = className.substring(0, className.length()
 198  
                         - "Component".length());
 199  0
                 elementName = StringUtil.decapitalize(elementName);
 200  
             } else {
 201  0
                 return null;
 202  
             }
 203  
         }
 204  
 
 205  0
         return elementName;
 206  
     }
 207  
 
 208  
     protected String getAttributeName(final Field field) {
 209  0
         ComponentAttribute attr = field.getAnnotation(ComponentAttribute.class);
 210  0
         if (attr == null) {
 211  0
             return null;
 212  
         }
 213  
 
 214  0
         String attrName = attr.name();
 215  0
         if (NULL_STRING.equals(attrName)) {
 216  0
             attrName = field.getName();
 217  
         }
 218  0
         return attrName;
 219  
     }
 220  
 
 221  
     protected void writeIndent() throws IOException {
 222  0
         StringBuffer buf = new StringBuffer(INDENT.length() * indentLevel);
 223  0
         for (int i = 0; i < indentLevel; i++) {
 224  0
             buf.append(INDENT);
 225  
         }
 226  0
         writer.write(buf.toString());
 227  0
     }
 228  
 
 229  
     /**
 230  
      * インデントをリセットします。<br />
 231  
      */
 232  
     public void resetIndent() {
 233  0
         indentLevel = 0;
 234  0
     }
 235  
 
 236  
     /**
 237  
      * インデントレベルを下げます。<br />
 238  
      */
 239  
     public void indent() {
 240  0
         indentLevel++;
 241  0
     }
 242  
 
 243  
     /**
 244  
      * インデントレベルを上げます。<br />
 245  
      */
 246  
     public void deindent() {
 247  0
         indentLevel--;
 248  0
     }
 249  
 }