1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.devtools.maintainablexml;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class ConvertMaintainableXML {
64
65 private static final String MAINTAINABLE_CONVERSION_CONFIG_FILE = "maintainable.conversion.rule.file";
66
67 public static void main(String[] args) {
68
69
70
71
72 String settingsFile = "";
73
74
75 if (StringUtils.isEmpty(settingsFile)) {
76 settingsFile = readInput("Enter config file location relative to Kuali main user directory "
77 + "(ie. dev/sample-app-config.xml OR MaintainableXMLConversionConfig.xml) : ", null, false);
78 }
79
80 String filePath = System.getProperty("user.home") + "/" + settingsFile;
81
82 File file = new File(filePath);
83 if (!file.exists()) {
84 System.out.println("The settings file location is not valid : \n" + filePath);
85 System.exit(1);
86 }
87
88 try {
89 HashMap settingsMap = getSettings(filePath);
90 System.out.println("Using the following settings : " + settingsMap + "\n");
91
92
93 String runMode = (String)settingsMap.get("runMode");
94 if (StringUtils.isEmpty(runMode) || !(StringUtils.equals(runMode, "1") || StringUtils.equals(runMode, "2"))) {
95 runMode = readInput("Run mode :\n1. Update xml in DB\n2. Print old and new xml\nEnter 1 or 2\n",
96 new String[]{"1", "2"}, false);
97 }
98
99
100 String fromRange = (String)settingsMap.get("fromRange");
101 if (StringUtils.isEmpty(fromRange) || (!StringUtils.equals(fromRange, "all") && !(isNumber(fromRange)))) {
102 fromRange = readInput("Please enter document start range value ('all', empty line for all, or the starting document number such as 1000) :\n",
103 new String[]{"all", ""}, true);
104 }
105
106 String toRange = null;
107 boolean hasRangeParameters = false;
108
109 if (!"".equals(fromRange) && !"all".equals(fromRange)) {
110
111 toRange = (String)settingsMap.get("toRange");
112 if (StringUtils.isEmpty(toRange) || !(isNumber(toRange))) {
113 toRange = readInput("Please enter end range value :\n", null, true);
114 }
115 System.out.println("Upgrading document numbers from " + fromRange + " to " + toRange + "\n");
116 hasRangeParameters = true;
117 }
118
119 FileConverter fileConverter = new FileConverter();
120
121
122 fileConverter.runFileConversion(settingsMap, runMode, fromRange, toRange, hasRangeParameters);
123
124 } catch (Exception e) {
125 System.out.println("Error executing conversion : " + e.getMessage());
126 e.printStackTrace();
127 }
128 }
129
130 public static boolean isNumber(String string) {
131 try {
132 Integer.parseInt(string);
133 } catch (Exception e) {
134 return false;
135 }
136 return true;
137 }
138
139
140
141
142
143
144
145
146 private static String readInput(String message, String[] validOptions, boolean numeric) {
147 System.out.print(message);
148 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
149
150 String inputString = null;
151
152 try {
153 inputString = br.readLine();
154 if (numeric && (validOptions == null || !Arrays.asList(validOptions).contains(inputString))) {
155 Integer.parseInt(inputString);
156 return inputString;
157 }
158 } catch (IOException ioe) {
159 System.out.println("IO error trying to read input!");
160 System.exit(1);
161 } catch (NumberFormatException nfe) {
162 System.out.println("Invalid Option! Must be numeric.");
163 readInput(message, validOptions, numeric);
164 }
165 if (validOptions != null && !Arrays.asList(validOptions).contains(inputString)) {
166 System.out.println("Invalid Option!");
167 readInput(message, validOptions, numeric);
168 }
169 return inputString;
170 }
171
172
173
174
175
176
177
178
179 public static HashMap getSettings(String filePath) throws Exception {
180 File file = new File(filePath);
181 HashMap params = new HashMap();
182 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
183 DocumentBuilder db = dbf.newDocumentBuilder();
184 Document doc = db.parse(file);
185 doc.getDocumentElement().normalize();
186 NodeList nodeLst = doc.getElementsByTagName("param");
187
188 for (int s = 0; s < nodeLst.getLength(); s++) {
189 Node fstNode = nodeLst.item(s);
190 if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
191 Element fstElmnt = (Element) fstNode;
192 String paramName = fstElmnt.getAttribute("name");
193 NodeList textFNList = fstElmnt.getChildNodes();
194 if (textFNList.item(0) != null) {
195 String paramValue = ((Node) textFNList.item(0)).getNodeValue().trim();
196 params.put(paramName, paramValue);
197 } else {
198 params.put(paramName, null);
199 }
200 }
201 }
202 return params;
203 }
204
205 }