001: /*
002: * JavuX - Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.proptree;
022:
023: import java.io.BufferedReader;
024: import java.io.FileInputStream;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.UnsupportedEncodingException;
029:
030: /**
031: * This class loads property tree documents into <code>PropTree</code> trees<br>
032: * <br>
033: * <b>This class is NOT thread safe.</b>
034: **/
035: public class PropTreeDocReader {
036: boolean skipComments;
037:
038: private StringBuffer value;
039: private boolean valueQuoted;
040: private String key;
041:
042: // private String lastKey;
043: private PropTree lastNode;
044: private PropTree rootNode;
045:
046: public PropTreeDocReader() {
047: this .skipComments = true;
048:
049: }
050:
051: public void setSkipComments(boolean skipComments) {
052: this .skipComments = skipComments;
053: }
054:
055: public PropTree load(String fileName) {
056: return load(fileName, null);
057: }
058:
059: public PropTree load(String fileName, PropTree rootNode) {
060: try {
061: FileInputStream fis = new FileInputStream(fileName);
062: PropTree r = load(fis, rootNode);
063: fis.close();
064: return r;
065: } catch (IOException e) {
066: return null;
067: }
068: }
069:
070: public PropTree load(InputStream is, PropTree rootNode) {
071: if (rootNode == null)
072: rootNode = new PropTree();
073: else
074: rootNode.removeAllChilds();
075: this .rootNode = rootNode;
076: // this.lastKey= "root";
077: load(is);
078: return rootNode;
079: }
080:
081: private void load(InputStream is) {
082: InputStreamReader isr = null;
083:
084: try {
085: isr = new InputStreamReader(is, "ISO8859_1");
086: } catch (UnsupportedEncodingException e) {
087: try {
088: isr = new InputStreamReader(is, "ISO-8859-1");
089: } catch (UnsupportedEncodingException e1) {
090: isr = new InputStreamReader(is);
091: }
092: }
093:
094: BufferedReader br = new BufferedReader(isr);
095:
096: value = new StringBuffer();
097: valueQuoted = false;
098: String line;
099: int i, j;
100:
101: try {
102: while (true) {
103: line = br.readLine();
104: if (line == null) {
105: procLine();
106: break;
107: }
108: ;
109: line = line.trim();
110: if (line.length() == 0 || line.startsWith("#")) {
111: procLine();
112: procComment(line);
113: } else if (isQuoted(line)) {
114: value.append(unquote(line));
115: valueQuoted = true;
116: } else {
117: procLine(); //previous line
118: i = line.indexOf('=');
119: j = line.indexOf(':');
120: if (j != -1 && j < i || i == -1)
121: i = j;
122: if (i > 0) {
123: key = line.substring(0, i).trim();
124: line = line.substring(i + 1).trim();
125: if (isQuoted(line))
126: valueQuoted = true;
127: value.append(unquote(line));
128: }
129: }
130: }
131: } catch (IOException e) {
132: }
133:
134: try {
135: is.close();
136: } catch (IOException e) {
137: }
138: }
139:
140: private boolean isQuoted(String s) {
141: if (s.length() < 1)
142: return false;
143: return (s.charAt(0) == '"' || s.charAt(0) == '\'');
144: }
145:
146: private String unquote(String s) {
147: if (s.length() > 0
148: && (s.charAt(0) == '"' || s.charAt(0) == '\''))
149: s = s.substring(1);
150: if (s.length() > 0
151: && (s.charAt(s.length() - 1) == '"' || s.charAt(s
152: .length() - 1) == '\''))
153: s = s.substring(0, s.length() - 1);
154: return s;
155: }
156:
157: private void procComment(String line) {
158: if (!skipComments) {
159: if (lastNode != null)
160: lastNode.addComment(line);
161: else
162: rootNode.addComment(line);
163: }
164: }
165:
166: private void procLine() {
167: if (key == null)
168: return;
169:
170: if (value.length() == 0 && !valueQuoted)
171: procDataNode(key, null);
172: else
173: procDataNode(key, PropTreeDocCoder.decode(value.toString()));
174:
175: key = null;
176: value.setLength(0);
177: valueQuoted = false;
178: }
179:
180: public void procDataNode(String key, String value) {
181: if (key.startsWith(".") && lastNode != null) {
182: lastNode = lastNode.continueNode(key, value);
183: } else
184: lastNode = rootNode.continueNode(key, value);
185: }
186:
187: }
|