00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.benchmarks.timit.util;
00032
00033 import java.io.File;
00034 import java.io.FileOutputStream;
00035 import java.io.IOException;
00036 import java.util.Arrays;
00037 import java.util.HashMap;
00038 import java.util.Iterator;
00039 import java.util.List;
00040 import java.util.regex.Matcher;
00041 import java.util.regex.Pattern;
00042
00043 import org.jdom.Attribute;
00044 import org.jdom.Document;
00045 import org.jdom.Element;
00046 import org.jdom.input.SAXBuilder;
00047 import org.jdom.output.Format;
00048 import org.jdom.output.XMLOutputter;
00049
00055 public class XMLHelper {
00056
00064 public static Document readFile(String filename) {
00065 try {
00066 return new SAXBuilder().build(new File(filename));
00067
00068 } catch (Exception e) {
00069 e.printStackTrace();
00070 return null;
00071 }
00072 }
00073
00082 public static void writeFile(Document document, String filename) {
00083 try {
00084 XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
00085 File file = new File(filename);
00086
00087 String path = file.getParent();
00088 if (path != null) {
00089 new File(path).mkdirs();
00090 }
00091 FileOutputStream fos = new FileOutputStream(file);
00092 out.output(document, fos);
00093
00094 } catch (Exception e) {
00095 System.err.println("Unable to write file: " + filename);
00096 e.printStackTrace();
00097 }
00098 }
00099
00108 public static void errorLog(String filename, String message) {
00109 try {
00110 File file = new File(filename);
00111 String path = file.getParent();
00112 if (path != null) {
00113 new File(path).mkdirs();
00114 }
00115 FileOutputStream fos = new FileOutputStream(file, true);
00116 String time = ""
00117 + (new java.sql.Timestamp(System.currentTimeMillis()));
00118 message = time + " " + message + "\n";
00119 fos.write(message.getBytes());
00120 fos.close();
00121
00122 } catch (Exception e) {
00123 System.err.println("Unable to write file: " + filename);
00124 e.printStackTrace();
00125 }
00126 }
00127
00142 public static void generateDescriptor(String inFilename,
00143 HashMap<String, String> gvars, HashMap<String, String> lvars,
00144 String outFilename) {
00145
00146
00147 Document doc = XMLHelper.readFile(inFilename);
00148 Element eVariables = doc.getRootElement().getChild("variables");
00149 Iterator it = eVariables.getChildren().iterator();
00150 while (it.hasNext()) {
00151 Element var = (Element) it.next();
00152 String name = var.getAttributeValue("name");
00153 if (lvars.containsKey(name)) {
00154 var.setAttribute("value", lvars.get(name));
00155 } else if (gvars.containsKey(name)) {
00156 var.setAttribute("value", gvars.get(name));
00157 }
00158 }
00159
00160
00161 XMLHelper.writeFile(doc, outFilename);
00162 }
00163
00174 public static void replaceAll(Element elt, String old, String value) {
00175 Iterator itAttr = elt.getAttributes().iterator();
00176 while (itAttr.hasNext()) {
00177 Attribute attr = (Attribute) itAttr.next();
00178 attr.setValue(attr.getValue().replaceAll(old, value));
00179 }
00180 }
00181
00190 public static void replaceVariables(List serieList,
00191 HashMap<String, String> vars) {
00192
00193
00194 Pattern p = Pattern.compile("[^\\x24\\x7B\\x7D]*\\x24\\x7B" +
00195 "([^\\x7D]*)" +
00196 "\\x7D[^\\x7D\\x24\\x7B]*");
00197 Iterator it = serieList.iterator();
00198 while (it.hasNext()) {
00199 Element serie = (Element) it.next();
00200
00201 replaceVariablesAttributes(serie, p, vars);
00202
00203
00204 Iterator itSerie = serie.getDescendants();
00205 while (itSerie.hasNext()) {
00206 Object elt = itSerie.next();
00207 if (elt instanceof Element) {
00208 replaceVariablesAttributes((Element) elt, p, vars);
00209 }
00210 }
00211 }
00212 }
00213
00225 private static void replaceVariablesAttributes(Element elt, Pattern p,
00226 HashMap<String, String> vars) {
00227 Iterator itAttr = elt.getAttributes().iterator();
00228 while (itAttr.hasNext()) {
00229 Attribute attr = (Attribute) itAttr.next();
00230 String values = attr.getValue();
00231 Matcher m = p.matcher(values);
00232 while (m.find()) {
00233 String var = m.group(1);
00234 String resolve = vars.get(var);
00235 values = values.replaceAll("\\x24\\x7B" + var + "\\x7D",
00236 resolve.split(",").length == 1 ? resolve : "#{"
00237 + resolve + "}");
00238 attr.setValue(values);
00239 }
00240 }
00241 }
00242
00253 public static void tagFiltering(Element eTag, String[] values) {
00254 Arrays.sort(values);
00255 List<Element> children = eTag.getChildren();
00256 int i = 0;
00257
00258 while( i<children.size() ) {
00259 Element elt = children.get(i);
00260 Element parent = elt.getParentElement();
00261
00262 while( ! filter( elt, values ) ) {
00263 if( ! elt.getName().equals(parent.getName()) ) {
00264 children = eTag.getChildren();
00265 i--;
00266 break;
00267 }
00268 elt = parent;
00269 }
00270 i++;
00271 }
00272 }
00273
00274 private static boolean filter( Element eTag, String[] values ) {
00275 if (values.length == 0) {
00276 return true;
00277 }
00278 if (Arrays.binarySearch(values, eTag.getAttributeValue("name")) < 0) {
00279
00280 eTag.detach();
00281 return false;
00282 } else {
00283 List children = eTag.getChildren();
00284 for (int i = 0; i < children.size(); i++) {
00285 Element child = (Element) children.get(i);
00286 Element parent = child.getParentElement();
00287 while( ! filter(child, values) ) {
00288 child = parent;
00289 }
00290 }
00291 return true;
00292 }
00293 }
00294
00300 public static void printOut(Document doc) {
00301 XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
00302 try {
00303 xmlOutputter.output(doc, System.out);
00304 } catch (IOException ex) {
00305 ex.printStackTrace();
00306 }
00307 }
00308 }