* @throws DmcRuleExceptionSet
*/
public void parseFile(String fileName, boolean isResource) throws ResultException, DmcValueException, DmcRuleExceptionSet {
boolean inObject = false;
String attrName = null;
DmcUncheckedObject uco = null;
StringBuffer attrVal = new StringBuffer();
String val = null;
// Note: we replace the friggin' windows backslashes with forward slashes
String fn = fileName.replace('\\', '/');
int lastLine = 0;
LineNumberReader in = null;
// Reset out global exception instance
exG = null;
try {
// BufferedReader in = new BufferedReader(new FileReader(fileName));
if (isResource){
InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(fn));
in = new LineNumberReader(isr);
}
else
in = new LineNumberReader(new FileReader(fileName));
// System.out.println("Reading " + fileName);
String str;
while ((str = in.readLine()) != null) {
//DebugInfo.debug("Near line: " + in.getLineNumber());
if (str.startsWith("*") || str.startsWith("//")){
// It's a comment, skip it
}
else{
StringTokenizer t = new StringTokenizer(str);
if (t.countTokens() != 0){
if (inObject == false){
ArrayList<String> al = new ArrayList<String>();
// We're starting a new object - these tokens should be the classes
while(t.hasMoreTokens()){
al.add(t.nextToken().trim());
}
uco = new DmcUncheckedObject(al,in.getLineNumber());
inObject = true;
attrName = null;
}
else{
// We have tokens
if (str.startsWith(" ")){
// Line continuation
if (preserveNL.get(attrName) != null)
attrVal.append("\n" + str);
else
attrVal.append(str);
}
else{
// A new attribute line
if (attrName != null){
// Add the current attribute to the object
val = attrVal.toString().trim();
if (preserveNL.get(attrName) != null)
val = val.replaceAll("\n", "\\\\n");
uco.addValue(attrName,new String(val));
}
// Get the new attribute name
attrName = t.nextToken().trim();
// Wipe the value buffer
attrVal.delete(0,attrVal.length());
// Reset it with the contents of the current line
attrVal.append(str);
// Trim the attribute name and leading spaces
attrVal.delete(0,attrName.length());
if (attrVal.length() == 0){
// We have a missing token value
ResultException ex = new ResultException();
ex.addError("No value for attribute: " + attrName);
ex.setLocationInfo(fileName, in.getLineNumber());
throw(ex);
}
while(attrVal.charAt(0) == ' '){
attrVal.delete(0,1);
}
}
}
}
else{
if (uco != null){
// We have a blank line which means we've reached the end of an object - pass off
// the current object for processing
inObject = false;
// Tack on the last attribute
if (attrName != null){
val = attrVal.toString().trim();
if (preserveNL.get(attrName) != null)
val = val.replaceAll("\n", "\\\\n");
uco.addValue(attrName,new String(val));
}
try{
handler.handleObject(uco,fn, in.getLineNumber());
}
catch(ResultException ex){
// DebugInfo.debug(ex.toString());
// If this is the first exception, just hang on to it - we may
// wind up adding to it later. Otherwise, just append the results
// to our existing exception.
if (exG == null)
exG = ex;
else
exG.result.addResults(ex.result);
if (exG.result.worst() == Result.FATAL){
uco = null;
break;
}
if (allowedErrorsV == -1){
// Couldn't care less about errors! Just go merrily on our way.
}
else if ((allowedErrorsV == 0) && (exG.result.errors() > 0)){
// Couldn't allow errors - let's bail
uco = null;
break;
}
else if (exG.result.errors() >= allowedErrorsV){
// We've reached the limits of our patience
uco = null;
break;
}
}
// Reset our object and go on for the next one
uco = null;
}
}
}
lastLine = in.getLineNumber();
}
in.close();
}
catch (IOException e) {
if (exG == null)
exG = new ResultException();
exG.result.addResult(Result.FATAL,e.toString());
exG.result.lastResult().moreMessages("Occurred while reading file: " + fileName);
uco = null;
}
if (uco != null){
// Finish off for the final attribute and object
if (attrName != null){
// Add the current attribute to the object
// System.out.println("Adding *" + attrVal + "*");
val = new String(attrVal);
if (preserveNL.get(attrName) != null)
val = val.replaceAll("\n", "\\\\n");
uco.addValue(attrName,val.trim());
}
try{
handler.handleObject(uco,fn,lastLine);
}