Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ReadCustomPropertySets.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.examples.hpsf;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.List;
  19. import org.apache.poi.hpsf.HPSFRuntimeException;
  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. /**
  28. * <p>Sample application showing how to read a document's custom property set.
  29. * Call it with the document's file name as command-line parameter.</p>
  30. *
  31. * <p>Explanations can be found in the HPSF HOW-TO.</p>
  32. */
  33. @SuppressWarnings({"java:S106","java:S4823"})
  34. public final class ReadCustomPropertySets {
  35. private ReadCustomPropertySets() {}
  36. /**
  37. * <p>Runs the example program.</p>
  38. *
  39. * @param args Command-line arguments (unused).
  40. * @throws IOException if any I/O exception occurs.
  41. */
  42. public static void main(final String[] args) throws IOException {
  43. final String filename = args[0];
  44. POIFSReader r = new POIFSReader();
  45. /* Register a listener for *all* documents. */
  46. r.registerListener(ReadCustomPropertySets::processPOIFSReaderEvent);
  47. r.read(new File(filename));
  48. }
  49. public static void processPOIFSReaderEvent(final POIFSReaderEvent event) {
  50. final String streamName = event.getPath() + event.getName();
  51. PropertySet ps;
  52. try {
  53. ps = PropertySetFactory.create(event.getStream());
  54. } catch (NoPropertySetStreamException ex) {
  55. out("No property set stream: \"" + streamName + "\"");
  56. return;
  57. } catch (Exception ex) {
  58. throw new HPSFRuntimeException("Property set stream \"" + streamName + "\": " + ex);
  59. }
  60. /* Print the name of the property set stream: */
  61. out("Property set stream \"" + streamName + "\":");
  62. /* Print the number of sections: */
  63. final long sectionCount = ps.getSectionCount();
  64. out(" No. of sections: " + sectionCount);
  65. /* Print the list of sections: */
  66. List<Section> sections = ps.getSections();
  67. int nr = 0;
  68. for (Section sec : sections) {
  69. /* Print a single section: */
  70. out(" Section " + nr++ + ":");
  71. String s = sec.getFormatID().toString();
  72. s = s.substring(0, s.length() - 1);
  73. out(" Format ID: " + s);
  74. /* Print the number of properties in this section. */
  75. int propertyCount = sec.getPropertyCount();
  76. out(" No. of properties: " + propertyCount);
  77. /* Print the properties: */
  78. Property[] properties = sec.getProperties();
  79. for (Property p : properties) {
  80. /* Print a single property: */
  81. long id = p.getID();
  82. long type = p.getType();
  83. Object value = p.getValue();
  84. out(" Property ID: " + id + ", type: " + type +
  85. ", value: " + value);
  86. }
  87. }
  88. }
  89. private static void out(final String msg) {
  90. System.out.println(msg);
  91. }
  92. }