1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.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.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public class ConvertMaintainableXML {
54
55 public static void main(String[] args) {
56
57 String settingsFile = readInput("Enter config file location relative to Kuali main user directory "
58 + "(ie. dev/sample-app-config.xml OR dev/trnapp-config.xml) : ", null, false);
59
60 String filePath = System.getProperty("user.home") + "/kuali/main/" + settingsFile;
61
62 File file = new File(filePath);
63 if (!file.exists()) {
64 System.out.println("The settings file location is not valid : \n" + filePath);
65 System.exit(1);
66 }
67
68 String runMode = readInput("Run mode :\n1. Update xml in DB\n2. Print old and new xml\nEnter 1 or 2\n",
69 new String[]{"1", "2"}, false);
70
71 String fromRange = readInput("Please enter document start range value ('%' or '' for all) :\n",
72 new String[]{"%", ""}, true);
73
74 String toRange = null;
75
76 boolean hasRangeParameters = false;
77
78 if (!"".equals(fromRange) && !"%".equals(fromRange)) {
79 toRange = readInput("Please enter end range value :\n", null, true);
80 System.out.println("Upgrading document numbers from " + fromRange + " to " + toRange + "\n");
81 hasRangeParameters = true;
82 }
83
84 System.out.println("Looking up settings file... " + filePath + "\n");
85
86 try {
87 HashMap map = getSettings(filePath);
88 System.out.println("Using the following settings : " + map + "\n");
89 FileConverter fileConverter = new FileConverter();
90 fileConverter.runFileConversion(map, runMode, fromRange, toRange, hasRangeParameters);
91 } catch (Exception e) {
92 System.out.println("Error executing conversion : " + e.getMessage());
93 e.printStackTrace();
94 }
95
96 }
97
98
99
100
101
102
103
104
105 private static String readInput(String message, String[] validOptions, boolean numeric) {
106 System.out.print(message);
107 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
108
109 String inputString = null;
110
111 try {
112 inputString = br.readLine();
113 if (numeric && (validOptions == null || !Arrays.asList(validOptions).contains(inputString))) {
114 Integer.parseInt(inputString);
115 return inputString;
116 }
117 } catch (IOException ioe) {
118 System.out.println("IO error trying to read input!");
119 System.exit(1);
120 } catch (NumberFormatException nfe) {
121 System.out.println("Invalid Option! Must be numeric.");
122 readInput(message, validOptions, numeric);
123 }
124 if (validOptions != null && !Arrays.asList(validOptions).contains(inputString)) {
125 System.out.println("Invalid Option!");
126 readInput(message, validOptions, numeric);
127 }
128 return inputString;
129 }
130
131
132
133
134
135
136
137
138 public static HashMap getSettings(String filePath) throws Exception {
139 File file = new File(filePath);
140 HashMap params = new HashMap();
141 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
142 DocumentBuilder db = dbf.newDocumentBuilder();
143 Document doc = db.parse(file);
144 doc.getDocumentElement().normalize();
145 NodeList nodeLst = doc.getElementsByTagName("param");
146
147 for (int s = 0; s < nodeLst.getLength(); s++) {
148 Node fstNode = nodeLst.item(s);
149 if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
150 Element fstElmnt = (Element) fstNode;
151 String paramName = fstElmnt.getAttribute("name");
152 NodeList textFNList = fstElmnt.getChildNodes();
153 String paramValue = ((Node) textFNList.item(0)).getNodeValue().trim();
154 params.put(paramName, paramValue);
155 }
156 }
157 return params;
158 }
159
160 }