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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package metier;
import javax.xml.parsers.*;
import org.w3c.dom.*;
//import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;
public class essai {
//public essai()
//{
//}
public String var;
public String SRCPort;
public String DESTPort;
public String SRCAdd;
public String DESTAdd;
public String Action;
/*public static String[]Array_Prctl={""};
public static String []Array_SRCPort={""};
public static String []Array_DESTPort={""};
String []Array_SRCAdd={""};
String []Array_DESTAdd={""};
String []Array_Action={""};*/
public void transformerXml(Document document, String fichier) {
try {
// Création de la source DOM
Source source = new DOMSource(document);
// Création du fichier de sortie
File file = new File(fichier);
Result resultat = new StreamResult(fichier);
// Configuration du transformer
TransformerFactory fabrique = TransformerFactory.newInstance();
Transformer transformer = fabrique.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
// Transformation
transformer.transform(source, resultat);
}catch(Exception e){
e.printStackTrace();
}
}
public essai()
{
super();
}
// public void File_Creation(String Prctl,String SRCPort,String DESTPort,String DESTAdd,String SRCAdd,String Action )
public void File_Creation()
{
try{
// Création d'un nouveau DOM
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
DocumentBuilder constructeur = fabrique.newDocumentBuilder();
Document document = constructeur.newDocument();
// Propriétés du DOM
document.setXmlVersion("1.0");
document.setXmlStandalone(true);
// Création de l'arborescence du DOM
Element racine = document.createElement("firewall");
racine.appendChild(document.createComment("Commentaire sous la racine"));
//for (int j=0;j<Array_Prctl.length;j++)
//{
Element protocole = document.createElement("protocole");
//protocole.setAttribute("nom_protocole",Array_Prctl[j]);
protocole.setAttribute("nom_protocole",var);
racine.appendChild(protocole);
Element add_src = document.createElement("add_src");
add_src.setAttribute("nom_add_src","192.168.1.20");
protocole.appendChild(add_src);
Element add_dest = document.createElement("add_dest");
add_dest.setAttribute("nom_add_dest","192.1.1.50");
add_src.appendChild(add_dest);
Element port_src = document.createElement("port_src");
// port_src.setAttribute("nom_port_src",Array_SRCPort[j]);
port_src.setAttribute("nom_port_src",SRCPort);
add_dest.appendChild(port_src);
Element port_dest = document.createElement("port_dest");
//port_dest.setAttribute("nom_port_dest",Array_DESTPort[j]);
port_dest.setAttribute("nom_port_dest",DESTAdd);
port_src.appendChild(port_dest);
Element action = document.createElement("action");
// action.setAttribute("nom_action",Array_Action[j]);
action.setAttribute("nom_action",Action);
port_dest.appendChild(action);
document.appendChild(racine);
//}
//Sauvegarde du DOM dans un fichier XML
transformerXml(document, "./mon_document.xml");
System.out.println("ok");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args)
{
String Prctol="local";
String SrcPort="local";
String DestPort="local";
String SrcAdd="local";
String DestAdd="local";
String Action="local";
essai test=new essai();
//test.File_Creation(Prctol,SrcPort,DestPort,SrcAdd,DestAdd,Action);
test.File_Creation();
}
} |
Partager