1 | package org.medias.eccad.helpers; |
---|
2 | |
---|
3 | import java.io.InputStream; |
---|
4 | import java.io.StringReader; |
---|
5 | import java.util.Date; |
---|
6 | import java.util.Iterator; |
---|
7 | import java.util.Set; |
---|
8 | |
---|
9 | |
---|
10 | import org.dom4j.Document; |
---|
11 | import org.dom4j.DocumentException; |
---|
12 | import org.dom4j.io.SAXReader; |
---|
13 | import org.xml.sax.EntityResolver; |
---|
14 | import org.xml.sax.InputSource; |
---|
15 | |
---|
16 | public class XmlHelper { |
---|
17 | |
---|
18 | public static String createNode(String tag, String valeur) { |
---|
19 | String noeud = new String(); |
---|
20 | if (valeur == null) |
---|
21 | return ""; |
---|
22 | |
---|
23 | valeur = valeur.trim(); |
---|
24 | |
---|
25 | if ( valeur.equals("") || valeur.equals("NULL") || valeur.equals("xx")) { |
---|
26 | return ""; |
---|
27 | } |
---|
28 | |
---|
29 | noeud = "<" + tag + ">" + valeur + "</" + tag + ">" + "\n"; |
---|
30 | |
---|
31 | return noeud; |
---|
32 | } |
---|
33 | |
---|
34 | public static String createNode(String tag, Date valeur) { |
---|
35 | if (valeur != null) { |
---|
36 | return createNode(tag, valeur.toString()); |
---|
37 | } |
---|
38 | return ""; |
---|
39 | } |
---|
40 | |
---|
41 | public static String createNode(String tag, int valeur) { |
---|
42 | return createNode(tag, Integer.toString(valeur)); |
---|
43 | } |
---|
44 | |
---|
45 | public static String createNode(String tag, long valeur) { |
---|
46 | return createNode(tag, Long.toString(valeur)); |
---|
47 | } |
---|
48 | |
---|
49 | public static String createNode(String tag, Long valeur) { |
---|
50 | if (valeur != null) { |
---|
51 | return createNode(tag, valeur.toString()); |
---|
52 | } |
---|
53 | return ""; |
---|
54 | } |
---|
55 | |
---|
56 | public static String objectToString(Object objet) { |
---|
57 | String retour = new String(); |
---|
58 | |
---|
59 | if (objet != null) |
---|
60 | retour = objet.toString(); |
---|
61 | |
---|
62 | return retour; |
---|
63 | } |
---|
64 | |
---|
65 | public static String listToString(String nom, Set liste) { |
---|
66 | String retour = new String(); |
---|
67 | |
---|
68 | if (liste.size()==0) |
---|
69 | return ""; |
---|
70 | |
---|
71 | retour += "<" + nom + ">\n"; |
---|
72 | |
---|
73 | for (Object object : liste) { |
---|
74 | retour += object.toString(); |
---|
75 | } |
---|
76 | |
---|
77 | retour += "</" + nom + ">\n"; |
---|
78 | |
---|
79 | return retour; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * fonction pour récupérer juste 1 élement... |
---|
84 | * @param nom |
---|
85 | * @param liste |
---|
86 | * @param index |
---|
87 | * @return |
---|
88 | */ |
---|
89 | public static String listToString(String nom, Set liste, int index) { |
---|
90 | String retour = new String(); |
---|
91 | |
---|
92 | if (liste.size()==0) |
---|
93 | return ""; |
---|
94 | |
---|
95 | Iterator<Object> ite = liste.iterator(); |
---|
96 | retour += ite.next().toString(); |
---|
97 | |
---|
98 | |
---|
99 | return retour; |
---|
100 | } |
---|
101 | |
---|
102 | public static Document getXMLtoDocument(InputStream is_xml, String identifiant) { |
---|
103 | |
---|
104 | Document dom = null; |
---|
105 | InputSource is = null; |
---|
106 | SAXReader reader = new SAXReader(); |
---|
107 | |
---|
108 | class NoDTDEntityResolver implements EntityResolver |
---|
109 | |
---|
110 | { |
---|
111 | public InputSource resolveEntity(String publicID, String systemID) |
---|
112 | { |
---|
113 | return new InputSource(new StringReader("")); |
---|
114 | } |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | try { |
---|
120 | is = new InputSource(is_xml); |
---|
121 | is.setEncoding("ISO-8859-1"); } |
---|
122 | catch (Exception e) { |
---|
123 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, "Erreur de lecture du xml", e); |
---|
124 | return null; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | try { |
---|
129 | |
---|
130 | //Using factory get an instance of document builder |
---|
131 | reader.setEntityResolver(new NoDTDEntityResolver()); |
---|
132 | //parse using builder to get DOM representation of the XML file |
---|
133 | dom = reader.read(is); |
---|
134 | } |
---|
135 | catch(DocumentException etout) { |
---|
136 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, "Document Exception " + identifiant); |
---|
137 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, etout); |
---|
138 | //etout.printStackTrace(); |
---|
139 | return null; |
---|
140 | } |
---|
141 | return dom; |
---|
142 | } |
---|
143 | } |
---|