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
| import javax.xml.transform.dom.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import java.util.regex.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.stream.*;
public class Rapport {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
Rap_analyser rap=new Rap_analyser();
}
}
class Rap_analyser{
public Rap_analyser() throws ParserConfigurationException, SAXException, IOException{
File f = new File("C:\\Documents and Settings\\Propriétaire\\Bureau\\rapport définitif.fo");
try {
fin = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse(fin);
Element root = doc.getDocumentElement();
try {
analyse(root);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
try {
write();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
public void analyse(Node el) throws FileNotFoundException, TransformerException{
NodeList children = el.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if(child instanceof Element) { analyse(child); }
else if((child instanceof Text )&& (((Text) child).getData().trim().length() != 0)){
String txt = ((Text) child).getData();
System.out.println("txt="+txt);
pattern=Pattern.compile("Quest\\s*\\(\\s*(\\d+)\\s*:\\s*(\\d+)\\s*\\)");
matcher=pattern.matcher(txt);
while(matcher.find()) {
el.removeChild(child);
System.out.println( " trouvé " + matcher.group());
}
}
}
}
public void write() throws TransformerFactoryConfigurationError, FileNotFoundException, TransformerException{
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(new File("C:\\Documents and Settings\\Propriétaire\\Bureau\\new rapport définitif.fo"))));
}
DocumentBuilderFactory factory;
DocumentBuilder builder;
FileInputStream fin;
Document doc;
Pattern pattern;
Matcher matcher;
} |