1 20 package org.jahia.tools.files; 21 22 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.util.Properties ; 28 import java.util.Vector ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 import javax.xml.transform.OutputKeys ; 34 35 import org.jahia.utils.JahiaConsole; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.Element ; 38 import org.w3c.dom.Node ; 39 import org.w3c.dom.NodeList ; 40 41 42 57 public class MimeTypesFromWebAppXmlFile { 58 59 private static final String CLASS_NAME = MimeTypesFromWebAppXmlFile.class.getName(); 60 61 62 protected Document m_XMLDocument; 63 64 protected String m_DocPath; 65 66 protected org.xml.sax.EntityResolver m_Resolver; 67 68 69 private static final String CANT_READ_FILE_MSG = "Can't read XML file"; 70 private static final String ERROR_READING_FILE_MSG = "Error reading file"; 71 72 private static final String WEB_APP_TAG = "web-app"; 73 private static final String MIME_MAPPING_TAG = "mime-mapping"; 74 private static final String EXTENSION_TAG = "extension"; 75 private static final String MIME_TYPE_TAG = "mime-type"; 76 77 private Properties m_MimeTypes = new Properties (); 78 79 85 public MimeTypesFromWebAppXmlFile (String docPath) 86 throws Exception { 87 m_DocPath = docPath; 88 89 try { 90 loadFile(m_DocPath); 91 } catch ( Throwable t ){ 92 throw new Exception ( CLASS_NAME 93 + ", Exception while loading to the file" 94 + m_DocPath + "\n" 95 + t.getMessage() ); 96 } 97 } 98 99 106 public MimeTypesFromWebAppXmlFile (String docPath, org.xml.sax.EntityResolver entityResolver) 107 throws Exception { 108 109 m_Resolver = entityResolver; 110 m_DocPath = docPath; 111 112 try { 113 loadFile(m_DocPath); 114 } catch ( Throwable t ){ 115 throw new Exception ( CLASS_NAME 116 + ", Exception while loading to the file" 117 + m_DocPath + "\n" 118 + t.getMessage() ); 119 } 120 } 121 122 129 public String getMimeTypeFromFilename (String filename){ 130 131 if ( (m_MimeTypes == null) 132 || (filename == null) 133 || (filename.lastIndexOf(".") == -1) ) 134 return ""; 135 136 String mimeType = ""; 137 String ext = filename.substring( filename.lastIndexOf(".") + 1, 138 filename.length()); 139 140 return getMimeTypeFromExt(ext); 141 } 142 143 150 public String getMimeTypeFromExt (String extension){ 151 152 if ( (m_MimeTypes == null) 153 || (extension == null) ) 154 return ""; 155 156 String mimeType = ""; 157 158 mimeType = m_MimeTypes.getProperty(extension.toLowerCase()); 159 if ( mimeType == null ) 160 mimeType = ""; 161 162 return mimeType; 163 } 164 165 166 172 public Properties getMimeTypes (){ 173 174 return (Properties )m_MimeTypes.clone(); 175 } 176 177 178 182 public void extractDocumentData() throws Exception { 183 184 if (m_XMLDocument == null) { 185 186 throw new Exception ( CLASS_NAME + ", web.xml document is null" ); 187 } 188 189 if (!m_XMLDocument.hasChildNodes()) { 190 191 throw new Exception ( CLASS_NAME + 192 ", Main document node has no children" ); 193 194 } 195 196 Element webAppNode; 198 webAppNode = m_XMLDocument.getDocumentElement(); 199 200 201 if (!webAppNode.getNodeName().equalsIgnoreCase(WEB_APP_TAG)) { 202 203 throw new Exception ( CLASS_NAME + 204 ", web-app tag is not present as starting tag in file" ); 205 } 206 207 Vector nodesList = getChildNodes(webAppNode,MIME_MAPPING_TAG); 209 int size = nodesList.size(); 210 if ( size>0 ){ 211 212 Node nodeItem = null; 213 String extension = ""; 214 String mimeType = ""; 215 216 Node currNode = null; 217 218 for ( int i=0 ; i<size ; i++ ){ 219 nodeItem = (Node )nodesList.get(i); 220 221 currNode = nextChildOfTag(nodeItem,EXTENSION_TAG); 222 if (currNode != null ){ 223 extension = currNode.getFirstChild().getNodeValue().trim(); 224 } 225 226 currNode = nextChildOfTag(nodeItem,MIME_TYPE_TAG); 227 if (currNode != null ){ 228 mimeType = currNode.getFirstChild().getNodeValue().trim(); 229 } 230 231 if ( extension != null && mimeType != null ){ 232 m_MimeTypes.setProperty(extension.toLowerCase(),mimeType); 233 } 235 } 236 } 237 } 238 239 private void loadFile(String sourceFileName) 241 throws ParserConfigurationException , Exception , IOException , org.xml.sax.SAXException { 242 243 JahiaConsole.println(CLASS_NAME+".loadFile","sourceFileName=" + sourceFileName); 244 245 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 246 248 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 249 if ( m_Resolver != null ){ 250 docBuilder.setEntityResolver(m_Resolver); 251 } 252 FileInputStream sourceStream = new FileInputStream (sourceFileName); 253 m_XMLDocument = docBuilder.parse(sourceStream); 254 m_XMLDocument.normalize(); 256 extractDocumentData (); 257 } 258 259 private void saveFile(String destinationFileName) 261 throws javax.xml.transform.TransformerConfigurationException , FileNotFoundException , 262 javax.xml.transform.TransformerException 263 { 264 265 m_XMLDocument.normalize(); 267 javax.xml.transform.TransformerFactory tfactory = javax.xml.transform.TransformerFactory.newInstance(); 268 269 javax.xml.transform.Transformer serializer = tfactory.newTransformer(); 272 273 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 274 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 275 FileOutputStream fileStream = new FileOutputStream (destinationFileName); 276 277 serializer.transform(new javax.xml.transform.dom.DOMSource (m_XMLDocument), 278 new javax.xml.transform.stream.StreamResult (fileStream)); 279 280 try { 281 fileStream.flush(); 282 fileStream.close(); 283 fileStream = null; 284 } catch ( IOException ioe ) { 285 } 286 } 287 288 297 private Vector getChildNodes( Node parentNode, 298 String tagName 299 ) throws Exception { 300 301 Vector childs = new Vector (); 302 303 NodeList nodeList = parentNode.getChildNodes(); 304 305 if ( nodeList != null ) { 306 307 int size = nodeList.getLength(); 308 for ( int i=0; i<size ; i++ ){ 309 Node nodeItem = null; 310 nodeItem = nodeList.item(i); 311 314 if ( nodeItem.getNodeName().equalsIgnoreCase(tagName) ){ 315 childs.add(nodeItem); 316 } 317 } 318 } 319 320 return childs; 321 } 322 323 333 private Node nextChildOfTag( Node startNode, 334 String tagName 335 ) throws Exception { 336 337 340 341 Vector childs = getChildNodes(startNode,tagName); 342 int size = childs.size(); 343 for ( int i=0 ; i<size; i++ ){ 344 Node child = (Node )childs.get(i); 345 if (child.getNodeName().equalsIgnoreCase(tagName)){ 346 349 return child; 350 } 351 } 352 353 return null; 354 } 355 356 357 358 } | Popular Tags |