「付加行」機能の実装中。
authorsatomichan <git-miya.20210624@...>
Fri, 25 Jun 2021 13:41:34 +0000 (22:41 +0900)
committersatomichan <git-miya.20210624@...>
Fri, 25 Jun 2021 13:41:34 +0000 (22:41 +0900)
いままで「PFCバランス計算」&「食品群別摂取量集計」機能をベタな Java のコードで書いていたが、
これを外部ファイル(XML)で記述できるようにする。

とりあえず「PFCバランス計算」は置き換えた(当該 Java コードは削除した)。

nucalgen/bright-colored-vegetables.xml [new file with mode: 0644]
nucalgen/pfc-balance.xml [new file with mode: 0644]
nucalgen/pom.xml
nucalgen/src/main/java/jp/satomichan/nucalgen/Nucalgen.java
nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCell.java [new file with mode: 0644]
nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCellType.java [new file with mode: 0644]
nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcRow.java [new file with mode: 0644]
nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionConfig.java [new file with mode: 0644]
nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionUtil.java [new file with mode: 0644]

diff --git a/nucalgen/bright-colored-vegetables.xml b/nucalgen/bright-colored-vegetables.xml
new file mode 100644 (file)
index 0000000..6bbf9d4
--- /dev/null
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<root>
+       <bright-colored-vegetable>
+               <name>あさつき</name>
+               <name>あしたば</name>
+               <name>アスパラガス</name>
+               <name>いんげんまめ</name>
+               <name>さやいんげん</name>
+               <name>エンダイブ</name>
+               <name>トウミョウ</name>
+               <name>おおさかしろな</name>
+               <name>さやえんどう</name>
+               <name>おかひじき</name>
+               <name>オクラ</name>
+               <name>かぶ 葉</name>
+               <name>日本かぼちゃ</name>
+               <name>西洋かぼちゃ</name>
+               <name>からしな</name>
+               <name>ぎょうじゃにんにく</name>
+               <name>みずな</name>
+               <name>キンサイ</name>
+               <name>クレソン</name>
+               <name>ケール</name>
+               <name>こごみ</name>
+               <name>こまつな</name>
+               <name>さんとうさい</name>
+               <name>ししとう</name>
+               <name>しそ </name>
+               <name>じゅうろくささげ</name>
+               <name>しゅんぎく</name>
+               <name>すぐきな 葉</name>
+               <name>せり</name>
+               <name>タアサイ</name>
+               <name>かいわれだいこん</name>
+               <name>葉だいこん</name>
+               <name>だいこん 葉</name>
+               <name>つまみな</name>
+               <name>たいさい</name>
+               <name>たかな</name>
+               <name>たらのめ</name>
+               <name>チンゲンサイ</name>
+               <name>つくし</name>
+               <name>つるな</name>
+               <name>つるむらさき</name>
+               <name>とうがらし</name>
+               <name>トマト</name>
+               <name>ミニトマト</name>
+               <name>とんぶり</name>
+               <name>ながさきはくさい</name>
+               <name>なずな</name>
+               <name>和種なばな</name>
+               <name>洋種なばな</name>
+               <name>にら</name>
+               <name>花にら</name>
+               <name>葉にんじん</name>
+               <name>にんじん</name>
+               <name>きんとき</name>
+               <name>ミニキャロット</name>
+               <name>茎にんにく</name>
+               <name>葉ねぎ</name>
+               <name>こねぎ</name>
+               <name>のざわな</name>
+               <name>のびる</name>
+               <name>パクチョイ</name>
+               <name>バジル</name>
+               <name>パセリ</name>
+               <name>青ピーマン</name>
+               <name>赤ピーマン</name>
+               <name>トマピー</name>
+               <name>ひのな</name>
+               <name>ひろしまな</name>
+               <name>ふだんそう</name>
+               <name>ブロッコリー</name>
+               <name>ほうれんそう</name>
+               <name>みずかけな</name>
+               <name>切りみつば</name>
+               <name>根みつば</name>
+               <name>糸みつば</name>
+               <name>めキャベツ</name>
+               <name>めたで</name>
+               <name>モロヘイヤ</name>
+               <name>ようさい</name>
+               <name>よめな</name>
+               <name>よもぎ</name>
+               <name>サラダな</name>
+               <name>リーフレタス</name>
+               <name>サニーレタス</name>
+               <name>レタス 水耕栽培</name>
+               <name>サンチュ</name>
+               <name>ルッコラ</name>
+               <name>わけぎ</name>
+               <name>葉たまねぎ</name>
+               <name>みぶな</name>
+       </bright-colored-vegetable>
+</root>
diff --git a/nucalgen/pfc-balance.xml b/nucalgen/pfc-balance.xml
new file mode 100644 (file)
index 0000000..5ed29f0
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<addition>
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value>PFCバランス (%) 七訂の方法で計算したエネルギー量で計算</value></cell>
+       </row>
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value>P</value></cell>
+               <cell><type>string</type><value>F</value></cell>
+               <cell><type>string</type><value>C</value></cell>
+       </row>
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>formula</type><value>ROUND(SUM_P*4*100/SUM_KCAL,0)</value><alias>PFC_BALANCE_P_7</alias></cell>
+               <cell><type>formula</type><value>ROUND(SUM_F*9*100/SUM_KCAL,0)</value><alias>PFC_BALANCE_F_7</alias></cell>
+               <cell><type>formula</type><value>100-(PFC_BALANCE_P_7+PFC_BALANCE_F_7)</value></cell>
+       </row>
+
+       <row></row>
+
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value>PFCバランス (%) 八訂のエネルギー量で計算</value></cell>
+       </row>
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value>P</value></cell>
+               <cell><type>string</type><value>F</value></cell>
+               <cell><type>string</type><value>C</value></cell>
+       </row>
+       <row>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>string</type><value></value></cell>
+               <cell><type>formula</type><value>ROUND(SUM_P_ENG*4*100/SUM_KCAL,0)</value><alias>PFC_BALANCE_P_8</alias></cell>
+               <cell><type>formula</type><value>ROUND(SUM_F_ENG*9*100/SUM_KCAL,0)</value><alias>PFC_BALANCE_F_8</alias></cell>
+               <cell><type>formula</type><value>100-(PFC_BALANCE_P_8+PFC_BALANCE_F_8)</value></cell>
+       </row>
+
+
+</addition>
index 7a3637517a3f832248f78f75a39b30cf2fc216d5..7bf56ffc618f44a35b731d50df1032b12f62bf6c 100644 (file)
                        <version>1.4</version>
                </dependency>
 
