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