• R/O
  • SSH
  • HTTPS

validatefa: 提交


Commit MetaInfo

修订版16 (tree)
时间2013-04-03 02:22:41
作者masasa

Log Message

初期登録

更改概述

差异

--- trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ReflectionUtil.java (nonexistent)
+++ trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ReflectionUtil.java (revision 16)
@@ -0,0 +1,120 @@
1+package jp.sourceforge.masasa.xlsXmlTemplate;
2+
3+import java.lang.reflect.InvocationTargetException;
4+import java.lang.reflect.Method;
5+import java.util.Map;
6+
7+/**
8+ * リフレクションユーティリティ.
9+ *
10+ * @author masasa
11+ */
12+public final class ReflectionUtil {
13+
14+ /**
15+ * シングルトン.
16+ */
17+ private ReflectionUtil() {
18+ // do nothing
19+ }
20+
21+ /** getter. */
22+ private static final String GET = "get"; //$NON-NLS-1$
23+ /** getter. */
24+ private static final String IS = "is"; //$NON-NLS-1$
25+
26+ /**
27+ * フィールドのGetterメソッドを返却します.
28+ *
29+ * @param obj
30+ * 対象オブジェクト.
31+ * @param name
32+ * フィールド名.
33+ * @return メソッドオブジェクト.
34+ */
35+ public static Method getGetter(Object obj, String name) {
36+ Method method = null;
37+ try {
38+ method = obj.getClass().getMethod(new StringBuilder(GET).append(name).toString(), new Class[0]);
39+ } catch (Exception e) {
40+ try {
41+ method = obj.getClass().getMethod(new StringBuilder(IS).append(name).toString(), new Class[0]);
42+ } catch (Exception e1) {
43+ method = null;
44+ }
45+ }
46+ return method;
47+ }
48+
49+ /**
50+ * {@code name} に対応する {@code getter} メソッド(getXXX またはisXXX)が {@code obj} 存在するかチェックします.
51+ *
52+ * @param obj
53+ * 対象オブジェクト.
54+ * @param name
55+ * 対象フィールド名.
56+ * @return チェック結果.
57+ * <ul>
58+ * <li>{@code true} : 存在する.</li>
59+ * <li>{@code false} : 存在しない.</li>
60+ * </ul>
61+ */
62+ public static boolean hasProperty(Object obj, String name) {
63+ return obj instanceof Map ? ((Map) obj).containsKey(name) : getGetter(obj, getCamelizedName(name)) != null;
64+ }
65+
66+ /**
67+ * {@code name} に対応する値を {@code obj} から取得して返却します.
68+ *
69+ * @param obj
70+ * 対象オブジェクト.
71+ * @param name
72+ * 対象フィールド名.
73+ * @return 取得値.
74+ * <ul>
75+ * <li>存在する場合 {@code name} に対応する {@code obj} の値を返却する.</li>
76+ * <li>上記以外の場合は {@code NULL} を返却する.</li>
77+ * </ul>
78+ */
79+ public static String getPropertyAsString(Object obj, String name) {
80+ Object value = getProperty(obj, name);
81+ return value == null ? "" : value.toString();
82+ }
83+
84+ /**
85+ * インスタンス<code>obj</code>から<code>name</code>に該当する値を返却します.
86+ *
87+ * @param obj
88+ * インスタンス.
89+ * @param name
90+ * 対象フィールド.
91+ * @return 値.
92+ */
93+ public static Object getProperty(Object obj, String name) {
94+ if (obj instanceof Map) return ((Map) obj).get(name);
95+ Method method = getGetter(obj, getCamelizedName(name));
96+ if (method == null) return null;
97+ Object value = null;
98+ try {
99+ value = method.invoke(obj, (Object[]) null);
100+ } catch (IllegalArgumentException e) {
101+ return value;
102+ } catch (IllegalAccessException e) {
103+ return value;
104+ } catch (InvocationTargetException e) {
105+ return value;
106+ }
107+ return value;
108+ }
109+
110+ /**
111+ * キャメライズした名称を返却します.
112+ *
113+ * @param name
114+ * 対象名称.
115+ * @return キャメライズ.
116+ */
117+ public static String getCamelizedName(String name) {
118+ return new StringBuilder(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
119+ }
120+}
--- trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelXMLTemplate.java (nonexistent)
+++ trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelXMLTemplate.java (revision 16)
@@ -0,0 +1,696 @@
1+package jp.sourceforge.masasa.xlsXmlTemplate;
2+
3+import java.io.File;
4+import java.io.FileInputStream;
5+import java.io.FileOutputStream;
6+import java.io.IOException;
7+import java.io.InputStream;
8+import java.io.OutputStream;
9+import java.util.ArrayList;
10+import java.util.Iterator;
11+import java.util.List;
12+import java.util.Map;
13+import java.util.Map.Entry;
14+
15+import javax.xml.parsers.DocumentBuilder;
16+import javax.xml.parsers.DocumentBuilderFactory;
17+import javax.xml.parsers.ParserConfigurationException;
18+import javax.xml.transform.TransformerException;
19+
20+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.Dom;
21+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.SpreadSheet;
22+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.Template;
23+
24+import org.w3c.dom.Document;
25+import org.w3c.dom.Element;
26+import org.w3c.dom.Node;
27+import org.xml.sax.SAXException;
28+
29+/**
30+ * スプレッド形式の {@code XML} に対してテンプレート処理を行います.</br>
31+ *
32+ * @author masasa
33+ */
34+public class ExcelXMLTemplate {
35+
36+ /** 入力テンプレートファイル. */
37+ private InputStream in;
38+ /** 出力ファイル. */
39+ private OutputStream out;
40+ /**
41+ * テンプレートするマッピングファイル.
42+ * <ul>
43+ * <li><code>key</code> : シート名.</li>
44+ * <li><code>value</code> : シートに対応するデータ.</li>
45+ * </ul>
46+ */
47+ private Map<String, Object> data;
48+ /** 入力ファイルの解析情報. */
49+ private Document doc;
50+
51+ /**
52+ * テーブルデータ.
53+ *
54+ * @author masasa
55+ */
56+ protected static class LoopInfo {
57+ /** テーブルデータ. */
58+ @SuppressWarnings("rawtypes")
59+ protected List tableData;
60+ /** 行数. */
61+ protected int verticalCount;
62+ /** #VLoopインデックス. */
63+ protected int vloopIndex;
64+ /** #VLoopプロパティ. */
65+ protected String vloopProperty;
66+
67+ /**
68+ * コンストラクタ.
69+ */
70+ @SuppressWarnings("unqualified-field-access")
71+ protected LoopInfo() {
72+ verticalCount = -1;
73+ vloopIndex = -1;
74+ vloopProperty = null;
75+ }
76+ }
77+
78+ /**
79+ * 本インスタンスを初期化します.
80+ *
81+ * @param inFile
82+ * 入力テンプレートファイル.
83+ * @param outFile
84+ * 出力ファイル.
85+ * @param mapData
86+ * テンプレートするマッピングファイル.
87+ * @throws IOException
88+ * IOException.
89+ */
90+ public ExcelXMLTemplate(File inFile, File outFile, Map mapData) throws IOException {
91+ this(new FileInputStream(inFile), new FileOutputStream(outFile), mapData);
92+ }
93+
94+ /**
95+ * 本インスタンスを初期化します.
96+ *
97+ * @param inStream
98+ * 入力テンプレートファイル.
99+ * @param outStream
100+ * 出力ファイル.
101+ * @param mapData
102+ * テンプレートするマッピングファイル.
103+ */
104+ @SuppressWarnings("unchecked")
105+ public ExcelXMLTemplate(InputStream inStream, OutputStream outStream, Map mapData) {
106+ this.doc = null;
107+ this.in = inStream;
108+ this.out = outStream;
109+ this.data = mapData;
110+ }
111+
112+ /**
113+ * テンプレート処理を実行します.
114+ *
115+ * @throws ParserConfigurationException
116+ * XML解析時例外.
117+ * @throws IOException
118+ * IO例外.
119+ * @throws SAXException
120+ * XML解析時例外.
121+ */
122+ public void execute() throws ParserConfigurationException, SAXException, IOException {
123+ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
124+ this.doc = builder.parse(this.in);
125+ Element[] aelement = ExcelDOMUtil.getElements(this.doc.getDocumentElement(), Dom.WORKSHEET);
126+ int i = 0;
127+ Element worksheet;
128+ Element pane;
129+ String sheetName;
130+ String[] dim;
131+ Object sheetData;
132+ Element[] aelement1;
133+ Element table;
134+ Element[] aelement2;
135+ Element row;
136+ Element dataValidation;
137+ Element dataHide;
138+ for (int j = aelement.length; i < j; i++) {
139+ worksheet = aelement[i];
140+ pane = ExcelDOMUtil.getElement(worksheet, new String[] { Dom.WORKSHEET_OPTIONS, Dom.PANES, Dom.PANE });
141+ if (pane != null) pane.getParentNode().removeChild(pane);
142+ sheetName = worksheet.getAttribute(SpreadSheet.NAME);
143+ if (sheetName.startsWith(Template.SHEET)) {
144+ dim = sheetName.split("\\.");
145+ if (dim.length == 3 && (ReflectionUtil.getProperty(this.data, dim[1]) instanceof List)) {
146+ processSheet(worksheet, dim);
147+ continue;
148+ }
149+ }
150+ sheetData = ReflectionUtil.getProperty(this.data, sheetName);
151+ if (sheetData == null) continue;
152+ aelement1 = ExcelDOMUtil.getElements(worksheet, Dom.TABLE);
153+ int k = 0;
154+ for (int l = aelement1.length; k < l; k++) {
155+ table = aelement1[k];
156+ aelement2 = ExcelDOMUtil.getElements(table, Dom.ROW);
157+ int j1 = 0;
158+ for (int k1 = aelement2.length; j1 < k1; j1++) {
159+ row = aelement2[j1];
160+ processRow(table, row, sheetData);
161+ }
162+
163+ }
164+ aelement1 = ExcelDOMUtil.getElements(worksheet, Dom.DATA_VALIDATION);
165+ k = 0;
166+ for (int i1 = aelement1.length; k < i1; k++) {
167+ dataValidation = aelement1[k];
168+ dataHide = ExcelDOMUtil.getElement(dataValidation, Dom.ERROR_HIDE);
169+ if (dataHide != null) dataValidation.removeChild(dataHide);
170+ }
171+ }
172+ // ExcelDOMUtil.writeXML(doc, out);
173+ return;
174+ }
175+
176+ /**
177+ * テンプレート処理を実行します.
178+ *
179+ * @throws ParserConfigurationException
180+ * XML解析時例外.
181+ * @throws IOException
182+ * IO例外.
183+ * @throws SAXException
184+ * XML解析時例外.
185+ * @throws TransformerException
186+ * トランスフォーマ設定時例外.
187+ */
188+ public void executeXML() throws ParserConfigurationException, SAXException, IOException, TransformerException {
189+ execute();
190+ ExcelDOMUtil.writeXML(this.doc, this.out);
191+ }
192+
193+ /**
194+ * テンプレート処理を実行します.
195+ *
196+ * @return バイト配列.
197+ * @throws ParserConfigurationException
198+ * XML解析時例外.
199+ * @throws IOException
200+ * IO例外.
201+ * @throws SAXException
202+ * XML解析時例外.
203+ * @throws TransformerException
204+ * トランスフォーマ設定時例外.
205+ * @deprecated {@link ExcelXLSTemplate#executeByByte(String, String, Map)}
206+ */
207+ @Deprecated
208+ public byte[] executeByByte() throws ParserConfigurationException, SAXException, IOException, TransformerException {
209+ execute();
210+ return ExcelDOMUtil.getByteXML(this.doc, this.out);
211+ // return ExcelRemoteConverter.converXmlToXLS(ExcelDOMUtil.getByteXML(doc, out));
212+ }
213+
214+ /**
215+ * @param worksheet
216+ * ワークシート.
217+ * @param dim
218+ */
219+ @SuppressWarnings({ "rawtypes", "unchecked" })
220+ protected void processSheet(Element worksheet, String[] dim) {
221+ List worksheetList = new ArrayList();
222+ List sheetDataList = (List) ReflectionUtil.getProperty(data, dim[1]);
223+ for (int i = 0; i < sheetDataList.size(); i++) {
224+ if (i == 0) {
225+ worksheetList.add(0, worksheet);
226+ } else {
227+ Element insertSheet = (Element) worksheet.cloneNode(true);
228+ worksheet.getParentNode().insertBefore(insertSheet, worksheet);
229+ worksheetList.add(i != 1 ? 1 : 0, insertSheet);
230+ }
231+ }
232+ for (int i = 0; i < worksheetList.size(); i++) {
233+ Object sheetData = sheetDataList.get(i);
234+ Element sheet = (Element) worksheetList.get(i);
235+ String sheetName = ReflectionUtil.getPropertyAsString(sheetData, dim[2]);
236+ sheet.setAttribute(SpreadSheet.NAME, sheetName);
237+ Element[] aelement = ExcelDOMUtil.getElements(sheet, Dom.NAMES);
238+ int j = 0;
239+ for (int k = aelement.length; j < k; j++) {
240+ Element names = aelement[j];
241+ Element[] aelement1 = ExcelDOMUtil.getElements(names, Dom.NAMED_RANGE);
242+ int i1 = 0;
243+ for (int k1 = aelement1.length; i1 < k1; i1++) {
244+ Element namedRange = aelement1[i1];
245+ if ("Print_Area".equals(namedRange.getAttribute(SpreadSheet.NAME))) {
246+ String refersTo = namedRange.getAttribute(SpreadSheet.REFERS_TO);
247+ refersTo = refersTo.replaceFirst((new StringBuilder("#Sheet\\.")).append(dim[1]).append("\\.")
248+ .append(dim[2]).toString(), sheetName);
249+ namedRange.setAttribute(SpreadSheet.REFERS_TO, refersTo);
250+ }
251+ }
252+ }
253+ aelement = ExcelDOMUtil.getElements(sheet, Dom.TABLE);
254+ j = 0;
255+ for (int l = aelement.length; j < l; j++) {
256+ Element table = aelement[j];
257+ Element[] aelement2 = ExcelDOMUtil.getElements(table, Dom.ROW);
258+ int j1 = 0;
259+ for (int l1 = aelement2.length; j1 < l1; j1++) {
260+ Element row = aelement2[j1];
261+ processRow(table, row, sheetData);
262+ }
263+ }
264+ }
265+ }
266+
267+ /**
268+ * 行にデータを設定します.
269+ *
270+ * @param table
271+ * テーブル.
272+ * @param row
273+ * 行.
274+ * @param rowData
275+ * データ.
276+ */
277+ protected void processRow(Element table, Element row, Object rowData) {
278+ Element[] aelement = ExcelDOMUtil.getElements(row, Dom.CELL);
279+ int i = 0;
280+ Element cell;
281+ Element[] aelement1;
282+ Element dataElement;
283+ String text;
284+ for (int j = aelement.length; i < j; i++) {
285+ cell = aelement[i];
286+ aelement1 = ExcelDOMUtil.getElements(cell, Dom.DATA);
287+ int k = 0;
288+ for (int l = aelement1.length; k < l; k++) {
289+ dataElement = aelement1[k];
290+ text = ExcelDOMUtil.getText(dataElement).trim();
291+ if (text.startsWith(ExcelXMLConstants.TEMPLATE_PLEFIX)) {
292+ if (ReflectionUtil.hasProperty(rowData, text.substring(1))) {
293+ ExcelDOMUtil
294+ .setTextToCell(cell, ReflectionUtil.getPropertyAsString(rowData, text.substring(1)));
295+ } else if (text.startsWith(Template.HLOOP)) {
296+ processHorizontalLoop(table, row, rowData, text);
297+ } else if (text.startsWith(Template.ITERATE) && text.endsWith(Template.BEGIN)) {
298+ processIterateTable(table, row, rowData, text);
299+ }
300+ }
301+ }
302+ }
303+ }
304+
305+ /**
306+ * イテレートループにデータを設定します.
307+ *
308+ * @param table
309+ * テーブル.
310+ * @param row
311+ * 行.
312+ * @param data
313+ * データ.
314+ * @param text
315+ * テキスト.
316+ */
317+ @SuppressWarnings({ "rawtypes", "unchecked" })
318+ protected void processIterateTable(Element table, Element row, Object data, String text) {
319+ String[] dim = text.split("\\.");
320+ if (dim.length == 3 && ReflectionUtil.getProperty(data, dim[1]) instanceof List
321+ && ((List) ReflectionUtil.getProperty(data, dim[1])).size() > 0) {
322+ List rows = new ArrayList();
323+ Element endRow = null;
324+ Element[] aelement1 = ExcelDOMUtil.getElements(table, Dom.ROW);
325+ Element[] aelement3;
326+ Element rowCell;
327+ Element[] aelement5;
328+ Element rowData;
329+ String dataText;
330+ int i1 = 0;
331+ for (int j1 = aelement1.length; i1 < j1; i1++) {
332+ Element element = aelement1[i1];
333+ if (element == row) {
334+ ExcelDOMUtil.clearRowData(row);
335+ rows.add(element);
336+ continue;
337+ }
338+ if (rows.size() <= 0) continue;
339+ aelement3 = ExcelDOMUtil.getElements(element, Dom.CELL);
340+ int i2 = 0;
341+ for (int j2 = aelement3.length; i2 < j2; i2++) {
342+ rowCell = aelement3[i2];
343+ aelement5 = ExcelDOMUtil.getElements(rowCell, Dom.DATA);
344+ int i3 = 0;
345+ for (int j3 = aelement5.length; i3 < j3; i3++) {
346+ rowData = aelement5[i3];
347+ dataText = ExcelDOMUtil.getText(rowData).trim();
348+ if (!dataText.equals(new StringBuilder(Template.ITERATE).append(dim[1]).append(Template.END)
349+ .toString())) continue;
350+ ExcelDOMUtil.clearRowData(element);
351+ endRow = element;
352+ break;
353+ }
354+ }
355+ rows.add(element);
356+ }
357+
358+ if (endRow != null) {
359+ List tableDatas = (List) ReflectionUtil.getProperty(data, dim[1]);
360+ List insertedRows = null;
361+ List clonedListList = new ArrayList();
362+ for (int i = 0; i < tableDatas.size(); i++) {
363+ Object tableData = tableDatas.get(i);
364+ if (i == 0) {
365+ for (int j = 0; j < tableDatas.size() - 1; j++) {
366+ List clonedList = new ArrayList();
367+ Node cloned;
368+ for (Iterator iterator1 = rows.iterator(); iterator1.hasNext(); clonedList.add(cloned)) {
369+ Element insertRow = (Element) iterator1.next();
370+ cloned = insertRow.cloneNode(true);
371+ }
372+ clonedListList.add(clonedList);
373+ }
374+ insertedRows = rows;
375+ } else {
376+ insertedRows = (List) clonedListList.get(i - 1);
377+ }
378+ Element element;
379+ for (Iterator iterator = insertedRows.iterator(); iterator.hasNext(); processRow(table, element,
380+ tableData)) {
381+ element = (Element) iterator.next();
382+ }
383+ if (i < tableDatas.size() - 1) {
384+ Element lastInsertRow = null;
385+ List clonedList = (List) clonedListList.get(i);
386+ for (Iterator iterator2 = clonedList.iterator(); iterator2.hasNext();) {
387+ Element cloned = (Element) iterator2.next();
388+ row.getParentNode().insertBefore(cloned, endRow);
389+ lastInsertRow = cloned;
390+ }
391+ ExcelDOMUtil.incrementRowCount(table, lastInsertRow, rows.size());
392+ }
393+ }
394+ }
395+ } else {
396+ Element[] aelement = ExcelDOMUtil.getElements(table, Dom.ROW);
397+ int k = 0;
398+ for (int l = aelement.length; k < l; k++) {
399+ Element element = aelement[k];
400+ Element[] aelement2 = ExcelDOMUtil.getElements(element, Dom.CELL);
401+ int k1 = 0;
402+ for (int l1 = aelement2.length; k1 < l1; k1++) {
403+ Element rowCell = aelement2[k1];
404+ Element[] aelement4 = ExcelDOMUtil.getElements(rowCell, Dom.DATA);
405+ int k2 = 0;
406+ for (int l2 = aelement4.length; k2 < l2; k2++) {
407+ Element rowData = aelement4[k2];
408+ String dataText = ExcelDOMUtil.getText(rowData).trim();
409+ if (dataText.startsWith(ExcelXMLConstants.TEMPLATE_PLEFIX)) {
410+ ExcelDOMUtil.setTextToCell(rowCell, "");
411+ }
412+ }
413+ }
414+ }
415+ }
416+ }
417+
418+ /**
419+ * 横ループにデータを設定します.
420+ *
421+ * @param table
422+ * テーブル.
423+ * @param row
424+ * 行.
425+ * @param data
426+ * データ.
427+ * @param text
428+ * テキスト.
429+ */
430+ @SuppressWarnings("unchecked")
431+ protected void processHorizontalLoop(Element table, Element row, Object data, String text) {
432+ String[] dim = text.split("\\.");
433+ if (dim.length == 3 && (ReflectionUtil.getProperty(data, dim[1]) instanceof List)) {
434+ Element[] rows = ExcelDOMUtil.getElements(table, Dom.ROW);
435+ Element beginRow = null;
436+ Element dataRow = null;
437+ Element endRow = null;
438+ Element[] aelement = rows;
439+ int j = 0;
440+ for (int k = aelement.length; j < k; j++) {
441+ Element element = aelement[j];
442+ if (element == row) {
443+ beginRow = element;
444+ continue;
445+ }
446+ if (beginRow != null && dataRow == null) {
447+ dataRow = element;
448+ continue;
449+ }
450+ if (dataRow == null) continue;
451+ endRow = element;
452+ break;
453+ }
454+
455+ if (beginRow != null && dataRow != null && endRow != null) {
456+ List dataList = (List) ReflectionUtil.getProperty(data, dim[1]);
457+ int rowCount = 0;
458+ String[] columns = extractLoopColumns(beginRow);
459+ ExcelDOMUtil.clearRowData(beginRow);
460+ ExcelDOMUtil.clearRowData(dataRow);
461+ ExcelDOMUtil.clearRowData(endRow);
462+ for (int i = 0; i < dataList.size() - 3; i++) {
463+ Node newRow = dataRow.cloneNode(true);
464+ dataRow.getParentNode().insertBefore(newRow, endRow);
465+ rowCount++;
466+ }
467+
468+ boolean flag = false;
469+ int dataIndex = 0;
470+ LoopInfo loopInfo = new LoopInfo();
471+ loopInfo.tableData = dataList;
472+ Element[] aelement1 = ExcelDOMUtil.getElements(table, Dom.ROW);
473+ int l = 0;
474+ for (int i1 = aelement1.length; l < i1; l++) {
475+ Element row1 = aelement1[l];
476+ if (row1 == beginRow) flag = true;
477+ if (flag && dataIndex < dataList.size()) {
478+ insertRowData(row1, dim[1], columns, dataList.get(dataIndex), loopInfo);
479+ dataIndex++;
480+ }
481+ if (row1 == endRow) break;
482+ }
483+ // 空回し
484+ for (int i = dataIndex + 1; i <= 3; i++) {
485+ Object obj = getEmptyObject(dataList);
486+ if (obj == null) break;
487+ Element row1 = aelement1[i];
488+ insertRowData(row1, dim[1], columns, obj, loopInfo);
489+ }
490+ if (loopInfo.verticalCount > 3) {
491+ for (int i = 0; i < 3 - dataIndex; i++) {
492+ Element row1 = null;
493+ switch (i) {
494+ case 0: // '\0'
495+ row1 = endRow;
496+ break;
497+ case 1: // '\001'
498+ row1 = dataRow;
499+ break;
500+ case 2: // '\002'
501+ row1 = beginRow;
502+ break;
503+ default:
504+ // do nothing
505+ }
506+ appendColumns(row1, ExcelDOMUtil.getElements(row1, Dom.CELL), loopInfo.vloopIndex,
507+ loopInfo.verticalCount);
508+ }
509+ }
510+ ExcelDOMUtil.incrementRowCount(table, endRow, rowCount);
511+ }
512+ }
513+ }
514+
515+ /**
516+ * 行を追加します.
517+ *
518+ * @param row
519+ * 行.
520+ * @param target
521+ * ターゲット.
522+ * @param columns
523+ * カラム.
524+ * @param data
525+ * データ.
526+ * @param loopInfo
527+ * ループ情報.
528+ */
529+ protected void insertRowData(Element row, String target, String[] columns, Object data, LoopInfo loopInfo) {
530+ Element[] elements = ExcelDOMUtil.getElements(row, Dom.CELL);
531+ String prefix;
532+ for (int i = 0; i < elements.length/* - 1 */; i++) {
533+ prefix = (new StringBuilder(Template.HLOOP)).append(target).append(".").toString();
534+ if (columns[i].startsWith(prefix)
535+ && ReflectionUtil.hasProperty(data, columns[i].substring(prefix.length()))) {
536+ ExcelDOMUtil.setTextToCell(elements[i],
537+ ReflectionUtil.getPropertyAsString(data, columns[i].substring(prefix.length())));
538+ } else {
539+ processVloop(row, elements, ExcelDOMUtil.getElements(elements[i], Dom.DATA), i, data, loopInfo);
540+ }
541+ }
542+ }
543+
544+ /**
545+ * 縦ループにデータを設定します.
546+ *
547+ * @param row
548+ * 行.
549+ * @param cells
550+ * セル.
551+ * @param datas
552+ * データ配列.
553+ * @param cellIndex
554+ * セルインデックス.
555+ * @param data
556+ * データ.
557+ * @param loopInfo
558+ * ループ情報.
559+ */
560+ @SuppressWarnings("rawtypes")
561+ protected static void processVloop(Element row, Element[] cells, Element[] datas, int cellIndex, Object data,
562+ LoopInfo loopInfo) {
563+ String text = datas.length != 1 ? "" : ExcelDOMUtil.getText(datas[0]);
564+ if (loopInfo.vloopIndex == cellIndex || text.startsWith(Template.VLOOP)) {
565+ String[] dim = text.split("\\.");
566+ if (dim.length == 3 && loopInfo.verticalCount == -1) {
567+ loopInfo.vloopIndex = cellIndex;
568+ loopInfo.vloopProperty = dim[2];
569+ loopInfo.verticalCount = ExcelDOMUtil.getVerticalLoopMaxLength(loopInfo.tableData, dim[2]);
570+ Element headerRow = ExcelDOMUtil.getHeaderRow(row);
571+ Element[] mapColumnCells = appendColumns(headerRow, ExcelDOMUtil.getElements(headerRow, Dom.CELL),
572+ loopInfo.vloopIndex, loopInfo.verticalCount);
573+ insertMapColumnData(mapColumnCells, (Map) ReflectionUtil.getProperty(data, loopInfo.vloopProperty),
574+ true);
575+ Element mergedHeaderRow = ExcelDOMUtil.getHeaderRow(headerRow);
576+ if (loopInfo.verticalCount > 3) {
577+ Element[] mergedHeaderCells = ExcelDOMUtil.getElements(mergedHeaderRow, Dom.CELL);
578+ int count = 0;
579+ Element[] aelement1 = mergedHeaderCells;
580+ int j = 0;
581+ for (int l = aelement1.length; j < l; j++) {
582+ Element cell = aelement1[j];
583+ if (count >= loopInfo.vloopIndex) {
584+ String mergeCount = cell.getAttribute(SpreadSheet.MERGE_ACROSS);
585+ if (mergeCount != null && mergeCount.length() > 0) {
586+ cell.setAttribute(SpreadSheet.MERGE_ACROSS, String.valueOf(loopInfo.verticalCount - 1));
587+ }
588+ break;
589+ }
590+ String value = cell.getAttribute(SpreadSheet.MERGE_ACROSS);
591+ if (value != null && value.length() > 0) {
592+ count += Integer.parseInt(value);
593+ }
594+ count++;
595+ }
596+ }
597+ if (loopInfo.verticalCount > 3) {
598+ Element worksheet = (Element) row.getParentNode().getParentNode();
599+ Element[] aelement = ExcelDOMUtil.getElements(worksheet, Dom.NAMES);
600+ int i = 0;
601+ for (int k = aelement.length; i < k; i++) {
602+ Element names = aelement[i];
603+ Element[] aelement2 = ExcelDOMUtil.getElements(names, Dom.NAMED_RANGE);
604+ int i1 = 0;
605+ for (int j1 = aelement2.length; i1 < j1; i1++) {
606+ Element namedRange = aelement2[i1];
607+ if ("Print_Area".equals(namedRange.getAttribute(SpreadSheet.NAME))) {
608+ ExcelDOMUtil.incrementPrintArea(namedRange, 0, loopInfo.verticalCount - 3);
609+ }
610+ }
611+ }
612+ }
613+ }
614+ Element[] mapColumnCells = appendColumns(row, cells, loopInfo.vloopIndex, loopInfo.verticalCount);
615+ insertMapColumnData(mapColumnCells, (Map) ReflectionUtil.getProperty(data, loopInfo.vloopProperty), false);
616+ }
617+ }
618+
619+ /**
620+ * @param mapColumnCells
621+ * @param data
622+ * @param isHeader
623+ */
624+ @SuppressWarnings("rawtypes")
625+ protected static void insertMapColumnData(Element[] mapColumnCells, Map data, boolean isHeader) {
626+ Object[] entries = data.entrySet().toArray();
627+ Entry entry;
628+ for (int index = 0; index < entries.length; index++) {
629+ entry = (Entry) entries[index];
630+ ExcelDOMUtil
631+ .setTextToCell(mapColumnCells[index], (isHeader ? entry.getKey() : entry.getValue()).toString());
632+ }
633+
634+ }
635+
636+ /**
637+ * @param row
638+ * @param cells
639+ * @param begin
640+ * @param append
641+ * @return
642+ */
643+ protected static Element[] appendColumns(Element row, Element[] cells, int begin, int append) {
644+ Element beginCell = cells[begin];
645+ Element dataCell = cells[begin + 1];
646+ Element endCell = cells[begin + 2];
647+ List<Element> list = new ArrayList<Element>();
648+ list.add(beginCell);
649+ list.add(dataCell);
650+ Node insertNode;
651+ for (int i = 0; i < append - 3; i++) {
652+ insertNode = dataCell.cloneNode(true);
653+ dataCell.getParentNode().insertBefore(insertNode, endCell);
654+ list.add((Element) insertNode);
655+ }
656+ list.add(endCell);
657+ return list.toArray(new Element[list.size()]);
658+ }
659+
660+ /**
661+ * @param dataRow
662+ * @return
663+ */
664+ protected static String[] extractLoopColumns(Element dataRow) {
665+ List<String> columns = new ArrayList<String>();
666+ Element[] aelement = ExcelDOMUtil.getElements(dataRow, Dom.CELL);
667+ int i = 0;
668+ Element column;
669+ Element[] elements;
670+ for (int j = aelement.length; i < j; i++) {
671+ column = aelement[i];
672+ elements = ExcelDOMUtil.getElements(column, Dom.DATA);
673+ if (elements.length > 0) {
674+ columns.add(ExcelDOMUtil.getText(elements[0]));
675+ } else {
676+ columns.add("");
677+ }
678+ }
679+ return columns.toArray(new String[columns.size()]);
680+ }
681+
682+ /**
683+ * @param list
684+ * @return
685+ */
686+ private Object getEmptyObject(List<? extends Object> list) {
687+ if (list == null || list.isEmpty()) return null;
688+ Object item = null;
689+ try {
690+ item = list.get(0).getClass().newInstance();
691+ } catch (Exception e) {
692+ // Do Nothing
693+ }
694+ return item;
695+ }
696+}
--- trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelDOMUtil.java (nonexistent)
+++ trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelDOMUtil.java (revision 16)
@@ -0,0 +1,480 @@
1+package jp.sourceforge.masasa.xlsXmlTemplate;
2+
3+import java.io.ByteArrayOutputStream;
4+import java.io.IOException;
5+import java.io.OutputStream;
6+import java.util.Iterator;
7+import java.util.List;
8+import java.util.Map;
9+
10+import javax.xml.transform.Transformer;
11+import javax.xml.transform.TransformerException;
12+import javax.xml.transform.TransformerFactory;
13+import javax.xml.transform.dom.DOMSource;
14+import javax.xml.transform.stream.StreamResult;
15+
16+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.Dom;
17+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.SpreadSheet;
18+import jp.sourceforge.masasa.xlsXmlTemplate.ExcelXMLConstants.Template;
19+
20+import org.apache.commons.lang3.StringUtils;
21+import org.w3c.dom.Document;
22+import org.w3c.dom.Element;
23+import org.w3c.dom.Node;
24+import org.w3c.dom.NodeList;
25+import org.w3c.dom.Text;
26+
27+/**
28+ * スプレッド形式の {@code XML} の {@code DOM} に関するユーティリティクラスです.</br>
29+ *
30+ * @see org.w3c.dom.Document
31+ * @see org.w3c.dom.Element
32+ * @see org.w3c.dom.Node
33+ * @see org.w3c.dom.NodeList
34+ * @see org.w3c.dom.Text
35+ * @author masasa
36+ */
37+public final class ExcelDOMUtil {
38+
39+ /**
40+ * デフォルトコンストラクタ.
41+ */
42+ private ExcelDOMUtil() {
43+ // do nothing
44+ }
45+
46+ /**
47+ * {@link Element} からテキストを取得します.
48+ *
49+ * @param element
50+ * 対象エレメント.
51+ * @return エレメント内の有効テキスト.
52+ */
53+ public static String getText(Element element) {
54+ StringBuffer sb = new StringBuffer();
55+ NodeList list = element.getChildNodes();
56+ Node node;
57+ for (int i = 0; i < list.getLength(); i++) {
58+ node = list.item(i);
59+ if (node instanceof Text) sb.append(((Text) node).getNodeValue());
60+ }
61+ return sb.toString();
62+ }
63+
64+ /**
65+ * {@link Element}から<code>elementName</code>に該当する{@link Element}を取得します.
66+ *
67+ * @param parent
68+ * 親エレメント.
69+ * @param elementName
70+ * 取得対象エレメント.
71+ * @return 子エレメント配列.
72+ */
73+ public static Element[] getElements(Element parent, String elementName) {
74+ NodeList list = parent.getElementsByTagName(elementName);
75+ Element[] elements = new Element[list.getLength()];
76+ for (int i = 0; i < list.getLength(); i++) {
77+ elements[i] = (Element) list.item(i);
78+ }
79+ return elements;
80+ }
81+
82+ /**
83+ * {@link Element}から<code>elementName</code>に該当する{@link Element}を取得します.
84+ *
85+ * @param parent
86+ * 親エレメント.
87+ * @param elementName
88+ * 取得対象エレメント.
89+ * @return 子エレメント.
90+ */
91+ public static Element getElement(Element parent, String elementName) {
92+ return getElement(parent, new String[] { elementName });
93+ }
94+
95+ /**
96+ * {@link Element}から<code>elementName</code>に該当する{@link Element}を取得します.
97+ *
98+ * @param parent
99+ * 親エレメント.
100+ * @param elementNames
101+ * 取得対象エレメント.
102+ * @return 子エレメント.
103+ */
104+ public static Element getElement(Element parent, String[] elementNames) {
105+ NodeList list;
106+ for (int i = 0; i < elementNames.length; i++) {
107+ list = parent.getElementsByTagName(elementNames[i]);
108+ if (list.getLength() != 0) parent = (Element) list.item(0);
109+ else return null;
110+ }
111+ return parent;
112+ }
113+
114+ /**
115+ * {@link Element}に文字列をセットします.
116+ *
117+ * @param parent
118+ * 対象エレメント.
119+ * @param text
120+ * 文字列.
121+ */
122+ public static void setText(Element parent, String text) {
123+ Document doc = parent.getOwnerDocument();
124+ NodeList list = parent.getChildNodes();
125+ for (int i = 0; i < list.getLength(); i++) {
126+ parent.removeChild(list.item(i));
127+ }
128+ if (!StringUtils.isEmpty(text)) {
129+ String data = text;
130+ data = data.replaceAll("\r\n", "&#10;"); //$NON-NLS-1$ //$NON-NLS-2$
131+ data = data.replaceAll("\r", "&#10;"); //$NON-NLS-1$ //$NON-NLS-2$
132+ data = data.replaceAll("\n", "&#10;"); //$NON-NLS-1$ //$NON-NLS-2$
133+ parent.appendChild(doc.createTextNode(data));
134+ }
135+ }
136+
137+ /**
138+ * {@link Document}を{@link OutputStream}に書き出します.
139+ *
140+ * @param doc
141+ * 対象ドキュメント.
142+ * @param out
143+ * ストリーム.
144+ * @throws IOException
145+ * ストリーム操作時の例外.
146+ * @throws TransformerException
147+ * 変換時の例外.
148+ */
149+ public static void writeXML(Document doc, OutputStream out) throws TransformerException, IOException {
150+ getByteXML(doc, out);
151+ }
152+
153+ /**
154+ * {@link Document}を{@link OutputStream}に書き出し、バイト配列を返却します.
155+ *
156+ * @param doc
157+ * 対象ドキュメント.
158+ * @param out
159+ * ストリーム.
160+ * @return 書き出しバイト配列.
161+ * @throws IOException
162+ * ストリーム操作時の例外.
163+ * @throws TransformerException
164+ * 変換時の例外.
165+ */
166+ public static byte[] getByteXML(Document doc, OutputStream out) throws TransformerException, IOException {
167+ TransformerFactory tff = TransformerFactory.newInstance();
168+ Transformer tf = tff.newTransformer();
169+ DOMSource src = new DOMSource();
170+ src.setNode(doc);
171+ StreamResult target = new StreamResult();
172+ ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
173+ target.setOutputStream(byteOut);
174+ tf.transform(src, target);
175+ target.getOutputStream().close();
176+ String xml = new String(byteOut.toByteArray(), ExcelXMLConstants.XML_ENCODE);
177+ xml = xml.replaceAll("&amp;#10;", "&#10;"); //$NON-NLS-1$ //$NON-NLS-2$
178+ return xml.getBytes(ExcelXMLConstants.XML_ENCODE);
179+ }
180+
181+ /**
182+ * 行を追加します.
183+ *
184+ * @param table
185+ * テーブル.
186+ * @param row
187+ * 行.
188+ * @param count
189+ * カウント.
190+ */
191+ public static void incrementRowCount(Element table, Element row, int count) {
192+ boolean flag = false;
193+ Element[] rows = getElements(table, Dom.ROW);
194+ Element[] aelement = rows;
195+ int i = 0;
196+ Element element;
197+ for (int j = aelement.length; i < j; i++) {
198+ element = aelement[i];
199+ if (element == row) {
200+ flag = true;
201+ } else if (flag && element.hasAttribute(SpreadSheet.INDEX)) {
202+ element.setAttribute(SpreadSheet.INDEX,
203+ String.valueOf(Integer.parseInt(element.getAttribute(SpreadSheet.INDEX)) + count));
204+ }
205+ }
206+ table.setAttribute(SpreadSheet.EXPANDED_ROW_COUNT,
207+ String.valueOf(Integer.parseInt(table.getAttribute(SpreadSheet.EXPANDED_ROW_COUNT)) + count));
208+ Element[] aelement1 = getElements((Element) table.getParentNode(), Dom.DATA_VALIDATION);
209+ int k = 0;
210+ Element dataValidation;
211+ Element range;
212+ Element value;
213+ for (int l = aelement1.length; k < l; k++) {
214+ dataValidation = aelement1[k];
215+ range = getElements(dataValidation, Dom.RANGE)[0];
216+ processRange(range, getRowIndex(rows, row), count);
217+ value = getElements(dataValidation, Dom.VALUE)[0];
218+ processRangeValue(value, count);
219+ }
220+
221+ aelement1 = getElements((Element) table.getParentNode(), Dom.NAMES);
222+ k = 0;
223+ Element names;
224+ Element[] aelement2;
225+ Element namedRange;
226+ for (int i1 = aelement1.length; k < i1; k++) {
227+ names = aelement1[k];
228+ aelement2 = getElements(names, Dom.NAMED_RANGE);
229+ int j1 = 0;
230+ for (int k1 = aelement2.length; j1 < k1; j1++) {
231+ namedRange = aelement2[j1];
232+ if ("Print_Area".equals(namedRange.getAttribute(SpreadSheet.NAME))) {
233+ incrementPrintArea(namedRange, count, 0);
234+ }
235+ }
236+ }
237+ }
238+
239+ /**
240+ * 行インデックスを返却します.
241+ *
242+ * @param rows
243+ * 走査行.
244+ * @param rowElement
245+ * 検索行.
246+ * @return インデックス.
247+ */
248+ private static int getRowIndex(Element[] rows, Element rowElement) {
249+ int index = 0;
250+ Element[] aelement = rows;
251+ int i = 0;
252+ Element row;
253+ for (int j = aelement.length; i < j; i++) {
254+ row = aelement[i];
255+ index++;
256+ if (row.hasAttribute(SpreadSheet.INDEX)) index = Integer.parseInt(row.getAttribute(SpreadSheet.INDEX));
257+ if (row == rowElement) break;
258+ }
259+ return index;
260+ }
261+
262+ /**
263+ * @param namedRange
264+ * @param rowCount
265+ * @param columnCount
266+ */
267+ public static void incrementPrintArea(Element namedRange, int rowCount, int columnCount) {
268+ StringBuilder sb = new StringBuilder();
269+ String[] splitted = namedRange.getAttribute(SpreadSheet.REFERS_TO).split("!");
270+ sb.append(splitted[0]).append("!");
271+ String[] xyValues = splitted[1].split(":");
272+ String[] dim;
273+ String newValue;
274+ for (int i = 0; i < xyValues.length; i++) {
275+ if (i != 0) {
276+ sb.append(":");
277+ dim = xyValues[i].split("C");
278+ int row = Integer.parseInt(dim[0].substring(1)) + rowCount;
279+ int column = Integer.parseInt(dim[1]) + columnCount;
280+ newValue = (new StringBuilder("R")).append(row).append("C").append(column).toString();
281+ sb.append(newValue);
282+ } else {
283+ sb.append(xyValues[i]);
284+ }
285+ }
286+
287+ namedRange.setAttribute(SpreadSheet.REFERS_TO, sb.toString());
288+ }
289+
290+ /**
291+ * @param rangeElement
292+ * @param insertRow
293+ * @param count
294+ */
295+ private static void processRange(Element rangeElement, int insertRow, int count) {
296+ StringBuilder sb = new StringBuilder();
297+ String[] as = getText(rangeElement).split(",");
298+ int j = 0;
299+ String rangeValue;
300+ StringBuilder builder;
301+ String[] xyValues;
302+ String[] dim;
303+ for (int k = as.length; j < k; j++) {
304+ rangeValue = as[j];
305+ if (rangeValue.length() != 0) {
306+ if (sb.length() > 0) {
307+ sb.append(",");
308+ }
309+ if (rangeValue.indexOf(':') == -1) {
310+ builder = new StringBuilder();
311+ builder.append(rangeValue);
312+ builder.append(":");
313+ builder.append(rangeValue);
314+ rangeValue = builder.toString();
315+ builder = null;
316+ }
317+ xyValues = rangeValue.split(":");
318+ for (int i = 0; i < xyValues.length; i++) {
319+ if (i != 0) {
320+ sb.append(":");
321+ }
322+ dim = xyValues[i].split("C");
323+ int row = Integer.parseInt(dim[0].substring(1));
324+ if (i == 0 && row > insertRow - count) {
325+ row += count;
326+ } else if (i == 1 && row >= insertRow - count) {
327+ row += count;
328+ }
329+ sb.append(new StringBuilder("R").append(row).append("C").append(dim[1]).toString());
330+ }
331+ }
332+ }
333+ setText(rangeElement, sb.toString());
334+ }
335+
336+ /**
337+ * @param rangeElement
338+ * @param count
339+ */
340+ private static void processRangeValue(Element rangeElement, int count) {
341+ StringBuilder sb = new StringBuilder();
342+ String[] as = getText(rangeElement).split(",");
343+ int j = 0;
344+ String rangeValue;
345+ String[] xyValues;
346+ String[] dim;
347+ for (int k = as.length; j < k; j++) {
348+ rangeValue = as[j];
349+ if (rangeValue.length() != 0) {
350+ if (sb.length() > 0) {
351+ sb.append(",");
352+ }
353+ xyValues = rangeValue.split(":");
354+ for (int i = 0; i < xyValues.length; i++) {
355+ if (i != 0) {
356+ sb.append(":");
357+ }
358+ dim = xyValues[i].split("C");
359+ if (!dim[0].substring(1).matches("[\\d]*")) {
360+ sb.append(rangeValue);
361+ } else {
362+ int row = Integer.parseInt(dim[0].substring(1)) + count;
363+ sb.append(new StringBuilder("R").append(row).append("C").append(dim[1]).toString());
364+ }
365+ }
366+ }
367+ }
368+ setText(rangeElement, sb.toString());
369+ }
370+
371+ /**
372+ * @param row
373+ */
374+ public static void clearRowData(Element row) {
375+ Element[] aelement = getElements(row, Dom.CELL);
376+ int i = 0;
377+ Element column;
378+ Element[] aelement1;
379+ Element data;
380+ String text;
381+ for (int j = aelement.length; i < j; i++) {
382+ column = aelement[i];
383+ aelement1 = getElements(column, Dom.DATA);
384+ int k = 0;
385+ for (int l = aelement1.length; k < l; k++) {
386+ data = aelement1[k];
387+ text = getText(data);
388+ if (!text.startsWith(Template.VLOOP)) {
389+ setText(data, "");
390+ }
391+ }
392+
393+ }
394+
395+ }
396+
397+ /**
398+ * @param rowDatas
399+ * @param target
400+ * @return
401+ */
402+ @SuppressWarnings("unchecked")
403+ public static int getVerticalLoopMaxLength(List rowDatas, String target) {
404+ int count = 0;
405+ Object tableData;
406+ Map map;
407+ for (Iterator iterator = rowDatas.iterator(); iterator.hasNext();) {
408+ tableData = iterator.next();
409+ map = (Map) ReflectionUtil.getProperty(tableData, target);
410+ if (map != null && count < map.size()) {
411+ count = map.size();
412+ }
413+ }
414+ return count;
415+ }
416+
417+ /**
418+ * @param cell
419+ * @param text
420+ */
421+ public static void setTextToCell(Element cell, String text) {
422+ Element[] datas = getElements(cell, Dom.DATA);
423+ if (text.indexOf('\n') >= 0 || text.indexOf('\r') >= 0) {
424+ String styleId = cell.getAttribute(SpreadSheet.STYLE_ID);
425+ if (styleId != null && styleId.length() != 0) {
426+ Element workbook = (Element) cell.getParentNode().getParentNode().getParentNode().getParentNode();
427+ Element styles = getElement(workbook, Dom.STYLES);
428+ Element[] aelement = getElements(styles, Dom.STYLE);
429+ int i = 0;
430+ Element style;
431+ for (int j = aelement.length; i < j; i++) {
432+ style = aelement[i];
433+ if (!style.getAttribute(SpreadSheet.ID).equals(styleId)) {
434+ continue;
435+ }
436+ getElement(style, Dom.ALIGNMENT).setAttribute(SpreadSheet.WRAP_TEXT, "1");
437+ break;
438+ }
439+ }
440+ }
441+ text = text.trim();
442+ if (datas.length == 0) {
443+ Element dataNode = cell.getOwnerDocument().createElement(Dom.DATA);
444+ dataNode.setAttribute(SpreadSheet.TYPE, Dom.STRING);
445+ setText(dataNode, text);
446+ cell.appendChild(dataNode);
447+ // System.out.println("××× " + text + " : " + dataNode.getAttribute(SpreadSheet.TYPE));
448+ } else {
449+ if (StringUtils.isEmpty(text)) {
450+ datas[0].setAttribute(SpreadSheet.TYPE, Dom.STRING);
451+ } else if (!isNumber(text)) {
452+ datas[0].setAttribute(SpreadSheet.TYPE, Dom.STRING);
453+ }
454+ setText(datas[0], text);
455+ // System.out.println("○○○ " + text + " : " + datas[0].getAttribute(SpreadSheet.TYPE));
456+ }
457+ }
458+
459+ /**
460+ * @param beginRow
461+ * @return
462+ */
463+ public static Element getHeaderRow(Element beginRow) {
464+ Element table = (Element) beginRow.getParentNode();
465+ Element[] rows = getElements(table, Dom.ROW);
466+ for (int i = 0; i < rows.length; i++) {
467+ if (rows[i] == beginRow) return rows[i - 1];
468+ }
469+ return null;
470+ }
471+
472+ /**
473+ * @param text
474+ * @return
475+ */
476+ private static boolean isNumber(String text) {
477+ return text == null || text.trim().length() == 0 ? false : text.trim()
478+ .replaceAll(",", "").matches("^[-]?[0-9]*[.]?[0-9]*"); //$NON-NLS-1$
479+ }
480+}
--- trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelXMLConstants.java (nonexistent)
+++ trunk/jp.sourceforge.masasa.xlsXmlTemplate/src/jp/sourceforge/masasa/xlsXmlTemplate/ExcelXMLConstants.java (revision 16)
@@ -0,0 +1,193 @@
1+package jp.sourceforge.masasa.xlsXmlTemplate;
2+
3+/**
4+ * ExcelXmlテンプレート定数クラス.
5+ *
6+ * @author masasa
7+ */
8+public final class ExcelXMLConstants {
9+
10+ /**
11+ * シングルトン.
12+ */
13+ private ExcelXMLConstants() {
14+ // do nothing
15+ }
16+
17+ /**
18+ * Activex.
19+ *
20+ * @author masasa
21+ */
22+ public static final class Activex {
23+
24+ /**
25+ * シングルトン.
26+ */
27+ private Activex() {
28+ // do nothing
29+ }
30+
31+ /** Excel.Application. */
32+ public static final String ACTIVEX_COMMAND_EXCEL_APPLICATION = "Excel.Application"; //$NON-NLS-1$
33+ /** Workbooks. */
34+ public static final String ACTIVEX_COMMAND_EXCEL_WORKBOOKS = "Workbooks"; //$NON-NLS-1$
35+ }
36+
37+ /**
38+ * Dom.
39+ *
40+ * @author masasa
41+ */
42+ public static final class Dom {
43+
44+ /**
45+ * シングルトン.
46+ */
47+ private Dom() {
48+ // do nothing
49+ }
50+
51+ /** . */
52+ public static final String ALIGNMENT = "Alignment"; //$NON-NLS-1$
53+ /** . */
54+ public static final String CELL = "Cell"; //$NON-NLS-1$
55+ /** . */
56+ public static final String DATA = "Data"; //$NON-NLS-1$
57+ /** . */
58+ public static final String DATA_VALIDATION = "DataValidation"; //$NON-NLS-1$
59+ /** . */
60+ public static final String ERROR_HIDE = "ErrorHide"; //$NON-NLS-1$
61+ /** . */
62+ public static final String NAMED_RANGE = "NamedRange"; //$NON-NLS-1$
63+ /** . */
64+ public static final String NAMES = "Names"; //$NON-NLS-1$
65+ /** . */
66+ public static final String PANE = "Pane"; //$NON-NLS-1$
67+ /** . */
68+ public static final String PANES = "Panes"; //$NON-NLS-1$
69+ /** . */
70+ public static final String ROW = "Row"; //$NON-NLS-1$
71+ /** . */
72+ public static final String STRING = "String"; //$NON-NLS-1$
73+ /** . */
74+ public static final String NUMBER = "Number"; //$NON-NLS-1$
75+ /** . */
76+ public static final String TABLE = "Table"; //$NON-NLS-1$
77+ /** . */
78+ public static final String WORKSHEET = "Worksheet"; //$NON-NLS-1$
79+ /** . */
80+ public static final String WORKSHEET_OPTIONS = "WorksheetOptions"; //$NON-NLS-1$
81+ /** . */
82+ public static final String RANGE = "Range"; //$NON-NLS-1$
83+ /** . */
84+ public static final String VALUE = "Value"; //$NON-NLS-1$
85+ /** . */
86+ public static final String STYLE = "Style"; //$NON-NLS-1$
87+ /** . */
88+ public static final String STYLES = "Styles"; //$NON-NLS-1$
89+ }
90+
91+ /**
92+ * Jacob.
93+ *
94+ * @author masasa
95+ */
96+ public static final class Jacob {
97+
98+ /**
99+ * シングルトン.
100+ */
101+ private Jacob() {
102+ // do nothing
103+ }
104+
105+ /** . */
106+ public static final String JACOB_COMMAND_CLOSE = "Close"; //$NON-NLS-1$
107+ /** . */
108+ public static final String JACOB_COMMAND_OPEN = "Open"; //$NON-NLS-1$
109+ /** . */
110+ public static final String JACOB_COMMAND_OPEN_XML = "OpenXML"; //$NON-NLS-1$
111+
112+ /** . */
113+ public static final String JACOB_COMMAND_QUIT = "Quit"; //$NON-NLS-1$
114+
115+ /** . */
116+ public static final String JACOB_COMMAND_SAVE_AS = "SaveAs"; //$NON-NLS-1$
117+ }
118+
119+ /**
120+ * SpreadSheet.
121+ *
122+ * @author masasa
123+ */
124+ public static final class SpreadSheet {
125+
126+ /**
127+ * シングルトン.
128+ */
129+ private SpreadSheet() {
130+ // do nothing
131+ }
132+
133+ /** . */
134+ public static final String EXPANDED_ROW_COUNT = "ss:ExpandedRowCount"; //$NON-NLS-1$
135+ /** . */
136+ public static final String ID = "ss:ID"; //$NON-NLS-1$
137+ /** . */
138+ public static final String INDEX = "ss:Index"; //$NON-NLS-1$
139+ /** . */
140+ public static final String MERGE_ACROSS = "ss:MergeAcross"; //$NON-NLS-1$
141+ /** . */
142+ public static final String NAME = "ss:Name"; //$NON-NLS-1$
143+ /** . */
144+ public static final String REFERS_TO = "ss:RefersTo"; //$NON-NLS-1$
145+ /** . */
146+ public static final String STYLE_ID = "ss:StyleID"; //$NON-NLS-1$
147+ /** . */
148+ public static final String TYPE = "ss:Type"; //$NON-NLS-1$
149+ /** . */
150+ public static final String WRAP_TEXT = "ss:WrapText"; //$NON-NLS-1$
151+ }
152+
153+ /**
154+ * Template.
155+ *
156+ * @author masasa
157+ */
158+ public static final class Template {
159+
160+ /**
161+ * シングルトン.
162+ */
163+ private Template() {
164+ // do nothing
165+ }
166+
167+ /** . */
168+ public static final String BEGIN = TEMPLATE_SEPARATOR.concat("Begin"); //$NON-NLS-1$
169+ /** . */
170+ public static final String END = TEMPLATE_SEPARATOR.concat("End"); //$NON-NLS-1$
171+ /** . */
172+ public static final String HLOOP = TEMPLATE_PLEFIX.concat("Hloop") //$NON-NLS-1$
173+ .concat(TEMPLATE_SEPARATOR);
174+ /** . */
175+ public static final String ITERATE = TEMPLATE_PLEFIX.concat("Iterate") //$NON-NLS-1$
176+ .concat(TEMPLATE_SEPARATOR);
177+ /** . */
178+ public static final String SHEET = TEMPLATE_PLEFIX.concat("Sheet") //$NON-NLS-1$
179+ .concat(TEMPLATE_SEPARATOR);
180+ /** . */
181+ public static final String VLOOP = TEMPLATE_PLEFIX.concat("Vloop") //$NON-NLS-1$
182+ .concat(TEMPLATE_SEPARATOR);
183+
184+ }
185+
186+ /** . */
187+ public static final String TEMPLATE_PLEFIX = "#"; //$NON-NLS-1$
188+ /** . */
189+ public static final String TEMPLATE_SEPARATOR = "."; //$NON-NLS-1$
190+ /** . */
191+ public static final String XML_ENCODE = "UTF-8"; //$NON-NLS-1$
192+
193+}
Show on old repository browser