001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf;
019:
020: import java.io.InputStream;
021: import java.io.IOException;
022: import java.io.UnsupportedEncodingException;
023: import java.rmi.UnexpectedException;
024:
025: import org.apache.poi.hpsf.wellknown.SectionIDMap;
026:
027: /**
028: * <p>Factory class to create instances of {@link SummaryInformation},
029: * {@link DocumentSummaryInformation} and {@link PropertySet}.</p>
030: *
031: * @author Rainer Klute <a
032: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
033: * @version $Id: PropertySetFactory.java 489730 2006-12-22 19:18:16Z bayard $
034: * @since 2002-02-09
035: */
036: public class PropertySetFactory {
037:
038: /**
039: * <p>Creates the most specific {@link PropertySet} from an {@link
040: * InputStream}. This is preferrably a {@link
041: * DocumentSummaryInformation} or a {@link SummaryInformation}. If
042: * the specified {@link InputStream} does not contain a property
043: * set stream, an exception is thrown and the {@link InputStream}
044: * is repositioned at its beginning.</p>
045: *
046: * @param stream Contains the property set stream's data.
047: * @return The created {@link PropertySet}.
048: * @throws NoPropertySetStreamException if the stream does not
049: * contain a property set.
050: * @throws MarkUnsupportedException if the stream does not support
051: * the <code>mark</code> operation.
052: * @throws IOException if some I/O problem occurs.
053: * @exception UnsupportedEncodingException if the specified codepage is not
054: * supported.
055: */
056: public static PropertySet create(final InputStream stream)
057: throws NoPropertySetStreamException,
058: MarkUnsupportedException, UnsupportedEncodingException,
059: IOException {
060: final PropertySet ps = new PropertySet(stream);
061: try {
062: if (ps.isSummaryInformation())
063: return new SummaryInformation(ps);
064: else if (ps.isDocumentSummaryInformation())
065: return new DocumentSummaryInformation(ps);
066: else
067: return ps;
068: } catch (UnexpectedPropertySetTypeException ex) {
069: /* This exception will never be throws because we already checked
070: * explicitly for this case above. */
071: throw new UnexpectedException(ex.toString());
072: }
073: }
074:
075: /**
076: * <p>Creates a new summary information.</p>
077: *
078: * @return the new summary information.
079: */
080: public static SummaryInformation newSummaryInformation() {
081: final MutablePropertySet ps = new MutablePropertySet();
082: final MutableSection s = (MutableSection) ps.getFirstSection();
083: s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
084: try {
085: return new SummaryInformation(ps);
086: } catch (UnexpectedPropertySetTypeException ex) {
087: /* This should never happen. */
088: throw new HPSFRuntimeException(ex);
089: }
090: }
091:
092: /**
093: * <p>Creates a new document summary information.</p>
094: *
095: * @return the new document summary information.
096: */
097: public static DocumentSummaryInformation newDocumentSummaryInformation() {
098: final MutablePropertySet ps = new MutablePropertySet();
099: final MutableSection s = (MutableSection) ps.getFirstSection();
100: s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
101: try {
102: return new DocumentSummaryInformation(ps);
103: } catch (UnexpectedPropertySetTypeException ex) {
104: /* This should never happen. */
105: throw new HPSFRuntimeException(ex);
106: }
107: }
108:
109: }
|