001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.search.robot;
007:
008: import java.util.*;
009: import java.io.*;
010: import java.net.*;
011:
012: public class ProcessConfig {
013:
014: class AVPair {
015: String name = null;
016: String valueString = null;
017: boolean comment = false;
018:
019: AVPair(String attribute, String value, boolean isCommentOut) {
020: name = attribute;
021: valueString = value;
022: comment = isCommentOut;
023: }
024: }
025:
026: Map avs = null;
027: ArrayList avsArray = null; //in order to keep original line order
028: ArrayList inProcessArray = null; // keep the lins between <Process ...> </Process>
029: String csid = null;
030: public String fileName = null;
031: ArrayList preComment = null;
032: ArrayList afterComment = null;
033: long fileLastModified = 0;
034:
035: static public void main(String[] args) throws Exception {
036:
037: if (args.length != 2) {
038: System.out
039: .println("Usage: ProcessConfig inputFileName outputFileName");
040: }
041: ProcessConfig processConf = ProcessConfig
042: .parseProcessConf(args[0]);
043: processConf.updateFile(args[1]);
044: }
045:
046: public ProcessConfig(String file_name) {
047: fileName = file_name;
048: Init();
049: }
050:
051: private void Init() {
052: avs = new HashMap();
053: avsArray = new ArrayList();
054: inProcessArray = new ArrayList();
055: preComment = new ArrayList();
056: afterComment = new ArrayList();
057: }
058:
059: private void reInit() {
060: try {
061: File file = new File(fileName);
062: long lastmodified = file.lastModified();
063: if (lastmodified <= fileLastModified) {
064: return;
065: }
066: Init();
067: parse();
068: } catch (Exception e) {
069: //ignore
070: }
071: }
072:
073: static public ProcessConfig parseProcessConf(String file)
074: throws Exception {
075: ProcessConfig processConf = new ProcessConfig(file);
076: processConf.parse();
077: return processConf;
078: }
079:
080: String Quote(String value) {
081: boolean needQuote = false;
082: StringBuffer outBuffer = new StringBuffer();
083: value = value.trim();
084: char[] str = value.toCharArray();
085: if (str.length == 0)
086: return ""; // Finished
087: for (int i = 0; i < str.length; i++) {
088: if (str[i] == '\\' || str[i] == '"') {
089: outBuffer.append('\\');
090: outBuffer.append(str[i]);
091: } else if (Character.isWhitespace(str[i])
092: || !Character.isLetterOrDigit(str[i])) {
093: needQuote = true;
094: outBuffer.append(str[i]);
095: } else {
096: outBuffer.append(str[i]);
097: }
098: }
099: if (needQuote) {
100: return "\"" + outBuffer.toString() + "\"";
101: } else {
102: return outBuffer.toString();
103: }
104: }
105:
106: AVPair str2AVPair(String line) throws Exception {
107: int end = 0, eos = 0;
108: boolean isCommentOut = false;
109: if (line.startsWith("#")) {
110: isCommentOut = true;
111: line = line.substring(1);
112: }
113: if (line == null || line.trim().length() == 0)
114: return null;
115: line = line.trim(); // Shift next param to start
116: char[] str = line.toCharArray();
117: if (str.length == 0)
118: return null; // Finished
119: end = 0;
120: eos = str.length;
121: while (end < eos && !Character.isWhitespace(str[end])
122: && str[end] != '=')
123: ++end; // Find end of param
124: if (end == eos || Character.isWhitespace(str[end])) {
125: return null; //syntax error
126: }
127:
128: String atr = line.substring(0, end);
129: int valstart = ++end; // Skip '='
130:
131: if (valstart == eos) {
132: return new AVPair(atr, "", isCommentOut);
133: }
134:
135: StringBuffer val = new StringBuffer();
136:
137: boolean quoted = str[valstart] == '\"'; // For handling quoted values
138: if (quoted)
139: ++valstart;
140:
141: for (end = valstart; end < eos; ++end) {
142: if (quoted) {
143: if (str[end] == '\"') {
144: ++end;
145: break;
146: }
147: } else if (Character.isWhitespace(str[end])) {
148: break;
149: }
150: if (str[end] == '\\')
151: ++end; // Handle escaped chars
152: if (end < eos)
153: val.append(str[end]);
154: }
155: return new AVPair(atr, val.toString(), isCommentOut);
156: }
157:
158: private void parse() throws Exception {
159: File file = new File(fileName);
160: fileLastModified = file.lastModified();
161: BufferedReader in = new BufferedReader(new FileReader(file));
162: String line = in.readLine();
163: boolean inProcessTag = false;
164: boolean inProcess = false;
165: boolean afterProcess = false;
166: while (line != null) {
167: line = line.trim();
168: if (!afterProcess && inProcessTag) {
169: if (line.startsWith(">")) {
170: inProcessTag = false;
171: inProcess = true;
172: } else {
173: AVPair av = str2AVPair(line);
174: avs.put(av.name, av);
175: avsArray.add(av);
176: }
177: } else if (!afterProcess && inProcess) {
178: if (line.startsWith("</Process")) {
179: afterProcess = true;
180: inProcess = false;
181: } else {
182: inProcessArray.add(line);
183: }
184: } else {
185: if (line.startsWith("<Process ")) {
186: inProcessTag = true;
187: line = line.substring(9);
188: AVPair av = str2AVPair(line);
189: if (av.name.compareToIgnoreCase("csid") != 0) {
190: throw new Exception(
191: "Missing csid=... in <Process ...");
192: }
193: csid = av.valueString;
194: } else if (afterProcess) {
195: afterComment.add(line);
196: } else {
197: preComment.add(line);
198: }
199: }
200: line = in.readLine();
201: }
202: }
203:
204: public void updateFile() {
205: updateFile(fileName);
206: }
207:
208: public void updateFile(String filename) {
209: PrintWriter out = null;
210: try {
211: FileOutputStream fout = new FileOutputStream(filename);
212: out = new PrintWriter(fout, true);
213: } catch (Exception e) {
214: System.out.println("[updateProcessConf]Exception:"
215: + e.getMessage());
216: return;
217: }
218: updateFile(out);
219: }
220:
221: public void updateFile(PrintWriter out) {
222: for (int i = 0; i < preComment.size(); i++) {
223: out.println((String) preComment.get(i));
224: }
225: out.println("<Process csid=" + Quote(csid));
226: for (int i = 0; i < avsArray.size(); i++) {
227: AVPair av = (AVPair) avsArray.get(i);
228: if (av.comment)
229: out.print("# ");
230: else
231: out.print(" ");
232: out.println(av.name + "=" + Quote(av.valueString));
233: }
234: out.println(">");
235: for (int i = 0; i < inProcessArray.size(); i++) {
236: out.println((String) inProcessArray.get(i));
237: }
238: out.println("</Process>");
239: for (int i = 0; i < afterComment.size(); i++) {
240: out.println((String) afterComment.get(i));
241: }
242: out.close();
243: }
244:
245: public String get(String name) {
246: reInit();
247: AVPair av = (AVPair) avs.get(name);
248: if (av != null && !av.comment) {
249: return av.valueString;
250: }
251: return null;
252: }
253:
254: public void put(String name, String value) {
255: int index = -1;
256: if (name == null || value == null) {
257: return;
258: }
259: if (avs.containsKey(name)) {
260: AVPair av = (AVPair) avs.get(name);
261: if (av != null) {
262: if (!av.comment || value.length() > 0) {
263: av = (AVPair) avs.remove(name);
264: if (av != null) {
265: index = avsArray.indexOf(av);
266: avsArray.remove(av);
267: }
268: } else {
269: // leave the comment if value is empty
270: return;
271: }
272: }
273: }
274: if (value.length() > 0) {
275: AVPair av = new AVPair(name, value, false);
276: avs.put(name, av);
277: if (index >= 0) {
278: avsArray.add(index, av);
279: } else {
280: avsArray.add(av);
281: }
282: }
283: }
284: }
|