PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hpsf/PropertySetFactory.java

http://github.com/openmicroscopy/bioformats
Java | 148 lines | 56 code | 13 blank | 79 comment | 3 complexity | 2dd1096c5bed6b6135df6d36350a80c1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hpsf;
  38. import java.io.InputStream;
  39. import java.io.IOException;
  40. import java.io.UnsupportedEncodingException;
  41. import java.rmi.UnexpectedException;
  42. import loci.poi.hpsf.wellknown.SectionIDMap;
  43. /**
  44. * <p>Factory class to create instances of {@link SummaryInformation},
  45. * {@link DocumentSummaryInformation} and {@link PropertySet}.</p>
  46. *
  47. * @author Rainer Klute <a
  48. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  49. * @version $Id: PropertySetFactory.java 489730 2006-12-22 19:18:16Z bayard $
  50. * @since 2002-02-09
  51. */
  52. public class PropertySetFactory
  53. {
  54. /**
  55. * <p>Creates the most specific {@link PropertySet} from an {@link
  56. * InputStream}. This is preferrably a {@link
  57. * DocumentSummaryInformation} or a {@link SummaryInformation}. If
  58. * the specified {@link InputStream} does not contain a property
  59. * set stream, an exception is thrown and the {@link InputStream}
  60. * is repositioned at its beginning.</p>
  61. *
  62. * @param stream Contains the property set stream's data.
  63. * @return The created {@link PropertySet}.
  64. * @throws NoPropertySetStreamException if the stream does not
  65. * contain a property set.
  66. * @throws MarkUnsupportedException if the stream does not support
  67. * the <code>mark</code> operation.
  68. * @throws IOException if some I/O problem occurs.
  69. * @exception UnsupportedEncodingException if the specified codepage is not
  70. * supported.
  71. */
  72. public static PropertySet create(final InputStream stream)
  73. throws NoPropertySetStreamException, MarkUnsupportedException,
  74. UnsupportedEncodingException, IOException
  75. {
  76. final PropertySet ps = new PropertySet(stream);
  77. try
  78. {
  79. if (ps.isSummaryInformation())
  80. return new SummaryInformation(ps);
  81. else if (ps.isDocumentSummaryInformation())
  82. return new DocumentSummaryInformation(ps);
  83. else
  84. return ps;
  85. }
  86. catch (UnexpectedPropertySetTypeException ex)
  87. {
  88. /* This exception will never be throws because we already checked
  89. * explicitly for this case above. */
  90. throw new UnexpectedException(ex.toString());
  91. }
  92. }
  93. /**
  94. * <p>Creates a new summary information.</p>
  95. *
  96. * @return the new summary information.
  97. */
  98. public static SummaryInformation newSummaryInformation()
  99. {
  100. final MutablePropertySet ps = new MutablePropertySet();
  101. final MutableSection s = (MutableSection) ps.getFirstSection();
  102. s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
  103. try
  104. {
  105. return new SummaryInformation(ps);
  106. }
  107. catch (UnexpectedPropertySetTypeException ex)
  108. {
  109. /* This should never happen. */
  110. throw new HPSFRuntimeException(ex);
  111. }
  112. }
  113. /**
  114. * <p>Creates a new document summary information.</p>
  115. *
  116. * @return the new document summary information.
  117. */
  118. public static DocumentSummaryInformation newDocumentSummaryInformation()
  119. {
  120. final MutablePropertySet ps = new MutablePropertySet();
  121. final MutableSection s = (MutableSection) ps.getFirstSection();
  122. s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
  123. try
  124. {
  125. return new DocumentSummaryInformation(ps);
  126. }
  127. catch (UnexpectedPropertySetTypeException ex)
  128. {
  129. /* This should never happen. */
  130. throw new HPSFRuntimeException(ex);
  131. }
  132. }
  133. }