1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
@Component("xmlBean")
@Scope("session")
public class Xml implements Serializable{
static Element racine = new Element("projects");
static org.jdom.Document document = new Document(racine);
public static void exportToXml(){
Element project = new Element("project");
racine.addContent(project);
Attribute id = new Attribute("id", "1");
project.setAttribute(id);
Attribute name = new Attribute("name", "project1");
project.setAttribute(name);
Attribute startdate = new Attribute("startdate", "2010,12,14");
project.setAttribute(startdate);
Element task = new Element("task");
project.addContent(task);
Attribute idTask = new Attribute("id", "12");
task.setAttribute(idTask);
Element taskName = new Element("name");
taskName.setText("project1 task1");
task.addContent(taskName);
Element taskEst = new Element("est");
taskEst.setText("2010,12,20");
task.addContent(taskEst);
Element taskDuration = new Element("duration");
taskDuration.setText("140");
task.addContent(taskDuration);
Element taskPercentcompleted = new Element("percentcompleted");
taskPercentcompleted.setText("60");
task.addContent(taskPercentcompleted);
Element taskPredecessortasks = new Element("predecessortasks");
taskPredecessortasks.setText("");
task.addContent(taskPredecessortasks);
Element childtasks = new Element("childtasks");
task.addContent(childtasks);
Element childtasksTask = new Element("task");
childtasks.addContent(childtasksTask);
Attribute idChildtasksTask = new Attribute("id", "2");
childtasksTask.setAttribute(idChildtasksTask);
Element nameChildtasksTask = new Element("name");
nameChildtasksTask.setText("project1 task1");
childtasksTask.addContent(nameChildtasksTask);
Element estChildtasksTask = new Element("est");
estChildtasksTask.setText("2010,12,20");
childtasksTask.addContent(estChildtasksTask);
Element durationChildtasksTask = new Element("duration");
durationChildtasksTask.setText("140");
childtasksTask.addContent(durationChildtasksTask);
Element percentcompletedChildtasksTask = new Element("percentcompleted");
percentcompletedChildtasksTask.setText("60");
childtasksTask.addContent(percentcompletedChildtasksTask);
Element predecessortasksChildtasksTask = new Element("predecessortasks");
predecessortasksChildtasksTask.setText("");
childtasksTask.addContent(predecessortasksChildtasksTask);
Element childtasksChildtasksTask = new Element("childtasks");
childtasksChildtasksTask.setText("");
childtasksTask.addContent(childtasksChildtasksTask);
Element task2 = new Element("task");
project.addContent(task2);
Attribute idTask2 = new Attribute("id", "1222");
task2.setAttribute(idTask2);
Element taskName2 = new Element("name");
taskName2.setText("project1 task122222");
task.addContent(taskName2);
Element taskEst2 = new Element("est");
taskEst2.setText("2010,12,20");
task.addContent(taskEst2);
Element taskDuration2 = new Element("duration");
taskDuration2.setText("140222");
task.addContent(taskDuration2);
Element taskPercentcompleted2 = new Element("percentcompleted");
taskPercentcompleted2.setText("60");
task.addContent(taskPercentcompleted2);
Element taskPredecessortasks2 = new Element("predecessortasks");
taskPredecessortasks2.setText("");
task.addContent(taskPredecessortasks2);
Element childtasks2 = new Element("childtasks");
task.addContent(childtasks2);
System.err.println("-----------------------------------------------");
enregistre("Data1.xml");
System.err.println("-------''''''''''''''''''''''''----------------------------------------");
String myInputFile = "C:\\Users\\user\\Documents\\NetBeansProjects\\gest_projet\\Data1.xml";
String myOutputFile = "C:\\Users\\user\\Documents\\NetBeansProjects\\gest_projet\\Data2.xml";
String theSymbol = "<predecessortasks />";
String theCommentSymbol = " ";
String theTag = "<predecessortasks></predecessortasks>";
FindAndUpdate(myInputFile, myOutputFile, theSymbol, theCommentSymbol, theTag);
myInputFile = "C:\\Users\\user\\Documents\\NetBeansProjects\\gest_projet\\Data2.xml";
myOutputFile = "C:\\Users\\user\\Documents\\NetBeansProjects\\gest_projet\\Data3.xml";
theSymbol = "<childtasks />";
theCommentSymbol = " ";
theTag = "<childtasks></childtasks>";
System.out.println("-------'''''''''''''++++++++++++++++++++++++='''''''''''----------------------------------------");
FindAndUpdate(myInputFile, myOutputFile, theSymbol, theCommentSymbol, theTag);
System.out.println("-------'''''''''''''++++++++++++++++++++++++='''''''''''----------------------------------------");
}
//////////////////////////////// ENREQISTER //////////////////
public static void enregistre(String fichier) {
try {
System.out.println("---------------------- entregistrer-------------------------");
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, new FileOutputStream(fichier));
} catch (java.io.IOException e) {
}
}
////////////////////////////// FIN ENREQISTER ///////////////////
//////////////////////////////// UPDATE //////////////////
public static void FindAndUpdate(String theInputFileName, String theOutputFileName, String theSymbol, String theCommentSymbol, String theExpression) {
int myStartIndex = -1;
String myTmpStr = null;
String myOutputString = "";
boolean found = false;
RandomAccessFile myInputFile = null;
RandomAccessFile myOutputFile = null;
String myLine = null;
System.out.println("----------------------update-------------------------");
try {
// --- Open the file
myInputFile = new RandomAccessFile(theInputFileName, "r");
myOutputFile = new RandomAccessFile(theOutputFileName, "rw");
// --- Read line per line
while ((myLine = myInputFile.readLine()) != null) {
// --- Init the index for the new line
myStartIndex = -1;
found = false;
myOutputString = "";
myTmpStr = myLine;
// --- And for each line, search the pattern 'theSymbol' and try to comment it
while (myTmpStr != "" && (myStartIndex = myTmpStr.indexOf(theSymbol)) != -1) {
found = true;
//myOutputString = myTmpStr.substring(0, myStartIndex).concat(theExpression).concat( theSymbol );
myOutputString = myTmpStr.substring(0, myStartIndex).concat(theExpression).concat(" ");
myTmpStr = myTmpStr.substring(myStartIndex + theSymbol.length(), myTmpStr.length());
}
// --- Add the end of the line
if (myTmpStr != "") {
myOutputString += myTmpStr;
}
// --- If the tag had been found, comment the line (add '//' at the beginning of the line
if (found) {
myOutputString = "\n".concat(theCommentSymbol).concat(myOutputString);
} else // --- No symbol found, so just write the same line
{
myOutputString = "\n".concat(myLine);
}
// --- Finally add this new line to the output file
myOutputFile.write(myOutputString.getBytes());
}
} catch (IOException e) {
System.out.println("IOException : " + e.getMessage());
} finally {
// --- Close the openned files
try {
myInputFile.close();
myOutputFile.close();
} catch (Exception e) {
// --- Do nothing
}
}
}
//////////////////////////////// FIN UPDATE //////////////////
} |
Partager