You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HPSFPropertiesExtractor.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hpsf.extractor;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.Iterator;
  21. import org.apache.poi.POIDocument;
  22. import org.apache.poi.POITextExtractor;
  23. import org.apache.poi.hpsf.CustomProperties;
  24. import org.apache.poi.hpsf.DocumentSummaryInformation;
  25. import org.apache.poi.hpsf.Property;
  26. import org.apache.poi.hpsf.SpecialPropertySet;
  27. import org.apache.poi.hpsf.SummaryInformation;
  28. import org.apache.poi.hpsf.wellknown.PropertyIDMap;
  29. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  30. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  31. import org.apache.poi.util.LittleEndian;
  32. /**
  33. * Extracts all of the HPSF properties, both
  34. * build in and custom, returning them in
  35. * textual form.
  36. */
  37. public class HPSFPropertiesExtractor extends POITextExtractor {
  38. public HPSFPropertiesExtractor(POITextExtractor mainExtractor) {
  39. super(mainExtractor);
  40. }
  41. public HPSFPropertiesExtractor(POIDocument doc) {
  42. super(doc);
  43. }
  44. public HPSFPropertiesExtractor(POIFSFileSystem fs) {
  45. super(new PropertiesOnlyDocument(fs));
  46. }
  47. public HPSFPropertiesExtractor(NPOIFSFileSystem fs) {
  48. super(new PropertiesOnlyDocument(fs));
  49. }
  50. public String getDocumentSummaryInformationText() {
  51. DocumentSummaryInformation dsi = document.getDocumentSummaryInformation();
  52. StringBuffer text = new StringBuffer();
  53. // Normal properties
  54. text.append( getPropertiesText(dsi) );
  55. // Now custom ones
  56. CustomProperties cps = dsi == null ? null : dsi.getCustomProperties();
  57. if(cps != null) {
  58. Iterator<String> keys = cps.nameSet().iterator();
  59. while(keys.hasNext()) {
  60. String key = keys.next();
  61. String val = getPropertyValueText( cps.get(key) );
  62. text.append(key + " = " + val + "\n");
  63. }
  64. }
  65. // All done
  66. return text.toString();
  67. }
  68. public String getSummaryInformationText() {
  69. SummaryInformation si = document.getSummaryInformation();
  70. // Just normal properties
  71. return getPropertiesText(si);
  72. }
  73. private static String getPropertiesText(SpecialPropertySet ps) {
  74. if(ps == null) {
  75. // Not defined, oh well
  76. return "";
  77. }
  78. StringBuffer text = new StringBuffer();
  79. PropertyIDMap idMap = ps.getPropertySetIDMap();
  80. Property[] props = ps.getProperties();
  81. for(int i=0; i<props.length; i++) {
  82. String type = Long.toString( props[i].getID() );
  83. Object typeObj = idMap.get(props[i].getID());
  84. if(typeObj != null) {
  85. type = typeObj.toString();
  86. }
  87. String val = getPropertyValueText( props[i].getValue() );
  88. text.append(type + " = " + val + "\n");
  89. }
  90. return text.toString();
  91. }
  92. private static String getPropertyValueText(Object val) {
  93. if(val == null) {
  94. return "(not set)";
  95. }
  96. if(val instanceof byte[]) {
  97. byte[] b = (byte[])val;
  98. if(b.length == 0) {
  99. return "";
  100. }
  101. if(b.length == 1) {
  102. return Byte.toString(b[0]);
  103. }
  104. if(b.length == 2) {
  105. return Integer.toString( LittleEndian.getUShort(b) );
  106. }
  107. if(b.length == 4) {
  108. return Long.toString( LittleEndian.getUInt(b) );
  109. }
  110. // Maybe it's a string? who knows!
  111. return new String(b);
  112. }
  113. return val.toString();
  114. }
  115. /**
  116. * @return the text of all the properties defined in
  117. * the document.
  118. */
  119. public String getText() {
  120. return getSummaryInformationText() + getDocumentSummaryInformationText();
  121. }
  122. /**
  123. * Prevent recursion!
  124. */
  125. public POITextExtractor getMetadataTextExtractor() {
  126. throw new IllegalStateException("You already have the Metadata Text Extractor, not recursing!");
  127. }
  128. /**
  129. * So we can get at the properties of any
  130. * random OLE2 document.
  131. */
  132. private static final class PropertiesOnlyDocument extends POIDocument {
  133. public PropertiesOnlyDocument(NPOIFSFileSystem fs) {
  134. super(fs.getRoot());
  135. }
  136. public PropertiesOnlyDocument(POIFSFileSystem fs) {
  137. super(fs);
  138. }
  139. public void write(OutputStream out) {
  140. throw new IllegalStateException("Unable to write, only for properties!");
  141. }
  142. }
  143. public static void main(String[] args) throws IOException {
  144. for(String file : args) {
  145. HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(
  146. new NPOIFSFileSystem(new File(file))
  147. );
  148. System.out.println(ext.getText());
  149. }
  150. }
  151. }