View Javadoc

1   package es.inteco.eni.xml;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Map.Entry;
9   
10  import javax.xml.parsers.DocumentBuilder;
11  import javax.xml.parsers.DocumentBuilderFactory;
12  import javax.xml.parsers.ParserConfigurationException;
13  import javax.xml.transform.OutputKeys;
14  import javax.xml.transform.Source;
15  import javax.xml.transform.Transformer;
16  import javax.xml.transform.TransformerException;
17  import javax.xml.transform.TransformerFactory;
18  import javax.xml.transform.dom.DOMSource;
19  import javax.xml.transform.stream.StreamResult;
20  import javax.xml.transform.stream.StreamSource;
21  
22  import org.w3c.dom.Attr;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.xml.sax.SAXException;
26  
27  import es.inteco.eni.commons.ApplicationPropertiesManager;
28  import es.inteco.eni.utils.FileUtils;
29  
30  /**
31   * Clase de utilidad para parsear los XML para ENI
32   * 
33   * 
34   *
35   */
36  public final class ENIXmlUtils {
37  
38  	/**
39  	 * Cadena para indetar el xml
40  	 */
41  	private static final String INDENT_AMOUNT = "{http://xml.apache.org/xslt}indent-amount";
42  	
43  
44  	/**
45  	 * Constructor privado, no se instancia
46  	 */
47  	private ENIXmlUtils(){
48  
49  	}
50  
51  	/**
52  	 * Validación de un XML a partir de un conjunto de esquemas XSD
53  	 * @param xmlContent Contenido XML
54  	 * @param filePath Ruta del fichero
55  	 * @return Excepción al validar el XML
56  	 * @throws IOException 
57  	 * @throws SAXException 
58  	 */
59  	public static ValidationXmlResult validateWithExtXSD(String xmlContent, String filePath) throws IOException{
60  		return XmlUtils.validateWithExtXSD(xmlContent, filePath);
61  	}
62  
63  	/**
64  	 * Devuelve un fichero con el xml representado por el nodo y los hijos pasados por parámetro 
65  	 * @param extension Extension del fichero
66  	 * @param xmlFileContentRoot Nodo del XML
67  	 * @return Un fichero con el xml representado por el nodo y los hijos pasados por parámetro 
68  	 * @throws ParserConfigurationException
69  	 * @throws IOException
70  	 * @throws TransformerException
71  	 */
72  	public static File getXmlFile(String extension, XmlNode xmlFileContentRoot) throws ParserConfigurationException, IOException, TransformerException {
73  		File xmlFile = null;
74  
75  		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
76  		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
77  
78  		//Root
79  		Document document = docBuilder.newDocument();
80  		Element rootElement= getElement(xmlFileContentRoot, document);
81  		document.appendChild(rootElement);
82  
83  		addXmlChilds(xmlFileContentRoot.getChilds(), document, rootElement);
84  
85  
86  		//Escribe el contenido en un ficho
87  		TransformerFactory transformerFactory = TransformerFactory.newInstance();
88  		Transformer transformer = transformerFactory.newTransformer();
89  		//Indenta
90  		transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
91  		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
92  		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
93  		transformer.setOutputProperty(INDENT_AMOUNT, "2");
94  		DOMSource source = new DOMSource(document);
95  		xmlFile = File.createTempFile(System.nanoTime()+"", extension==null ? "" : "."+extension);
96  		StreamResult result =  new StreamResult(xmlFile);
97  		transformer.transform(source, result);
98  
99  
100 		return xmlFile;
101 	} 
102 
103 
104 	private static void addXmlChilds(Map<String, XmlNode> xmlNodes, Document document, Element parent){
105 		Iterator<Entry<String, XmlNode>> it = xmlNodes.entrySet().iterator();
106 		while(it.hasNext()){
107 			Entry<String, XmlNode> entry = it.next();
108 			XmlNode xmlNode = entry.getValue();
109 			Element element = getElement(xmlNode, document);
110 			parent.appendChild(element);
111 			addXmlChilds(xmlNode.getChilds(), document, element);
112 		}
113 	}
114 
115 	private static Element getElement(XmlNode xmlNode, Document document){
116 		Element element = document.createElement(xmlNode.getlName());
117 
118 		for (int i=0; xmlNode.getAttributes()!=null && i<xmlNode.getAttributes().getLength(); i++){
119 			String lName = xmlNode.getAttributes().getLocalName(i);
120 			String value = xmlNode.getAttributes().getValue(i);
121 			Attr attr = document.createAttribute(lName);
122 			attr.setValue(value);
123 			element.setAttributeNode(attr);
124 		}
125 		if(xmlNode.getValue()!=null){
126 			element.setTextContent(xmlNode.getValue().trim());
127 		}
128 		return element;
129 	}
130 
131 }