Debian Codegraph @

src/org/stathissideris/ascii2image/core/ConfigurationParser.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /**
2 * ditaa - Diagrams Through Ascii Art
3 *
•4 * Copyright (C) 2004-2011 Efstathios Sideris
5 *
•6 * ditaa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
•11 * ditaa is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
•14 * GNU Lesser General Public License for more details.
15 *
•16 * You should have received a copy of the GNU Lesser General Public
17 * License along with ditaa. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package org.stathissideris.ascii2image.core;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.HashMap;
26
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.parsers.SAXParser;
29 import javax.xml.parsers.SAXParserFactory;
30
31 import org.stathissideris.ascii2image.graphics.CustomShapeDefinition;
32 import org.xml.sax.Attributes;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.helpers.DefaultHandler;
35
36 public class ConfigurationParser {
37
•38 private static final boolean DEBUG = false;
39
40 private static final String INLCUDE_TAG_NAME = "include";
41 private static final String SHAPE_TAG_NAME = "shape";
42 private static final String SHAPE_GROUP_TAG_NAME = "shapes";
43
44 private String currentDir = "";
45 private File configFile;
46
47 private HashMap<String, CustomShapeDefinition> shapeDefinitions = new HashMap<String, CustomShapeDefinition>();
48
49 public Collection<CustomShapeDefinition> getShapeDefinitions() {
50 return shapeDefinitions.values();
51 }
52
53 public HashMap<String, CustomShapeDefinition> getShapeDefinitionsHash() {
54 return shapeDefinitions;
55 }
56
57 public void parseFile(File file) throws ParserConfigurationException,
58 SAXException, IOException {
59 configFile = file;
60
61 DefaultHandler handler = new XMLHandler();
62
63 // Use the default (non-validating) parser
64 SAXParserFactory factory = SAXParserFactory.newInstance();
65
66 SAXParser saxParser = factory.newSAXParser();
67 saxParser.parse(file, handler);
68 }
69
70 private class XMLHandler extends DefaultHandler {
71 public void startElement(String uri, String localName, String qName,
72 Attributes attributes) throws SAXException {
73 if (qName.equals(SHAPE_GROUP_TAG_NAME)) {
74 if (attributes.getLength() == 1) {
75 currentDir = attributes.getValue(0).trim();
76 if (currentDir.equals(""))
77 currentDir = configFile.getParentFile()
78 .getAbsolutePath();
79 } else {
80 // the dir that contains the config file:
81 currentDir = configFile.getParentFile().getAbsolutePath();
82 }
83 }
84 if (qName.equals(SHAPE_TAG_NAME)) {
85 CustomShapeDefinition definition = new CustomShapeDefinition();
86
87 int len = attributes.getLength();
88 for (int i = 0; i < len; i++) {
89 String name = attributes.getQName(i);
90 String value = attributes.getValue(i);
91
92 if (name.equals("tag")) {
93 definition.setTag(value);
94 } else if (name.equals("stretch")) {
95 definition
96 .setStretches(getBooleanFromAttributeValue(value));
97 } else if (name.equals("border")) {
98 definition
99 .setHasBorder(getBooleanFromAttributeValue(value));
100 } else if (name.equals("shadow")) {
101 definition
102 .setDropsShadow(getBooleanFromAttributeValue(value));
103 } else if (name.equals("comment")) {
104 definition.setComment(value);
105 } else if (name.equals("filename")) {
106 File file = new File(value);
107 if (file.isAbsolute()) {
108 definition.setFilename(value);
109 } else { // relative to the location of the config file
110 // or to the group's base dir
111 definition.setFilename(createFilename(currentDir,
112 value));
113 }
114 }
115 }
116
117 if (shapeDefinitions.containsKey(definition.getTag())) {
118 CustomShapeDefinition oldDef = shapeDefinitions
119 .get(definition.getTag());
120 System.err.println("*** Warning: shape \""
121 + oldDef.getTag() + "\" (file: "
122 + oldDef.getFilename()
123 + ") has been redefined as file: "
124 + definition.getFilename());
125 }
126
127 File file = new File(definition.getFilename());
128 if (file.exists()) {
129 shapeDefinitions.put(definition.getTag(), definition);
130 if (DEBUG)
131 System.out.println(definition);
132 } else {
133 System.err.println("File " + file
134 + " does not exist, skipping tag "
135 + definition.getTag());
136 }
137
138 }
139 if (qName.equals(INLCUDE_TAG_NAME)) {
140 if (attributes.getLength() == 1) {
141 File includedFile = new File(attributes.getValue(0).trim());
142
143 if (!includedFile.isAbsolute()) {
144 includedFile = new File(createFilename(configFile
145 .getParentFile().getAbsolutePath(),
146 includedFile.getPath()));
147 }
148
149 if (!includedFile.exists()) {
150 System.err.println("Included file " + includedFile
151 + " does not exist, skipping");
152 return;
153 }
154
155 ConfigurationParser configParser = new ConfigurationParser();
156 try {
157 configParser.parseFile(includedFile);
158 } catch (ParserConfigurationException e) {
159 e.printStackTrace();
160 } catch (SAXException e) {
161 e.printStackTrace();
162 } catch (IOException e) {
163 e.printStackTrace();
164 }
165 HashMap<String, CustomShapeDefinition> shapes = configParser
166 .getShapeDefinitionsHash();
167 shapeDefinitions.putAll(shapes);
168 }
169 }
170 }
171 }
172
173 private String createFilename(String baseDir, String filename) {
174 if (baseDir == null || baseDir.trim().equals("")) {
175 return filename;
176 }
177 if (baseDir.endsWith(File.separator)) {
178 return baseDir + filename;
179 }
180 return baseDir + File.separator + filename;
181 }
182
183 private boolean getBooleanFromAttributeValue(String value) {
184 value = value.toLowerCase();
185 if ("no".equals(value))
186 return false;
187 if ("false".equals(value))
188 return false;
189 if ("yes".equals(value))
190 return true;
191 if ("true".equals(value))
192 return true;
193 throw new IllegalArgumentException("value " + value
194 + " cannot be interpreted as a boolean");
195 }
196
197 public static void main(String argv[]) throws ParserConfigurationException,
198 SAXException, IOException {
199 ConfigurationParser parser = new ConfigurationParser();
200 parser.parseFile(new File("config.xml"));
201 parser.getShapeDefinitions();
202 }
203
204 }