1 25 package com.catcode.odf; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.w3c.dom.Node ; 31 32 import java.io.File ; 33 import java.io.InputStream ; 34 import java.io.IOException ; 35 36 import java.lang.reflect.Method ; 37 import java.lang.reflect.InvocationTargetException ; 38 39 import java.util.zip.ZipFile ; 40 import java.util.zip.ZipEntry ; 41 import java.util.zip.ZipInputStream ; 42 43 import javax.xml.parsers.DocumentBuilder ; 44 import javax.xml.parsers.DocumentBuilderFactory ; 45 46 import com.catcode.odf.OpenDocumentMetadata; 47 48 62 public class ODFMetaFileAnalyzer 63 { 64 protected String officeNamespace; 65 protected String dcNamespace; 66 protected String metaNamespace; 67 68 private static final String OPENDOCUMENT_URI = 69 "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; 70 private static final String DC_URI = 71 "http://purl.org/dc/elements/1.1/"; 72 private static final String META_URI = 73 "urn:oasis:names:tc:opendocument:xmlns:meta:1.0"; 74 private static final String STATISTICS= 75 "document-statistic"; 76 private static final String USER_DEFINED= 77 "user-defined"; 78 79 private static Class metaDataClass = OpenDocumentMetadata.class; 80 private static Class [] stringParameter = {String .class}; 81 private static Class [] intParameter = {int.class}; 82 83 84 111 public OpenDocumentMetadata analyzeMetaData( InputStream metaStream ) 112 { 113 DocumentBuilder builder; 114 Document doc; 115 Node metaElement; 116 OpenDocumentMetadata metaDataResult; 117 118 try 119 { 120 metaDataResult = new OpenDocumentMetadata(); 121 builder = 122 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 123 doc = builder.parse( metaStream ); 124 findNamespaces( doc.getDocumentElement() ); 125 metaElement = doc.getElementsByTagName( 126 officeNamespace + "meta").item(0); 127 if (metaElement != null) 128 { 129 metaElement = metaElement.getFirstChild(); 130 while (metaElement != null) 131 { 132 if (metaElement.getNodeType() == Node.ELEMENT_NODE) 133 { 134 String name = metaElement.getNodeName(); 135 if (name.equals(metaNamespace + STATISTICS)) 136 { 137 processStatistic( (Element ) metaElement, 138 metaDataResult ); 139 } 140 else if (name.equals( metaNamespace + USER_DEFINED)) 141 { 142 processUserDefined( (Element ) metaElement, 143 metaDataResult ); 144 } 145 else 146 { 147 processElement( (Element ) metaElement, 148 metaDataResult ); 149 } 150 } 151 metaElement = metaElement.getNextSibling(); 152 } 153 } 154 } 155 catch (Exception e) 156 { 157 metaDataResult = null; 158 } 159 160 return metaDataResult; 161 } 162 163 172 public OpenDocumentMetadata analyzeZip( InputStream inputStream ) 173 { 174 ZipInputStream zipStream = new ZipInputStream ( inputStream ); 175 OpenDocumentMetadata metaDataResult = null; 176 177 try 178 { 179 while (zipStream.available() == 1) 180 { 181 ZipEntry metaEntry = zipStream.getNextEntry(); 183 if (metaEntry != null && "meta.xml".equals(metaEntry.getName())) 184 { 185 metaDataResult = analyzeMetaData( zipStream ); 187 break; 189 } 190 } 191 } 192 catch (IOException ioe) 193 { 194 } 196 finally 197 { 198 try { 199 zipStream.close(); 201 } 202 catch (IOException ioe) { 203 } 205 } 206 return metaDataResult; 207 } 208 209 217 public OpenDocumentMetadata analyzeZip( File inputFile ) 218 { 219 ZipFile zipFile; 220 ZipEntry metaEntry; 221 InputStream metaStream = null; 222 OpenDocumentMetadata metaDataResult = null; 223 224 try 225 { 226 zipFile = new ZipFile ( inputFile ); 227 metaEntry = zipFile.getEntry("meta.xml"); 228 if (metaEntry != null) 229 { 230 metaStream = zipFile.getInputStream(metaEntry); 231 } 232 } 233 catch (IOException e) 234 { 235 metaStream = null; 236 } 237 if (metaStream != null) 238 { 239 metaDataResult = analyzeMetaData( metaStream ); 240 } 241 return metaDataResult; 242 } 243 244 260 protected void processElement( Element element, OpenDocumentMetadata 261 metaDataResult ) 262 { 263 String elementContent; 264 String [] theParameter = new String [1]; 265 Node textChild; 266 String methodName = makeSetMethodName( getBaseName(element) ); 267 try 268 { 269 Method setMethod = metaDataClass.getDeclaredMethod( 270 methodName, stringParameter); 271 if (setMethod != null) 272 { 273 textChild = element.getFirstChild(); 274 theParameter[0] = (textChild != null) 275 ? textChild.getNodeValue().trim() : ""; 276 setMethod.invoke( metaDataResult, theParameter ); 277 } 278 } 279 catch (InvocationTargetException e) 280 { 281 } 283 catch (IllegalAccessException e) 284 { 285 } 287 catch (NoSuchMethodException e) 288 { 289 } 291 } 292 293 308 protected void processStatistic( Element element, OpenDocumentMetadata 309 metaDataResult ) 310 { 311 String attrValue; 312 String attrName; 313 String methodName; 314 Integer [] theParameter = new Integer [1]; 315 NamedNodeMap attr; 316 attr = element.getAttributes(); 317 for (int i=0; i < attr.getLength(); i++) 318 { 319 try 320 { 321 attrName = getBaseName( attr.item(i) ); 322 methodName = makeSetMethodName( attrName ); 323 Method setMethod = metaDataClass.getDeclaredMethod( 324 methodName, intParameter); 325 if (setMethod != null) 326 { 327 attrValue = attr.item(i).getNodeValue(); 328 theParameter[0] = (attrValue != null) 329 ? Integer.valueOf( attrValue ) : new Integer (0); 330 setMethod.invoke( metaDataResult, theParameter ); 331 } 332 } 333 catch (InvocationTargetException e) 334 { 335 } 337 catch (IllegalAccessException e) 338 { 339 } 341 catch (NoSuchMethodException e) 342 { 343 } 345 } 346 347 } 348 349 361 protected void processUserDefined( Element element, OpenDocumentMetadata 362 metaDataResult ) 363 { 364 String dataType; 365 String content; 366 String key; 367 368 if (element.hasChildNodes()) 369 { 370 content = element.getFirstChild().getNodeValue(); 371 dataType = element.getAttribute( metaNamespace + "value-type" ); 372 dataType = (dataType.equals("")) ? "string" : dataType; 373 374 key = element.getAttribute( metaNamespace + "name" ); 375 if (key != "") 376 { 377 if (dataType == "string" || dataType == "date") 378 { 379 metaDataResult.setUserDefined( key, content ); 380 } 381 else if (dataType == "float") 382 { 383 metaDataResult.setUserDefined( key, 384 Double.valueOf( content ) ); 385 } 386 else if (dataType == "boolean") 387 { 388 metaDataResult.setUserDefined( key, 389 Boolean.valueOf( content ) ); 390 } 391 else if (dataType == "time") 392 { 393 metaDataResult.setUserDefined( key, 394 Duration.parseDuration( content ) ); 395 } 396 } 397 } 398 } 399 400 407 public static OpenDocumentMetadata analyzeFile( File inputFile ) 408 { 409 ODFMetaFileAnalyzer mfa = new ODFMetaFileAnalyzer(); 410 return mfa.analyzeZip( inputFile ); 411 } 412 413 422 protected void findNamespaces( Element rootElement ) 423 { 424 NamedNodeMap attributes; 425 Node node; 426 String value; 427 428 attributes = rootElement.getAttributes(); 429 for (int i=0; i < attributes.getLength(); i++) 430 { 431 node = attributes.item(i); 432 value = node.getNodeValue(); 433 434 if (value.equals( DC_URI )) 435 { 436 dcNamespace = extractNamespace( node.getNodeName() ); 437 } 438 else if (value.equals( META_URI )) 439 { 440 metaNamespace = extractNamespace( node.getNodeName() ); 441 } 442 else if (value.equals( OPENDOCUMENT_URI )) 443 { 444 officeNamespace = extractNamespace( node.getNodeName() ); 445 } 446 } 447 } 448 449 455 protected String extractNamespace( String namespaceAttrName ) 456 { 457 String result; 458 int pos = namespaceAttrName.indexOf(":"); 459 460 result = (pos > 0) 461 ? namespaceAttrName.substring( pos + 1 ) + ":" 462 : ""; 463 return result; 464 } 465 466 471 protected String getBaseName( Node node ) 472 { 473 String result = node.getNodeName(); 474 int pos = result.indexOf(":"); 475 if (pos >= 0) 476 { 477 result = result.substring( pos + 1 ); 478 } 479 return result; 480 } 481 482 494 protected String makeSetMethodName( String elementName ) 495 { 496 String [] part; 497 String result; 498 int i; 499 part = elementName.split("-"); 500 result = "set"; 501 for (i=0; i<part.length; i++) 502 { 503 result += part[i].substring(0,1).toUpperCase() + 504 part[i].substring(1); 505 } 506 return result; 507 } 508 509 } 510 | Popular Tags |