XmlBuilder.java

  1. package com.gh.mygreen.xlsmapper.xml;

  2. import java.lang.annotation.Annotation;

  3. import com.gh.mygreen.xlsmapper.xml.bind.AnnotationInfo;
  4. import com.gh.mygreen.xlsmapper.xml.bind.AnnotationMappingInfo;
  5. import com.gh.mygreen.xlsmapper.xml.bind.ClassInfo;
  6. import com.gh.mygreen.xlsmapper.xml.bind.FieldInfo;
  7. import com.gh.mygreen.xlsmapper.xml.bind.MethodInfo;

  8. /**
  9.  * アノテーション用のXMLのオブジェクト({@link AnnotationMappingInfo})を組み立てるためのヘルパークラス。
  10.  *
  11.  * <pre class="highlight"><code class="java">
  12.  * // static import をすると使いやすくなります。
  13.  * import static com.gh.mygreen.xlsmapper.xml.XmlBuilder.*;
  14.  *
  15.  * public void sample() {
  16.  *     AnnotationMappingInfo annotationMapping = createXml()
  17.  *               .classInfo(createClass(SimpleSheet.class)
  18.  *                       .annotation(createAnnotationBuilder(XlsSheet.class) // クラスに対するアノテーションの定義
  19.  *                               .attribute("name", "単純なシート")
  20.  *                               .buildAnnotation())
  21.  *                       .field(createField("sheetName") // フィールドに対するアノテーションの定義
  22.  *                               .annotation(createAnnotation(XlsSheetName.class)
  23.  *                                       .buildAnnotation())
  24.  *                               .buildField())
  25.  *                       .field(createField("name")
  26.  *                               .annotation(createAnnotation(XlsLabelledCell.class)
  27.  *                                       .attribute("label", "名称")
  28.  *                                       .attribute("type", LabelledCellType.Right)
  29.  *                                       .buildAnnotation())
  30.  *                               .annotation(createAnnotation(XlsTrim.class)
  31.  *                                       .buildAnnotation())
  32.  *                               .annotation(createAnnotation(XlsDefaultValue.class)
  33.  *                                       .attribute("value", "ー")
  34.  *                                       .buildAnnotation())
  35.  *                               .buildField())
  36.  *                       .method(createMethod("setRecords") // メソッドに対するアノテーションの定義
  37.  *                               .annotation(createAnnotation(XlsHorizontalRecords.class)
  38.  *                                       .attribute("tableLabel", "名簿一覧")
  39.  *                                       .attribute("terminal", RecordTerminal.Border)
  40.  *                                       .buildAnnotation())
  41.  *                               .buildMethod())
  42.  *                       .buildClass())
  43.  *               .buildXml();
  44.  *
  45.  *     // ファイルへの保存
  46.  *     XmlIO.save(annotationMapping, new File("anno_simple.xml"), "UTF-8");
  47.  *
  48.  *     // システム設定へ渡す。
  49.  *     XlsMapper xlsMapper = new XlsMapper();
  50.  *     xlsMapper.getConfiguration.setAnnotationMapping(annotaionMapping);
  51.  *
  52.  * }
  53.  * </code></pre>
  54.  * @since 1.1
  55.  * @author T.TSUCHIE
  56.  *
  57.  */
  58. public class XmlBuilder {

  59.     /**
  60.      * JavaオブジェクトをOGNL式に変換するためのクラス。
  61.      */
  62.     private static OgnlValueFormatter valueFormatter = new OgnlValueFormatter();

  63.     /**
  64.      * {@link AnnotationMappingInfo}のビルダクラスの{@link AnnotationMappingInfo.Builder}インスタンスを作成する。
  65.      * @return
  66.      */
  67.     public static AnnotationMappingInfo.Builder createXml() {
  68.         return AnnotationMappingInfo.builder();
  69.     }

  70.     /**
  71.      * {@link ClassInfo}のビルダクラスの{@link ClassInfo.Builder}インスタンスを作成する。
  72.      * @param clazz マッピング対象のJavaのクラス情報。
  73.      * @return
  74.      */
  75.     public static ClassInfo.Builder createClass(final Class<?> clazz) {
  76.         return ClassInfo.builder().name(clazz);
  77.     }

  78.     /**
  79.      * {@link MethodInfo}のビルダクラスの{@link MethodInfo.Builder}インスタンスを作成する。
  80.      * @param methodName メソッド名
  81.      * @return
  82.      */
  83.     public static MethodInfo.Builder createMethod(final String methodName) {
  84.         return MethodInfo.builder().name(methodName);
  85.     }

  86.     /**
  87.      * {@link FieldInfo}のビルダクラスの{@link FieldInfo.Builder}インスタンスを作成する。
  88.      * @param fieldName フィールド名
  89.      * @return
  90.      */
  91.     public static FieldInfo.Builder createField(final String fieldName) {
  92.         return FieldInfo.builder().name(fieldName);
  93.     }

  94.     /**
  95.      * {@link AnnotationInfo}のビルダクラスの{@link AnnotationInfo.Builder}インスタンスを作成する。
  96.      * <p>JavaオブジェクトをOGNL式に変換するクラス{@link OgnlValueFormatter}はデフォルトの物が使用される。
  97.      *    独自のものを設定したい場合は、{@link #setValueFormatter(OgnlValueFormatter)}を予め呼び変更しておく必要がある。
  98.      * @param clazz アノテーションのクラス
  99.      * @return
  100.      */
  101.     public static AnnotationInfo.Builder createAnnotation(Class<? extends Annotation> clazz) {
  102.         return AnnotationInfo.builder(valueFormatter).name(clazz);
  103.     }

  104.     /**
  105.      * JavaオブジェクトをOGNL式に変換するためのクラスを取得する。
  106.      * @return OGNL式のフォーマッタを返す。
  107.      */
  108.     public synchronized static OgnlValueFormatter getValueFormatter() {
  109.         return valueFormatter;
  110.     }

  111.     /**
  112.      * JavaオブジェクトをOGNL式に変換するためのクラスを設定する。
  113.      *
  114.      * @param valueFormatter
  115.      */
  116.     public synchronized static void setValueFormatter(final OgnlValueFormatter valueFormatter) {
  117.         XmlBuilder.valueFormatter = valueFormatter;
  118.     }

  119. }