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 |
|
---|
9 | http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 |
|
---|
11 | Unless required by applicable law or agreed to in writing, software
|
---|
12 | distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | See the License for the specific language governing permissions and
|
---|
15 | limitations under the License.
|
---|
16 | ==================================================================== */
|
---|
17 |
|
---|
18 | package org.apache.poi;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
21 |
|
---|
22 | import org.apache.poi.hpsf.DocumentSummaryInformation;
|
---|
23 | import org.apache.poi.hpsf.PropertySet;
|
---|
24 | import org.apache.poi.hpsf.PropertySetFactory;
|
---|
25 | import org.apache.poi.hpsf.SummaryInformation;
|
---|
26 | import org.apache.poi.poifs.filesystem.DirectoryNode;
|
---|
27 | import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
---|
28 | import org.apache.poi.util.POILogFactory;
|
---|
29 | import org.apache.poi.util.POILogger;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This holds the common functionality for all POI
|
---|
33 | * Document classes.
|
---|
34 | * Currently, this relates to Document Information Properties
|
---|
35 | *
|
---|
36 | * @author Nick Burch
|
---|
37 | */
|
---|
38 | public abstract class POIDocument {
|
---|
39 | /** Holds metadata on our document */
|
---|
40 | private SummaryInformation sInf;
|
---|
41 | /** Holds further metadata on our document */
|
---|
42 | private DocumentSummaryInformation dsInf;
|
---|
43 | /** The directory that our document lives in */
|
---|
44 | protected DirectoryNode directory;
|
---|
45 |
|
---|
46 | /** For our own logging use */
|
---|
47 | private final static POILogger logger = POILogFactory.getLogger(POIDocument.class);
|
---|
48 |
|
---|
49 | /* Have the property streams been read yet? (Only done on-demand) */
|
---|
50 | private boolean initialized = false;
|
---|
51 |
|
---|
52 |
|
---|
53 | protected POIDocument(DirectoryNode dir) {
|
---|
54 | this.directory = dir;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Fetch the Document Summary Information of the document
|
---|
60 | */
|
---|
61 | public DocumentSummaryInformation getDocumentSummaryInformation() { // NO_UCD
|
---|
62 | if(!initialized) readProperties();
|
---|
63 | return dsInf;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Fetch the Summary Information of the document
|
---|
68 | */
|
---|
69 | public SummaryInformation getSummaryInformation() { // NO_UCD
|
---|
70 | if(!initialized) readProperties();
|
---|
71 | return sInf;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Find, and create objects for, the standard
|
---|
77 | * Documment Information Properties (HPSF).
|
---|
78 | * If a given property set is missing or corrupt,
|
---|
79 | * it will remain null;
|
---|
80 | */
|
---|
81 | protected void readProperties() {
|
---|
82 | PropertySet ps;
|
---|
83 |
|
---|
84 | // DocumentSummaryInformation
|
---|
85 | ps = getPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
|
---|
86 | if(ps != null && ps instanceof DocumentSummaryInformation) {
|
---|
87 | dsInf = (DocumentSummaryInformation)ps;
|
---|
88 | } else if(ps != null) {
|
---|
89 | logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back with wrong class - ", ps.getClass());
|
---|
90 | }
|
---|
91 |
|
---|
92 | // SummaryInformation
|
---|
93 | ps = getPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
|
---|
94 | if(ps instanceof SummaryInformation) {
|
---|
95 | sInf = (SummaryInformation)ps;
|
---|
96 | } else if(ps != null) {
|
---|
97 | logger.log(POILogger.WARN, "SummaryInformation property set came back with wrong class - ", ps.getClass());
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Mark the fact that we've now loaded up the properties
|
---|
101 | initialized = true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * For a given named property entry, either return it or null if
|
---|
106 | * if it wasn't found
|
---|
107 | */
|
---|
108 | protected PropertySet getPropertySet(String setName) {
|
---|
109 | //directory can be null when creating new documents
|
---|
110 | if(directory == null) return null;
|
---|
111 |
|
---|
112 | DocumentInputStream dis;
|
---|
113 | try {
|
---|
114 | // Find the entry, and get an input stream for it
|
---|
115 | dis = directory.createDocumentInputStream(setName);
|
---|
116 | } catch(IOException ie) {
|
---|
117 | // Oh well, doesn't exist
|
---|
118 | logger.log(POILogger.WARN, "Error getting property set with name " + setName + "\n" + ie);
|
---|
119 | return null;
|
---|
120 | }
|
---|
121 |
|
---|
122 | try {
|
---|
123 | // Create the Property Set
|
---|
124 | PropertySet set = PropertySetFactory.create(dis);
|
---|
125 | return set;
|
---|
126 | } catch(IOException ie) {
|
---|
127 | // Must be corrupt or something like that
|
---|
128 | logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + ie);
|
---|
129 | } catch(org.apache.poi.hpsf.HPSFException he) {
|
---|
130 | // Oh well, doesn't exist
|
---|
131 | logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + he);
|
---|
132 | }
|
---|
133 | return null;
|
---|
134 | }
|
---|
135 | }
|
---|