PageRenderTime 160ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/ppt/poi/org/apache/poi/hpsf/PropertySetFactory.java

https://github.com/minstrelsy/POI-Android
Java | 122 lines | 55 code | 12 blank | 55 comment | 3 complexity | bb0e6127f1776772f4d6b0cc0809e990 MD5 | raw file
  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;
  16. import java.io.InputStream;
  17. import java.io.IOException;
  18. import java.io.UnsupportedEncodingException;
  19. import org.apache.poi.hpsf.wellknown.SectionIDMap;
  20. /**
  21. * <p>Factory class to create instances of {@link SummaryInformation},
  22. * {@link DocumentSummaryInformation} and {@link PropertySet}.</p>
  23. *
  24. * @author Rainer Klute <a
  25. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  26. */
  27. public class PropertySetFactory
  28. {
  29. /**
  30. * <p>Creates the most specific {@link PropertySet} from an {@link
  31. * InputStream}. This is preferrably a {@link
  32. * DocumentSummaryInformation} or a {@link SummaryInformation}. If
  33. * the specified {@link InputStream} does not contain a property
  34. * set stream, an exception is thrown and the {@link InputStream}
  35. * is repositioned at its beginning.</p>
  36. *
  37. * @param stream Contains the property set stream's data.
  38. * @return The created {@link PropertySet}.
  39. * @throws NoPropertySetStreamException if the stream does not
  40. * contain a property set.
  41. * @throws MarkUnsupportedException if the stream does not support
  42. * the <code>mark</code> operation.
  43. * @throws IOException if some I/O problem occurs.
  44. * @exception UnsupportedEncodingException if the specified codepage is not
  45. * supported.
  46. */
  47. public static PropertySet create(final InputStream stream)
  48. throws NoPropertySetStreamException, MarkUnsupportedException,
  49. UnsupportedEncodingException, IOException
  50. {
  51. final PropertySet ps = new PropertySet(stream);
  52. try
  53. {
  54. if (ps.isSummaryInformation())
  55. return new SummaryInformation(ps);
  56. else if (ps.isDocumentSummaryInformation())
  57. return new DocumentSummaryInformation(ps);
  58. else
  59. return ps;
  60. }
  61. catch (UnexpectedPropertySetTypeException ex)
  62. {
  63. /* This exception will never be throws because we already checked
  64. * explicitly for this case above. */
  65. throw new IOException(ex.toString());
  66. }
  67. }
  68. /**
  69. * <p>Creates a new summary information.</p>
  70. *
  71. * @return the new summary information.
  72. */
  73. public static SummaryInformation newSummaryInformation()
  74. {
  75. final MutablePropertySet ps = new MutablePropertySet();
  76. final MutableSection s = (MutableSection) ps.getFirstSection();
  77. s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
  78. try
  79. {
  80. return new SummaryInformation(ps);
  81. }
  82. catch (UnexpectedPropertySetTypeException ex)
  83. {
  84. /* This should never happen. */
  85. throw new HPSFRuntimeException(ex);
  86. }
  87. }
  88. /**
  89. * <p>Creates a new document summary information.</p>
  90. *
  91. * @return the new document summary information.
  92. */
  93. public static DocumentSummaryInformation newDocumentSummaryInformation()
  94. {
  95. final MutablePropertySet ps = new MutablePropertySet();
  96. final MutableSection s = (MutableSection) ps.getFirstSection();
  97. s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
  98. try
  99. {
  100. return new DocumentSummaryInformation(ps);
  101. }
  102. catch (UnexpectedPropertySetTypeException ex)
  103. {
  104. /* This should never happen. */
  105. throw new HPSFRuntimeException(ex);
  106. }
  107. }
  108. }