();
classSet.addAll(Arrays.asList(classes));
ConfigParser parser = new ConfigParser();
parser.parseConfigFile(DEFAULT_SYSTEM_ID, classSet);
parser.addPackagePrefix("ptolemy.backtrack", classSet);
OutputStreamWriter writer = new OutputStreamWriter(System.out);
XmlOutput.outputXmlTree(parser.getTree(), writer);
writer.close();
}
/** Parse a configuration file and build the XML tree below the given
* parent node (or null if not given). Only the nodes
* corresponding to the classes in includedClasses and their
* parent nodes are created. Other nodes in the XML tree are ignored.
*
* This method is the same as parseConfigFile(fileName,
* includedClasses, true).
*
* @param fileName The name of the configuration file.
* @param includedClasses The set of names of classes to be included.
* @exception Exception If error occurs.
* @see #parseConfigFile(String, Set, boolean)
*/
public void parseConfigFile(String fileName, Set includedClasses)
throws Exception {
parseConfigFile(fileName, includedClasses, true);
}
/** Parse a configuration file and build the XML tree below the given
* parent node (or null if not given). Only the nodes
* corresponding to the classes in includedClasses and their
* parent nodes are created. Other nodes in the XML tree are ignored.
*
* @param fileName The name of the configuration file.
* @param includedClasses The set of names of classes to be included.
* @param backtrackingElement Whether to set the parent node of the
* constructed XML tree to be the "backtrack" node.
* @exception Exception If error occurs.
*/
public void parseConfigFile(String fileName, Set includedClasses,
boolean backtrackingElement) throws Exception {
XmlParser parser = new XmlParser();
BufferedReader br = new BufferedReader(new FileReader(fileName));
ConfigXmlHandler handler = new ConfigXmlHandler(_xmlTree, fileName,
includedClasses);
handler.addExcludedFiles(_excludedFiles);
parser.setHandler(handler);
try {
// This fails to parse gtLibrary because it goes into a loop,
// and causes a stack overflow, so we catch Throwable and
// throw an exception.
parser.parse(fileName, null, br);
} catch (Throwable throwable) {
throw new Exception("Failed to parse \"" + fileName + "\"",
throwable);
}
// Manually add a element to the root of the tree.
if (backtrackingElement) {
_xmlTree._setElementName("group");
}
}
///////////////////////////////////////////////////////////////////
//// public fields ////
// FindBugs suggests making these final.
/** The default path of the default Ptolemy configuration file. It is
* usually located at ptolemy/configs/full/configuration.xml in
* the Ptolemy tree.
*/
public static final String DEFAULT_SYSTEM_ID = PathFinder.getPtolemyPath()
+ "ptolemy/configs/full/configuration.xml";
///////////////////////////////////////////////////////////////////
//// private methods ////
/** The recursive function to traverse the XML tree and add a package
* prefix to each class name found in the given set.
*
* @param tree The XML tree to be traversed.
* @param packagePrefix The package prefix to be added.
* @param classes The set of names of affected classes.
*/
private void addPackagePrefix(ConfigXmlTree tree, String packagePrefix,
Set classes) {
String className = tree.getAttribute("class");
if ((className != null) && classes.contains(className)) {
tree.setAttribute("class", packagePrefix + "." + className);
}
tree.startTraverseChildren();
while (tree.hasMoreChildren()) {
addPackagePrefix(tree.nextChild(), packagePrefix, classes);
}
}
///////////////////////////////////////////////////////////////////
//// private fields ////
/** The set of excluded files.
*/
private Set _excludedFiles = new HashSet();
/** The XML tree.
*/
private ConfigXmlTree _xmlTree;
}