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.util;
007:
008: import java.util.*;
009: import java.io.*;
010:
011: /**
012: * PBlock config file handling
013: */
014: public class PBlock {
015:
016: // XXX this should be replaced with java property files
017:
018: // static methods only
019: private PBlock() {
020: }
021:
022: /**
023: * Puts the contents of string into the Map as AV pairs.
024: * pbstr looks like a=b c=d e=f g h, etc. Each value may
025: * be surrounded by quotes "" which are stripped, and
026: * contain internal escaped chars, ie, \c (esp \"), which
027: * are unescaped.
028: */
029: public static void str2pblock(String line, Map map)
030: throws Exception {
031: int end = 0, eos = 0;
032: for (int param = 1;; ++param) {
033: if (end >= line.length())
034: break;
035: line = line.substring(end).trim(); // Shift next param to start
036: char[] str = line.toCharArray();
037: if (str.length == 0)
038: break; // Finished
039: end = 0;
040: eos = str.length;
041: while (end < eos && !Character.isWhitespace(str[end])
042: && str[end] != '=')
043: ++end; // Find end of param
044:
045: if (end == eos || Character.isWhitespace(str[end])) {
046: // Set the name to the param position when no '='
047: map.put(new String("" + param), line.substring(0, end));
048: continue;
049: }
050:
051: String atr = line.substring(0, end);
052: int valstart = ++end; // Skip '='
053:
054: if (valstart == eos
055: || Character.isWhitespace(str[valstart])) {
056: map.put(atr, ""); // Empty val - treat as ""
057: continue;
058: }
059:
060: StringBuffer val = new StringBuffer();
061:
062: boolean quoted = str[valstart] == '\"'; // For handling quoted values
063: if (quoted)
064: ++valstart;
065:
066: for (end = valstart; end < eos; ++end) {
067: if (quoted) {
068: if (str[end] == '\"') {
069: ++end;
070: break;
071: }
072: } else if (Character.isWhitespace(str[end])) {
073: break;
074: }
075: if (str[end] == '\\')
076: ++end; // Handle escaped chars
077: if (end < eos)
078: val.append(str[end]);
079: }
080: map.put(atr, val.toString());
081: }
082: }
083:
084: /** Read all of the parameters from a pblock config file into a map */
085: public static void parsePBlockConfigFile(String filename, Map map)
086: throws Exception {
087: BufferedReader r = new BufferedReader(new InputStreamReader(
088: new FileInputStream(filename), "UTF-8"));
089: String line;
090: while ((line = r.readLine()) != null) {
091: if (line.length() == 0 || line.charAt(0) == '#')
092: continue; // skip blank lines and comments
093: str2pblock(line, map);
094: }
095: }
096:
097: /** Return an OrderedMap for AV pairs by given string */
098: public static OrderedMap str2pblock(String line) throws Exception {
099: OrderedMap map = new OrderedMap();
100: str2pblock(line, map);
101: return map;
102: }
103:
104: /** Return a pblock string by given OrderedMap */
105: public static String pblock2str(OrderedMap map) throws Exception {
106: StringBuffer sb = new StringBuffer();
107: String preSpace = "";
108: if (map != null) {
109: String[] keys = map.getOrderedKeys();
110: if (keys != null) {
111: for (int i = 0; i < keys.length; i++) {
112: String value = (String) map.get(keys[i]);
113: if (value != null) {
114: try {
115: if (Integer.parseInt(keys[i]) == i + 1) {
116: sb.append(preSpace
117: + quotedString(value));
118: } else {
119: sb.append(preSpace + keys[i] + "="
120: + quotedString(value));
121: }
122: } catch (NumberFormatException e) {
123: sb.append(preSpace + keys[i] + "="
124: + quotedString(value));
125: }
126: if (preSpace.length() == 0) {
127: preSpace = " ";
128: }
129: }
130: }
131: }
132: }
133: return sb.toString();
134: }
135:
136: /** Return a escaped , and quoted if needed, string */
137: public static String quotedString(String value) {
138: boolean needQuote = false;
139: StringBuffer outBuffer = new StringBuffer();
140: value = value.trim();
141: char[] str = value.toCharArray();
142: if (str.length == 0)
143: return ""; // Finished
144: for (int i = 0; i < str.length; i++) {
145: if (str[i] == '\\' || str[i] == '"') {
146: outBuffer.append('\\');
147: outBuffer.append(str[i]);
148: } else if (Character.isWhitespace(str[i])
149: || !Character.isLetterOrDigit(str[i])) {
150: needQuote = true;
151: outBuffer.append(str[i]);
152: } else {
153: outBuffer.append(str[i]);
154: }
155: }
156: if (needQuote) {
157: return "\"" + outBuffer.toString() + "\"";
158: } else {
159: return outBuffer.toString();
160: }
161: }
162:
163: }
|