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.

ReadCustomPropertySets.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.examples;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import org.apache.poi.hpsf.NoPropertySetStreamException;
  21. import org.apache.poi.hpsf.Property;
  22. import org.apache.poi.hpsf.PropertySet;
  23. import org.apache.poi.hpsf.PropertySetFactory;
  24. import org.apache.poi.hpsf.Section;
  25. import org.apache.poi.poifs.eventfilesystem.POIFSReader;
  26. import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
  27. import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
  28. import org.apache.poi.util.HexDump;
  29. /**
  30. * <p>Sample application showing how to read a document's custom property set.
  31. * Call it with the document's file name as command-line parameter.</p>
  32. *
  33. * <p>Explanations can be found in the HPSF HOW-TO.</p>
  34. *
  35. * @author Rainer Klute <a
  36. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  37. */
  38. public class ReadCustomPropertySets
  39. {
  40. /**
  41. * <p>Runs the example program.</p>
  42. *
  43. * @param args Command-line arguments (unused).
  44. * @throws IOException if any I/O exception occurs.
  45. */
  46. public static void main(final String[] args)
  47. throws IOException
  48. {
  49. final String filename = args[0];
  50. POIFSReader r = new POIFSReader();
  51. /* Register a listener for *all* documents. */
  52. r.registerListener(new MyPOIFSReaderListener());
  53. r.read(new FileInputStream(filename));
  54. }
  55. static class MyPOIFSReaderListener implements POIFSReaderListener
  56. {
  57. public void processPOIFSReaderEvent(final POIFSReaderEvent event)
  58. {
  59. PropertySet ps = null;
  60. try
  61. {
  62. ps = PropertySetFactory.create(event.getStream());
  63. }
  64. catch (NoPropertySetStreamException ex)
  65. {
  66. out("No property set stream: \"" + event.getPath() +
  67. event.getName() + "\"");
  68. return;
  69. }
  70. catch (Exception ex)
  71. {
  72. throw new RuntimeException
  73. ("Property set stream \"" +
  74. event.getPath() + event.getName() + "\": " + ex);
  75. }
  76. /* Print the name of the property set stream: */
  77. out("Property set stream \"" + event.getPath() +
  78. event.getName() + "\":");
  79. /* Print the number of sections: */
  80. final long sectionCount = ps.getSectionCount();
  81. out(" No. of sections: " + sectionCount);
  82. /* Print the list of sections: */
  83. List<Section> sections = ps.getSections();
  84. int nr = 0;
  85. for (Iterator<Section> i = sections.iterator(); i.hasNext();)
  86. {
  87. /* Print a single section: */
  88. Section sec = i.next();
  89. out(" Section " + nr++ + ":");
  90. String s = hex(sec.getFormatID().getBytes());
  91. s = s.substring(0, s.length() - 1);
  92. out(" Format ID: " + s);
  93. /* Print the number of properties in this section. */
  94. int propertyCount = sec.getPropertyCount();
  95. out(" No. of properties: " + propertyCount);
  96. /* Print the properties: */
  97. Property[] properties = sec.getProperties();
  98. for (int i2 = 0; i2 < properties.length; i2++)
  99. {
  100. /* Print a single property: */
  101. Property p = properties[i2];
  102. long id = p.getID();
  103. long type = p.getType();
  104. Object value = p.getValue();
  105. out(" Property ID: " + id + ", type: " + type +
  106. ", value: " + value);
  107. }
  108. }
  109. }
  110. }
  111. static void out(final String msg)
  112. {
  113. System.out.println(msg);
  114. }
  115. static String hex(final byte[] bytes)
  116. {
  117. return HexDump.dump(bytes, 0L, 0);
  118. }
  119. }