[68] | 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 | |
---|
[70] | 65 | public static String listToString(String nom, Set<?> liste) { |
---|
[68] | 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 | */ |
---|
[70] | 89 | public static String listToString(String nom, Set<?> liste, int index) { |
---|
[68] | 90 | String retour = new String(); |
---|
| 91 | |
---|
| 92 | if (liste.size()==0) |
---|
| 93 | return ""; |
---|
| 94 | |
---|
[70] | 95 | Iterator<?> ite = liste.iterator(); |
---|
[68] | 96 | retour += ite.next().toString(); |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | return retour; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public static Document getXMLtoDocument(InputStream is_xml, String identifiant) { |
---|
[70] | 103 | |
---|
| 104 | // LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, "LECTURE XML"); |
---|
[68] | 105 | Document dom = null; |
---|
| 106 | InputSource is = null; |
---|
| 107 | SAXReader reader = new SAXReader(); |
---|
| 108 | |
---|
| 109 | class NoDTDEntityResolver implements EntityResolver |
---|
| 110 | |
---|
| 111 | { |
---|
| 112 | public InputSource resolveEntity(String publicID, String systemID) |
---|
| 113 | { |
---|
| 114 | return new InputSource(new StringReader("")); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | try { |
---|
| 121 | is = new InputSource(is_xml); |
---|
| 122 | is.setEncoding("ISO-8859-1"); } |
---|
| 123 | catch (Exception e) { |
---|
| 124 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, "Erreur de lecture du xml", e); |
---|
| 125 | return null; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | try { |
---|
| 130 | |
---|
| 131 | //Using factory get an instance of document builder |
---|
| 132 | reader.setEntityResolver(new NoDTDEntityResolver()); |
---|
| 133 | //parse using builder to get DOM representation of the XML file |
---|
| 134 | dom = reader.read(is); |
---|
| 135 | } |
---|
| 136 | catch(DocumentException etout) { |
---|
| 137 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, "Document Exception " + identifiant); |
---|
| 138 | LoggerPerso.log(XmlHelper.class, LoggerPerso.WARN, etout); |
---|
| 139 | //etout.printStackTrace(); |
---|
| 140 | return null; |
---|
| 141 | } |
---|
| 142 | return dom; |
---|
| 143 | } |
---|
| 144 | } |
---|