+               <!-- https://mvnrepository.com/artifact/commons-digester/commons-digester -->
+               <dependency>
+                       <groupId>commons-digester</groupId>
+                       <artifactId>commons-digester</artifactId>
+                       <version>2.1</version>
+               </dependency>
+
+
 
        </dependencies>
 </project>
\ No newline at end of file
index ba760ccef079074e2b5eb751194d9d078fde8dd3..b6065c9262bb8df7a7656526906b157076dfbe3a 100644 (file)
@@ -1,5 +1,6 @@
 package jp.satomichan.nucalgen;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.util.ArrayList;
@@ -7,6 +8,8 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.regex.Matcher;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
@@ -21,6 +24,12 @@ import org.apache.poi.ss.usermodel.WorkbookFactory;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 
+import jp.satomichan.nucalgen.addition.AcCell;
+import jp.satomichan.nucalgen.addition.AcCellType;
+import jp.satomichan.nucalgen.addition.AcRow;
+import jp.satomichan.nucalgen.addition.AdditionConfig;
+import jp.satomichan.nucalgen.addition.AdditionUtil;
+
 public class Nucalgen {
        private static final int COL_INDEX_START = 4;
 
@@ -31,12 +40,12 @@ public class Nucalgen {
                options.addOption(Option.builder("c").required().hasArg().longOpt("columns").build());
                options.addOption(Option.builder("o").required().hasArg().longOpt("output").build());
                options.addOption(Option.builder("l").required().hasArg().longOpt("lines").build());
-               options.addOption(Option.builder("p").longOpt("use-processed-table").build());
-               options.addOption(Option.builder("pfc").longOpt("with-pfc-balance").build());
+               options.addOption(Option.builder("add").hasArgs().longOpt("addition").build());
+
                options.addOption(Option.builder("groupsum").longOpt("with-group-sum").build());
                options.addOption(Option.builder("bright").hasArg().longOpt("bright-colored-vegetables-list").build());
                options.addOption(Option.builder("protect").longOpt("set-protect").build());
-               options.addOption(Option.builder("r").longOpt("-use-processed-table").build());
+               options.addOption(Option.builder("processed").longOpt("use-processed-table").build());
 
                Map<String, String> namedAreaMap = new HashMap<String, String>();
 
@@ -118,9 +127,7 @@ public class Nucalgen {
                                                        String cell = new CellReference(rowIndex, 4 + nch.indexOf(aAlias)).formatAsString();
                                                        formula = formula.replaceAll("([^A-Za-z0-9_])" + aAlias + "([^A-Za-z0-9_])", "$1" + cell + "$2");
                                                }
-                                               //System.out.println(formula);
                                                thisCell.setCellFormula(formula);
-                                               //thisCell.setCellValue(formula);
 
 
                                        } else {
@@ -168,13 +175,14 @@ public class Nucalgen {
                                colIndex++;
                        }
 
-
-                       //「PFCバランス」出力
-                       if(cmd.hasOption("with-pfc-balance")) {
-                               rowIndex += 3;
-                               rowIndex = generatePfcBalance(calcSheet, csPool, rowIndex, namedAreaMap);
-                               rowIndex += 1;
-                               rowIndex = generatePfcBalance8(calcSheet, csPool, rowIndex, namedAreaMap);
+                       
+                       //「付加行」出力
+                       String[] additionOptionValues = cmd.getOptionValues("addition");
+                       if(additionOptionValues != null) {
+                               for(String aAdditionFileName : additionOptionValues) {
+                                       rowIndex += 3;
+                                       rowIndex = generateAddition(aAdditionFileName, calcSheet, csPool, rowIndex, namedAreaMap);
+                               }
                        }
 
                        //「食品群別摂取量」出力
@@ -213,67 +221,6 @@ public class Nucalgen {
        }
 
 
-
-       //PFCバランス
-       private static int generatePfcBalance(Sheet calcSheet, CellStylePool csPool, int rowIndex, Map<String,String> _namedAreaMap) {
-               Row pfbBalanceRow1 = calcSheet.createRow(rowIndex);
-               pfbBalanceRow1.createCell(1).setCellValue("PFCバランス (%) 七訂の方法で計算したエネルギー量で計算");
-               calcSheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 2));
-               pfbBalanceRow1.createCell(3).setCellValue("P");
-               pfbBalanceRow1.createCell(4).setCellValue("F");
-               pfbBalanceRow1.createCell(5).setCellValue("C");
-
-               final String sumKiloCalorieCell = _namedAreaMap.get("SUM_KCAL");
-
-               rowIndex++;
-               Row pfbBalanceRow2 = calcSheet.createRow(rowIndex);
-               Cell pCell = pfbBalanceRow2.createCell(3);
-               pCell.setCellStyle(csPool.getCellStyle("0"));
-               pCell.setCellFormula("ROUND(" + _namedAreaMap.get("SUM_P") + "*4*100/" + sumKiloCalorieCell + ",0)");
-
-               Cell fCell = pfbBalanceRow2.createCell(4);
-               fCell.setCellStyle(csPool.getCellStyle("0"));
-               fCell.setCellFormula("ROUND(" + _namedAreaMap.get("SUM_F") + "*9*100/" + sumKiloCalorieCell + ",0)");
-
-               Cell cCell = pfbBalanceRow2.createCell(5);
-               cCell.setCellStyle(csPool.getCellStyle("0"));
-               cCell.setCellFormula("100 - (" + new CellReference(cCell.getRowIndex(), 3).formatAsString() + " + "
-                                             + new CellReference(cCell.getRowIndex(), 4).formatAsString() + ")");
-
-               return rowIndex;
-       }
-
-
-       //PFCバランス(八訂)
-       private static int generatePfcBalance8(Sheet calcSheet, CellStylePool csPool, int rowIndex, Map<String,String> _namedAreaMap) {
-               Row pfbBalanceRow1 = calcSheet.createRow(rowIndex);
-               pfbBalanceRow1.createCell(1).setCellValue("PFCバランス (%) 八訂のエネルギー量で計算");
-               calcSheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 2));
-               pfbBalanceRow1.createCell(3).setCellValue("P");
-               pfbBalanceRow1.createCell(4).setCellValue("F");
-               pfbBalanceRow1.createCell(5).setCellValue("C");
-
-               final String sumKiloCalorieCell = _namedAreaMap.get("SUM_KCAL");
-
-               rowIndex++;
-               Row pfbBalanceRow2 = calcSheet.createRow(rowIndex);
-               Cell pCell = pfbBalanceRow2.createCell(3);
-               pCell.setCellStyle(csPool.getCellStyle("0"));
-               pCell.setCellFormula("ROUND(" + _namedAreaMap.get("SUM_P_ENG") + "*4*100/" + sumKiloCalorieCell + ",0)");
-
-               Cell fCell = pfbBalanceRow2.createCell(4);
-               fCell.setCellStyle(csPool.getCellStyle("0"));
-               fCell.setCellFormula("ROUND(" + _namedAreaMap.get("SUM_F_ENG") + "*9*100/" + sumKiloCalorieCell + ",0)");
-
-               Cell cCell = pfbBalanceRow2.createCell(5);
-               cCell.setCellStyle(csPool.getCellStyle("0"));
-               cCell.setCellFormula("100 - (" + new CellReference(cCell.getRowIndex(), 3).formatAsString() + " + "
-                                             + new CellReference(cCell.getRowIndex(), 4).formatAsString() + ")");
-
-               return rowIndex;
-       }
-
-
         //群別摂取量
        private static int generateGroupSum(Sheet calcSheet, CellStylePool csPool, int rowIndex, Map<String,String> _namedAreaMap) {
 
@@ -314,7 +261,51 @@ public class Nucalgen {
        }
 
 
+       //「付加行」生成
+       private static int generateAddition(String fileName, Sheet calcSheet, CellStylePool csPool, int rowIndex,
+                                                                                                                                                               Map<String,String> _namedAreaMap) {
+
+               System.out.println("ADDITION : " + fileName);
 
+               AdditionConfig ac = AdditionUtil.additionFileReader(new File(fileName));
+               for(AcRow acRow : ac.getRows()) { //行ごとのループ
+                       Row thisRow = calcSheet.createRow(rowIndex);
+                       int colIndex = 0;
+                       
+                       for(AcCell acCell : acRow.getCells()) { //セルごとのループ
+                               Cell thisCell = thisRow.createCell(colIndex);
+                               
+                               //alias 「付加行」内の別名定義(制約:右方・下方のセルからしか参照できない)
+                               if(acCell.getAlias() != null) {
+                                       _namedAreaMap.put(acCell.getAlias(), new CellReference(rowIndex, colIndex, true, true).formatAsString());
+                               }
+                               
+                               if(acCell.getCellType() == AcCellType.FORMULA) {
+                                       String formula = "<" + acCell.getValue() + ">";
+                                       for(Entry<String, String> keyValue : _namedAreaMap.entrySet()) {
+                                               String k = keyValue.getKey();
+                                               String v = keyValue.getValue();
+                                               v = Matcher.quoteReplacement(v);
+                                               formula = formula.replaceAll("([^A-Za-z0-9_])" + k + "([^A-Za-z0-9_])", "$1" + v + "$2");
+                                               formula = formula.replaceAll("^<", "");
+                                               formula = formula.replaceAll(">$", "");
+                                       }
+                                       thisCell.setCellFormula(formula);
+                               } else {
+                                       thisCell.setCellValue(acCell.getValue());
+                               }
+                       
+                               
+                               colIndex++;
+                       } //セルごとのループ
+                       
+                       rowIndex++;
+               } //行ごとのループ
+               
+
+
+               return rowIndex;
+       }
 
 
 
diff --git a/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCell.java b/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCell.java
new file mode 100644 (file)
index 0000000..b2d881d
--- /dev/null
@@ -0,0 +1,49 @@
+package jp.satomichan.nucalgen.addition;
+
+public class AcCell {
+       private String value;
+       private AcCellType type;
+       private String alias;
+       
+       public AcCell(){
+               //System.out.println("Cell#Cell()");
+       }
+       
+       public String getValue() {
+               return value;
+       }
+       
+       public void setValue(String value) {
+               this.value = value;
+       }
+       
+       
+       public AcCellType getCellType() {
+               return type;
+       }
+       
+       public String getType() {
+               return type.getId();
+       }
+       
+       public void setType(String type) {
+               AcCellType[] types = AcCellType.values();
+               for(AcCellType aType : types) {
+                       if(aType.getId().equals(type)){
+                               this.setType(aType);
+                       }
+               }
+       }
+       
+       private void setType(AcCellType type) {
+               this.type = type;
+       }
+
+       public String getAlias() {
+               return alias;
+       }
+
+       public void setAlias(String alias) {
+               this.alias = alias;
+       }
+}
diff --git a/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCellType.java b/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcCellType.java
new file mode 100644 (file)
index 0000000..a3074e2
--- /dev/null
@@ -0,0 +1,17 @@
+package jp.satomichan.nucalgen.addition;
+
+public enum AcCellType {
+       FORMULA("formula"),
+       STRING("string");
+
+       private String id;
+
+
+       private AcCellType(String  id) {
+               this.id = id;
+       }
+
+       public String getId() {
+               return id;
+       }
+}
diff --git a/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcRow.java b/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AcRow.java
new file mode 100644 (file)
index 0000000..5fd994f
--- /dev/null
@@ -0,0 +1,24 @@
+package jp.satomichan.nucalgen.addition;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AcRow {
+       private List<AcCell> cells = new ArrayList<AcCell>();
+
+       public List<AcCell> getCells() {
+               return cells;
+       }
+
+       public void setCells(List<AcCell> cells) {
+               this.cells = cells;
+       }
+       
+       
+       public void addCell(AcCell cell) {
+               this.cells.add(cell);
+               //System.out.println("Row#addCell()");
+       }
+               
+       
+}
diff --git a/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionConfig.java b/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionConfig.java
new file mode 100644 (file)
index 0000000..cc03b8e
--- /dev/null
@@ -0,0 +1,26 @@
+package jp.satomichan.nucalgen.addition;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AdditionConfig {
+       private List<AcRow> rows = new ArrayList<AcRow>();
+
+       public List<AcRow> getRows() {
+               return rows;
+       }
+
+       public void setRows(List<AcRow> rows) {
+               this.rows = rows;
+       }
+       
+       
+       public void addRow(AcRow row) {
+               this.rows.add(row);
+       }
+       
+       
+       public AdditionConfig(){
+       }
+
+}
diff --git a/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionUtil.java b/nucalgen/src/main/java/jp/satomichan/nucalgen/addition/AdditionUtil.java
new file mode 100644 (file)
index 0000000..8b03346
--- /dev/null
@@ -0,0 +1,59 @@
+package jp.satomichan.nucalgen.addition;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.commons.digester.Digester;
+import org.xml.sax.SAXException;
+
+public class AdditionUtil {
+
+       public static void main(String[] args) {
+               AdditionConfig add = additionFileReader(new File("C:\\Users\\h520s\\Desktop\\pfc-balance.xml"));
+       }
+
+
+       public static AdditionConfig additionFileReader(File reportXmlFile){
+               SAXParserFactory factory = SAXParserFactory.newInstance();
+               javax.xml.parsers.SAXParser parser = null;
+               AdditionConfig addition = null;
+
+               try {
+                       parser = factory.newSAXParser();
+
+                       Digester digester = new Digester(parser);
+
+                       digester.addObjectCreate("addition", AdditionConfig.class);
+                       digester.addObjectCreate("addition/row", AcRow.class);
+
+                       digester.addSetNext("addition/row", "addRow");
+                       digester.addSetProperties("addition/row");
+
+                       digester.addObjectCreate("addition/row/cell", AcCell.class);
+
+                       digester.addSetNext("addition/row/cell", "addCell");
+                       digester.addBeanPropertySetter("addition/row/cell/type");
+                       digester.addBeanPropertySetter("addition/row/cell/value");
+                       digester.addBeanPropertySetter("addition/row/cell/alias");
+
+                       //パースする
+                       InputStream in;
+
+                       in = new FileInputStream(reportXmlFile);
+                       addition = (AdditionConfig)digester.parse(in);
+                       
+                       
+                       
+               } catch (IOException | SAXException | ParserConfigurationException e) {
+                       throw new RuntimeException(e);
+               }
+
+               return addition;
+
+       }
+}