| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.design; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.util.Stack; |
| 20 |
|
| 21 |
import org.apache.tools.ant.Location; |
| 22 |
import org.apache.tools.ant.Project; |
| 23 |
import org.xml.sax.Attributes; |
| 24 |
import org.xml.sax.ContentHandler; |
| 25 |
import org.xml.sax.InputSource; |
| 26 |
import org.xml.sax.Locator; |
| 27 |
import org.xml.sax.SAXException; |
| 28 |
import org.xml.sax.SAXParseException; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class DesignFileHandler implements ContentHandler { |
| 35 |
|
| 36 |
private final static String DESIGN = "design"; |
| 37 |
private final static String PACKAGE = "package"; |
| 38 |
private final static String DEPENDS = "depends"; |
| 39 |
|
| 40 |
private Log log = null; |
| 41 |
private File file = null; |
| 42 |
private boolean isCircularDesign; |
| 43 |
private boolean needDeclarationsDefault = true; |
| 44 |
private boolean needDependsDefault = true; |
| 45 |
|
| 46 |
private Design design = null; |
| 47 |
private Package currentPackage = null; |
| 48 |
private Stack stack = new Stack(); |
| 49 |
private Locator locator = null; |
| 50 |
private Location loc; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
DesignFileHandler(Log log, File file, boolean isCircularDesign, Location loc) { |
| 56 |
this.log = log; |
| 57 |
this.file = file; |
| 58 |
this.isCircularDesign = isCircularDesign; |
| 59 |
this.loc = loc; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public void setNeedDeclarationsDefault(boolean b) { |
| 66 |
needDeclarationsDefault = b; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public void setNeedDependsDefault(boolean b) { |
| 73 |
needDependsDefault = b; |
| 74 |
} |
| 75 |
|
| 76 |
public Design getDesign() { |
| 77 |
return design; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public InputSource resolveEntity(String publicId, |
| 90 |
String systemId) { |
| 91 |
log.log("publicId="+publicId+" systemId="+systemId, |
| 92 |
Project.MSG_VERBOSE); |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setDocumentLocator(Locator locator) { |
| 103 |
this.locator = locator; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void endPrefixMapping(String prefix) throws SAXException { |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public void startElement(String uri, String name, String qName, Attributes attrs) throws SAXException { |
| 122 |
log.log("Parsing startElement="+name, Project.MSG_DEBUG); |
| 123 |
if (name == null || "".equals(name)) { |
| 124 |
|
| 125 |
name = qName; |
| 126 |
} |
| 127 |
|
| 128 |
try { |
| 129 |
Object o = null; |
| 130 |
if(name.equals(DESIGN)) { |
| 131 |
o = handleDesign(attrs); |
| 132 |
} else if(name.equals(PACKAGE)) { |
| 133 |
currentPackage = handlePackage(attrs); |
| 134 |
o = currentPackage; |
| 135 |
} else if(name.equals(DEPENDS)) { |
| 136 |
o = handleDepends(attrs); |
| 137 |
} else { |
| 138 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 139 |
+", Unexpected element \"" + name + "\"", locator); |
| 140 |
} |
| 141 |
stack.push(o); |
| 142 |
} catch(RuntimeException e) { |
| 143 |
log.log("exception111111111111111111", Project.MSG_INFO); |
| 144 |
throw new SAXParseException("PRoblem parsing", locator, e); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private Design handleDesign(Attributes attrs) throws SAXParseException { |
| 149 |
if(attrs.getLength() > 0) |
| 150 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 151 |
+", no attributes allowed for "+DESIGN+" element", locator); |
| 152 |
else if(stack.size() > 0) |
| 153 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 154 |
+", "+DESIGN+" cannot be a subelement of "+stack.pop(), locator); |
| 155 |
else if(attrs.getLength() > 0) |
| 156 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 157 |
+", "+DESIGN+" element can't have any attributes", locator); |
| 158 |
design = new Design(isCircularDesign, log, loc); |
| 159 |
return design; |
| 160 |
} |
| 161 |
|
| 162 |
private Package handlePackage(Attributes attrs) throws SAXParseException { |
| 163 |
if(stack.size() <= 0 || !(stack.peek() instanceof Design)) |
| 164 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 165 |
+", "+PACKAGE+" element must be nested in a "+DESIGN+" element", locator); |
| 166 |
|
| 167 |
int len = attrs.getLength(); |
| 168 |
String name = null; |
| 169 |
String thePackage = null; |
| 170 |
String depends = null; |
| 171 |
String subpackages = null; |
| 172 |
String needDeclarations = null; |
| 173 |
String needDepends = null; |
| 174 |
for(int i = 0; i < len; i++) { |
| 175 |
String attrName = attrs.getLocalName(i); |
| 176 |
|
| 177 |
if ("".equals(attrName)) { |
| 178 |
|
| 179 |
attrName = attrs.getQName(i); |
| 180 |
} |
| 181 |
String value = attrs.getValue(i); |
| 182 |
log.log("attr="+attrName+" value="+value, Project.MSG_DEBUG); |
| 183 |
if("name".equals(attrName)) |
| 184 |
name = value; |
| 185 |
else if("package".equals(attrName)) |
| 186 |
thePackage = value; |
| 187 |
else if("depends".equals(attrName)) |
| 188 |
depends = value; |
| 189 |
else if("subpackages".equals(attrName)) |
| 190 |
subpackages = value; |
| 191 |
else if("needdeclarations".equals(attrName)) |
| 192 |
needDeclarations = value; |
| 193 |
else if("needdepends".equals(attrName)) |
| 194 |
needDepends = value; |
| 195 |
else |
| 196 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 197 |
+"\n'"+attrName+"' attribute is an invalid attribute for the package element", locator); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
if(subpackages == null) |
| 202 |
subpackages = "exclude"; |
| 203 |
if(needDeclarations == null) |
| 204 |
needDeclarations = Boolean.toString(needDeclarationsDefault); |
| 205 |
if(needDepends == null) |
| 206 |
needDepends = Boolean.toString(needDependsDefault); |
| 207 |
|
| 208 |
|
| 209 |
if(name == null) |
| 210 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 211 |
+", package element must contain the 'name' attribute", locator); |
| 212 |
else if(thePackage == null) |
| 213 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 214 |
+", package element must contain the 'package' attribute", locator); |
| 215 |
else if(!("include".equals(subpackages) || "exclude".equals(subpackages))) |
| 216 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 217 |
+"\nThe subpackages attribute in the package element can only have a" |
| 218 |
+"\nvalue of \"include\" or \"exclude\". value='"+subpackages+"'", locator); |
| 219 |
else if(!("true".equals(needDeclarations) || "false".equals(needDeclarations))) |
| 220 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 221 |
+"\nThe needdeclarations attribute in the package element can only have a" |
| 222 |
+"\nvalue of \"true\" or \"false\". value='"+needDeclarations+"'", locator); |
| 223 |
else if(!("true".equals(needDepends) || "false".equals(needDepends))) |
| 224 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 225 |
+"\nThe needdepends attribute in the package element can only have a" |
| 226 |
+"\nvalue of \"true\" or \"false\". value='"+needDepends+"'", locator); |
| 227 |
|
| 228 |
Package p = new Package(); |
| 229 |
p.setName(name); |
| 230 |
p.setPackage(thePackage); |
| 231 |
if("exclude".equals(subpackages)) |
| 232 |
p.setIncludeSubpackages(false); |
| 233 |
else |
| 234 |
p.setIncludeSubpackages(true); |
| 235 |
if("true".equals(needDeclarations)) |
| 236 |
p.setNeedDeclarations(true); |
| 237 |
else |
| 238 |
p.setNeedDeclarations(false); |
| 239 |
if("true".equals(needDepends)) |
| 240 |
p.setNeedDepends(true); |
| 241 |
else |
| 242 |
p.setNeedDepends(false); |
| 243 |
|
| 244 |
if(depends != null) |
| 245 |
p.addDepends(new Depends(depends)); |
| 246 |
return p; |
| 247 |
} |
| 248 |
|
| 249 |
private Depends handleDepends(Attributes attrs) throws SAXParseException { |
| 250 |
if(stack.size() <= 0 || !(stack.peek() instanceof Package)) |
| 251 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 252 |
+", "+DEPENDS+" element must be nested in a "+PACKAGE+" element", locator); |
| 253 |
else if(attrs.getLength() > 0) |
| 254 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 255 |
+", "+DEPENDS+" element can't have any attributes", locator); |
| 256 |
|
| 257 |
return new Depends(); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public void endElement(String uri, String localName, String qName) throws SAXException { |
| 264 |
try { |
| 265 |
Object o = stack.pop(); |
| 266 |
if(o instanceof Package) { |
| 267 |
Package p = (Package)o; |
| 268 |
|
| 269 |
Package tmp = design.getPackage(p.getName()); |
| 270 |
if(tmp != null) |
| 271 |
throw new SAXParseException("Error in file="+file.getAbsolutePath() |
| 272 |
+"\nname attribute on "+PACKAGE+" element has the same\n" |
| 273 |
+"name as another package. name=\""+p.getName()+"\" is used twice or more", locator); |
| 274 |
|
| 275 |
|
| 276 |
design.addConfiguredPackage(p); |
| 277 |
currentPackage = null; |
| 278 |
} else if(o instanceof Depends) { |
| 279 |
Depends d = (Depends)o; |
| 280 |
currentPackage.addDepends(d); |
| 281 |
} |
| 282 |
} catch(RuntimeException e) { |
| 283 |
throw new SAXParseException("exception", locator, e); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void skippedEntity(String name) throws SAXException { |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public void startDocument() throws SAXException { |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public void endDocument() throws SAXException { |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public void characters(char[] ch, int start, int length) throws SAXException { |
| 309 |
try { |
| 310 |
Object o = stack.peek(); |
| 311 |
if(o instanceof Depends) { |
| 312 |
String s = new String(ch, start, length); |
| 313 |
Depends d = (Depends)o; |
| 314 |
if (d.getName() != null) |
| 315 |
d.setName(d.getName() + s.trim()); |
| 316 |
else |
| 317 |
d.setName(s.trim()); |
| 318 |
} |
| 319 |
} catch(RuntimeException e) { |
| 320 |
log.log("exception3333333333333333333", Project.MSG_INFO); |
| 321 |
throw new SAXParseException("exception", locator, e); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public void processingInstruction(String target, String data) throws SAXException { |
| 335 |
} |
| 336 |
} |