1 20 package org.jahia.data.xml; 21 22 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.OutputKeys ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerConfigurationException ; 34 import javax.xml.transform.TransformerException ; 35 import javax.xml.transform.TransformerFactory ; 36 import javax.xml.transform.dom.DOMSource ; 37 import javax.xml.transform.stream.StreamResult ; 38 39 import org.jahia.exceptions.JahiaException; 40 import org.jahia.registries.ServicesRegistry; 41 import org.jahia.utils.JahiaConsole; 42 import org.w3c.dom.Document ; 43 import org.xml.sax.EntityResolver ; 44 import org.xml.sax.SAXException ; 45 46 47 53 public abstract class JahiaXmlDocument { 54 55 56 protected Document m_XMLDocument; 58 59 protected String m_DocPath; 60 61 62 private static final String CANT_READ_FILE_MSG = "Can't read XML file"; 63 private static final String ERROR_READING_FILE_MSG = "Error reading file"; 64 private static final String PARAMETER_TAG = "parameter"; 65 private static final String PARAMETER_TAG_NAME_ATTRIBUTE = "name"; 66 67 68 73 public JahiaXmlDocument (String docPath) throws JahiaException 74 { 75 m_DocPath = docPath; 76 78 try { 79 loadFile(m_DocPath); 80 } catch ( Throwable t ){ 81 throw new JahiaException( "JahiaXmlDocument", 82 "Exception while loading to the file" + m_DocPath + t.getMessage(), 83 JahiaException.ERROR_SEVERITY, 84 JahiaException.SERVICE_ERROR, t); 85 } 86 } 87 88 89 95 public JahiaXmlDocument (String docPath, org.xml.sax.helpers.ParserAdapter parser) 96 97 throws JahiaException { 99 100 101 m_DocPath = docPath; 102 104 try { 105 loadFile(m_DocPath); 106 } catch ( Throwable t ){ 107 throw new JahiaException( "JahiaXmlDocument", 108 "Exception while loading to the file" + m_DocPath + t.getMessage(), 109 JahiaException.ERROR_SEVERITY, 110 JahiaException.SERVICE_ERROR, t); 111 } 112 } 113 114 115 118 public abstract void extractDocumentData() throws JahiaException ; 119 120 121 122 126 public void write() throws JahiaException { 127 128 try { 129 saveFile(m_DocPath); 130 } catch ( Throwable t ){ 131 throw new JahiaException( "JahiaXmlDocument", 132 "Exception while writing to the file" + m_DocPath + t.getMessage(), 133 JahiaException.ERROR_SEVERITY, 134 JahiaException.SERVICE_ERROR); 135 } 136 137 } 138 139 140 141 142 private void loadFile(String sourceFileName) 143 throws ParserConfigurationException , IOException , SAXException { 144 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 145 147 EntityResolver et = null; 148 try { 149 et = ServicesRegistry.getInstance().getJahiaWebAppsDeployerService().getDtdEntityResolver(); 150 } catch ( Throwable t ){ 151 JahiaConsole.println("JahiaXmlDocument.loadFile","Entityresolver is null"); 152 } 153 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 154 if ( et != null ){ 155 docBuilder.setEntityResolver(et); 156 } 157 FileInputStream sourceStream = new FileInputStream (sourceFileName); 159 m_XMLDocument = docBuilder.parse(sourceStream); 160 m_XMLDocument.normalize(); 162 } 164 165 166 private void saveFile(String destinationFileName) 167 throws TransformerConfigurationException , FileNotFoundException , 168 TransformerException 169 { 170 171 m_XMLDocument.normalize(); 173 TransformerFactory tfactory = TransformerFactory.newInstance(); 174 175 Transformer serializer = tfactory.newTransformer(); 178 179 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 180 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 181 FileOutputStream fileStream = new FileOutputStream (destinationFileName); 182 183 serializer.transform(new DOMSource (m_XMLDocument), 184 new StreamResult (fileStream)); 185 186 try { 187 fileStream.flush(); 188 fileStream.close(); 189 fileStream = null; 190 } catch ( IOException ioe ) { 191 } 192 } 193 194 195 196 197 198 199 200 201 202 203 204 } | Popular Tags |