📄 xmlreader.java
字号:
protected static String getStationType(Element station) {
NodeList sections = station.getElementsByTagName(XML_E_STATION_SECTION);
String[] sectionNames = new String[sections.getLength()];
// Gets all section classnames
for (int i=0; i<sectionNames.length; i++) {
sectionNames[i] = ((Element)sections.item(i)).getAttribute(XML_A_STATION_SECTION_CLASSNAME);
}
// Finds station type, basing on section names
if (sectionNames[0].equals(CLASSNAME_SINK))
return STATION_TYPE_SINK;
else if (sectionNames[0].equals(CLASSNAME_SOURCE))
return STATION_TYPE_SOURCE;
else if (sectionNames[0].equals(CLASSNAME_TERMINAL))
return STATION_TYPE_TERMINAL;
else if(sectionNames[1].equals(CLASSNAME_DELAY))
return STATION_TYPE_DELAY;
else if(sectionNames[1].equals(CLASSNAME_SERVER))
return STATION_TYPE_SERVER;
else if(sectionNames[2].equals(CLASSNAME_FORK))
return STATION_TYPE_FORK;
else if(sectionNames[0].equals(CLASSNAME_JOIN))
return STATION_TYPE_JOIN;
else if(sectionNames[1].equals(CLASSNAME_TUNNEL))
return STATION_TYPE_ROUTER;
return null;
}
// -----------------------------------------------------------------------------------------------
// --- Measure section ---------------------------------------------------------------------------
/**
* Parses all informations on measures to be taken during simulation
* @param root root element of XML Document
* @param model data structure where all properties have to be set
*/
protected static void parseMeasures(Element root, CommonModel model) {
NodeList measures = root.getElementsByTagName(XML_E_MEASURE);
Object stationKey, classKey;
String type;
Double alpha, precision;
for (int i=0; i<measures.getLength(); i++) {
String stationName = ((Element)measures.item(i)).getAttribute(XML_A_MEASURE_STATION);
if (stationName != null && !stationName.equals(""))
stationKey = stations.get(stationName);
else
stationKey = null;
String className = ((Element)measures.item(i)).getAttribute(XML_A_MEASURE_CLASS);
if (className != null && !className.equals(""))
classKey = classes.get(className);
else
classKey = null;
type = ((Element)measures.item(i)).getAttribute(XML_A_MEASURE_TYPE);
// Inverts alpha
alpha = new Double(1 - Double.parseDouble(
((Element)measures.item(i)).getAttribute(XML_A_MEASURE_ALPHA)));
precision = Double.valueOf(((Element)measures.item(i)).getAttribute(XML_A_MEASURE_PRECISION));
// Adds measure to the model
model.addMeasure(type, stationKey, classKey, alpha, precision);
}
}
// -----------------------------------------------------------------------------------------------
// --- Connection section ------------------------------------------------------------------------
/**
* Parses all informations on connections to be made into model
* @param root root element of XML Document
* @param model data structure where all properties have to be set
*/
protected static void parseConnections(Element root, CommonModel model) {
NodeList connections = root.getElementsByTagName(XML_E_CONNECTION);
Object sourceKey, targetKey;
for (int i=0; i<connections.getLength(); i++) {
sourceKey = stations.get(((Element)connections.item(i)).getAttribute(XML_A_CONNECTION_SOURCE));
targetKey = stations.get(((Element)connections.item(i)).getAttribute(XML_A_CONNECTION_TARGET));
// Adds connection to data structure
model.setConnected(sourceKey, targetKey, true);
}
}
// -----------------------------------------------------------------------------------------------
// --- Preloading section ------------------------------------------------------------------------
/**
* Parses all informations on preloading to be added to the model
* @param root root element of XML Document
* @param model data structure where all properties have to be set
*/
protected static void parsePreloading(Element root, CommonModel model) {
NodeList preload = root.getElementsByTagName(XML_E_PRELOAD);
if (preload.getLength() > 0) {
// For every station, search for classes and initial jobs in queue
NodeList station_pop = ((Element)preload.item(0)).getElementsByTagName(XML_E_STATIONPOPULATIONS);
for (int i=0; i<station_pop.getLength(); i++) {
Object stationKey = stations.get(((Element)station_pop.item(i)).getAttribute(XML_A_PRELOADSTATION_NAME));
NodeList class_pop = ((Element)station_pop.item(i)).getElementsByTagName(XML_E_CLASSPOPULATION);
for (int j=0; j<class_pop.getLength(); j++) {
Object classKey = classes.get(((Element)class_pop.item(j)).getAttribute(XML_A_CLASSPOPULATION_NAME));
Integer jobs = new Integer(((Element)class_pop.item(j)).getAttribute(XML_A_CLASSPOPULATION_POPULATION));
// Sets preloading informations
model.setPreloadedJobs(jobs, stationKey, classKey);
}
}
}
}
// -----------------------------------------------------------------------------------------------
// --- Blocking regions section ------------------------------------------------------------------
/**
* Parses all informations on blocking regions to be added to the model
* @param root root element of XML Document
* @param model data structure where all properties have to be set
*/
protected static void parseBlockingRegions(Element root, CommonModel model) {
NodeList regions = root.getElementsByTagName(XML_E_REGION);
// Creates each region into data structure
for (int i=0; i<regions.getLength(); i++) {
Element region = (Element) regions.item(i);
String name = region.getAttribute(XML_A_REGION_NAME);
String type = region.getAttribute(XML_A_REGION_TYPE);
if (type == null || type.equals(""))
type = Defaults.get("blockingRegionType");
// Adds blocking region to data structure
Object key = model.addBlockingRegion(name, type);
// Now parses all included stations
NodeList nodes = region.getElementsByTagName(XML_E_REGIONNODE);
for (int j = 0; j<nodes.getLength(); j++) {
String stationName = ((Element)nodes.item(j)).getAttribute(XML_A_REGIONNODE_NAME);
model.addRegionStation(key, stations.get(stationName));
}
// Now parses class constraints
NodeList classConstraints = region.getElementsByTagName(XML_E_CLASSCONSTRAINT);
for (int j = 0; j<classConstraints.getLength(); j++) {
Element constraint = (Element) classConstraints.item(j);
//TODO Add support for Double class constraints
int num = new Double(constraint.getAttribute(XML_A_CLASSCONSTRAINT_MAXJOBS)).intValue();
model.setRegionClassCustomerConstraint(key,
classes.get(constraint.getAttribute(XML_A_CLASSCONSTRAINT_CLASS)),
new Integer(num));
}
//TODO parse weights...
// Now parses global costraint
Element globalConstraint = (Element) region.getElementsByTagName(XML_E_GLOBALCONSTRAINT).item(0);
//TODO Add support for Double global constraints
int num = new Double(globalConstraint.getAttribute(XML_A_GLOBALCONSTRAINT_MAXJOBS)).intValue();
model.setRegionCustomerConstraint(key, new Integer(num));
// Now parses drop rules
NodeList drop = region.getElementsByTagName(XML_E_DROPRULES);
for (int j=0; j<drop.getLength(); j++) {
Element rule = (Element) drop.item(j);
model.setRegionClassDropRule(key,
classes.get(rule.getAttribute(XML_A_DROPRULES_CLASS)),
Boolean.valueOf(rule.getAttribute(XML_A_DROPRULES_DROP)));
}
}
}
// -----------------------------------------------------------------------------------------------
// --- Generic XML Loader ------------------------------------------------------------------------
/**
* Loads an XML file, returning the Document rappresentation of it. This method is generic
* and can be used to load every xml file. Actually it is used by <code>XMLReader</code>
* and by <code>GuiXMLReader</code>. This method will validate input file.
* @param filename name of the file to be loaded
* @param schemaSource url of schema to be used to validate the model
* @return Document rappresentation of input xml file
*/
public static Document loadXML(String filename, String schemaSource) {
DOMParser parser = new DOMParser();
try {
// Sets validation only if needed
if (schemaSource != null) {
parser.setFeature(NAMESPACES_FEATURE_ID, true);
parser.setFeature(VALIDATION_FEATURE_ID, true);
parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
parser.setFeature(VALIDATION_DYNAMIC_FEATURE_ID, true);
parser.setProperty(EXTERNAL_SCHEMA_LOCATION_PROPERTY_ID, schemaSource);
}
// Creates a DOM document from the xml file
parser.parse(filename);
return parser.getDocument();
} catch (SAXException e) {
System.err.println("XMLLoader Error - An error occurs while attempting to parse the document \"" + e.getMessage() + "\".");
/*
JOptionPane.showMessageDialog(null,
"An error occurs while attempting to parse the document \"" + e.getMessage() + "\".",
"JMT - File error",
JOptionPane.ERROR_MESSAGE); */
return null;
} catch (IOException e) {
System.err.println("XMLLoader Error - An error occurs while attempting to parse the document.");
return null;
}
}
/**
* Loads an XML file, returning the Document rappresentation of it. This method is generic
* and can be used to load every xml file. Actually it is used by <code>XMLReader</code>
* and by <code>GuiXMLReader</code>. This method will <b>not</b> validate input file.
* @param filename name of the file to be loaded
* @return Document rappresentation of input xml file
*/
public static Document loadXML(String filename) {
return loadXML(filename, null);
}
// -----------------------------------------------------------------------------------------------
// --- Debug -------------------------------------------------------------------------------------
/**
* This method is used for debug purpose to write a portion of xml on standard output.
* This can be removed freely!
* @param node node to be written on standard output
*/
protected static void write(Node node) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.setOutputProperty("encoding", ENCODING);
transformer.transform(new DOMSource(node),
new StreamResult(System.out));
} catch (TransformerConfigurationException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (TransformerFactoryConfigurationError transformerFactoryConfigurationError) {
transformerFactoryConfigurationError.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (TransformerException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
}
// -----------------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -