001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.sequencer.odf; 017 018import java.io.InputStream; 019import java.util.ArrayList; 020import java.util.Calendar; 021import java.util.List; 022 023import javax.xml.datatype.Duration; 024 025import org.odftoolkit.simple.Document; 026import org.odftoolkit.simple.PresentationDocument; 027import org.odftoolkit.simple.SpreadsheetDocument; 028import org.odftoolkit.simple.meta.Meta; 029 030/** 031 * Utility for extracting metadata from OpenDocument formats. 032 * 033 * @since 5.1 034 */ 035public class OdfMetadata { 036 037 static final String[] MIME_TYPE_STRINGS = { "application/vnd.oasis.opendocument.text", 038 "application/vnd.oasis.opendocument.spreadsheet", 039 "application/vnd.oasis.opendocument.presentation", 040 "application/vnd.oasis.opendocument.graphics", 041 "application/vnd.oasis.opendocument.chart", 042 "application/vnd.oasis.opendocument.text-template", 043 "application/vnd.oasis.opendocument.spreadsheet-template", 044 "application/vnd.oasis.opendocument.presentation-template", 045 "application/vnd.oasis.opendocument.graphics-template", 046 "application/vnd.oasis.opendocument.chart-template", 047 // not supported yet in odftoolkit: 048 // "application/vnd.oasis.opendocument.base", 049 // "application/vnd.oasis.opendocument.formula", 050 // "application/vnd.oasis.opendocument.formula-template", 051 // "application/vnd.oasis.opendocument.image", 052 // "application/vnd.oasis.opendocument.image-template" 053 }; 054 055 private Integer pages; 056 private Integer sheets; 057 private Calendar creationDate; 058 private String creator; 059 private String description; 060 private Integer editingCycles; 061 private Long editingTime; 062 private String generator; 063 private String initialCreator; 064 private List<String> keywords = new ArrayList<>(); 065 private String language; 066 private Calendar modificationDate; 067 private String printedBy; 068 private Calendar printDate; 069 private String title; 070 private String subject; 071 072 private InputStream in; 073 074 public OdfMetadata( InputStream inputStream ) { 075 this.in = inputStream; 076 } 077 078 /* 079 * Check that given file is supported by this sequencer and parse the metadata in the process. 080 */ 081 public boolean check() throws Exception { 082 Document doc = Document.loadDocument(in); 083 Meta metadata = doc.getOfficeMetadata(); 084 if (metadata != null) { 085 title = metadata.getTitle(); 086 subject = metadata.getSubject(); 087 description = metadata.getDescription(); 088 initialCreator = metadata.getInitialCreator(); 089 creator = metadata.getCreator(); 090 language = metadata.getLanguage(); 091 editingCycles = metadata.getEditingCycles(); 092 creationDate = metadata.getCreationDate(); 093 modificationDate = metadata.getDcdate(); 094 if (metadata.getEditingDuration() != null) { 095 Duration duration = metadata.getEditingDuration().getValue(); 096 editingTime = duration.getTimeInMillis(Calendar.getInstance()) / 1000; 097 } 098 printDate = metadata.getPrintDate(); 099 printedBy = metadata.getPrintedBy(); 100 if (metadata.getKeywords() != null) { 101 keywords.addAll(metadata.getKeywords()); 102 } 103 generator = metadata.getGenerator(); 104 if (metadata.getDocumentStatistic() != null) { 105 pages = metadata.getDocumentStatistic().getPageCount(); 106 } 107 } 108 109 // document specific meta 110 if (doc instanceof PresentationDocument) { 111 PresentationDocument presentation = (PresentationDocument) doc; 112 pages = presentation.getSlideCount(); 113 } 114 115 if (doc instanceof SpreadsheetDocument) { 116 SpreadsheetDocument spreadsheet = (SpreadsheetDocument) doc; 117 sheets = spreadsheet.getSheetCount(); 118 } 119 120 return true; 121 } 122 123 public Integer getPages() { 124 return pages; 125 } 126 127 public Integer getSheets() { 128 return sheets; 129 } 130 131 public Calendar getCreationDate() { 132 return creationDate; 133 } 134 135 public String getCreator() { 136 return creator; 137 } 138 139 public String getDescription() { 140 return description; 141 } 142 143 public Integer getEditingCycles() { 144 return editingCycles; 145 } 146 147 public Long getEditingTime() { 148 return editingTime; 149 } 150 151 public String getGenerator() { 152 return generator; 153 } 154 155 public String getInitialCreator() { 156 return initialCreator; 157 } 158 159 public List<String> getKeywords() { 160 return keywords; 161 } 162 163 public String getLanguage() { 164 return language; 165 } 166 167 public Calendar getModificationDate() { 168 return modificationDate; 169 } 170 171 public String getPrintedBy() { 172 return printedBy; 173 } 174 175 public Calendar getPrintDate() { 176 return printDate; 177 } 178 179 public String getTitle() { 180 return title; 181 } 182 183 public String getSubject() { 184 return subject; 185 } 186 187}