Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ContentTypeManager.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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.openxml4j.opc.internal;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;
  21. import java.util.Map.Entry;
  22. import java.util.TreeMap;
  23. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  24. import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
  25. import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
  26. import org.apache.poi.openxml4j.opc.OPCPackage;
  27. import org.apache.poi.openxml4j.opc.PackagePart;
  28. import org.apache.poi.openxml4j.opc.PackagePartName;
  29. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  30. import org.apache.poi.util.DocumentHelper;
  31. import org.w3c.dom.Document;
  32. import org.w3c.dom.Element;
  33. import org.w3c.dom.NodeList;
  34. import org.xml.sax.SAXException;
  35. /**
  36. * Manage package content types ([Content_Types].xml part).
  37. *
  38. * @author Julien Chable
  39. */
  40. public abstract class ContentTypeManager {
  41. /**
  42. * Content type part name.
  43. */
  44. public static final String CONTENT_TYPES_PART_NAME = "[Content_Types].xml";
  45. /**
  46. * Content type namespace
  47. */
  48. public static final String TYPES_NAMESPACE_URI = "http://schemas.openxmlformats.org/package/2006/content-types";
  49. /* Xml elements in content type part */
  50. private static final String TYPES_TAG_NAME = "Types";
  51. private static final String DEFAULT_TAG_NAME = "Default";
  52. private static final String EXTENSION_ATTRIBUTE_NAME = "Extension";
  53. private static final String CONTENT_TYPE_ATTRIBUTE_NAME = "ContentType";
  54. private static final String OVERRIDE_TAG_NAME = "Override";
  55. private static final String PART_NAME_ATTRIBUTE_NAME = "PartName";
  56. /**
  57. * Reference to the package using this content type manager.
  58. */
  59. protected OPCPackage container;
  60. /**
  61. * Default content type tree. <Extension, ContentType>
  62. */
  63. private TreeMap<String, String> defaultContentType;
  64. /**
  65. * Override content type tree.
  66. */
  67. private TreeMap<PackagePartName, String> overrideContentType;
  68. /**
  69. * Constructor. Parses the content of the specified input stream.
  70. *
  71. * @param in
  72. * If different of <i>null</i> then the content types part is
  73. * retrieve and parse.
  74. * @throws InvalidFormatException
  75. * If the content types part content is not valid.
  76. */
  77. public ContentTypeManager(InputStream in, OPCPackage pkg)
  78. throws InvalidFormatException {
  79. this.container = pkg;
  80. this.defaultContentType = new TreeMap<String, String>();
  81. if (in != null) {
  82. try {
  83. parseContentTypesFile(in);
  84. } catch (InvalidFormatException e) {
  85. throw new InvalidFormatException(
  86. "Can't read content types part !");
  87. }
  88. }
  89. }
  90. /**
  91. * Build association extention-> content type (will be stored in
  92. * [Content_Types].xml) for example ContentType="image/png" Extension="png"
  93. * <p>
  94. * [M2.8]: When adding a new part to a package, the package implementer
  95. * shall ensure that a content type for that part is specified in the
  96. * Content Types stream; the package implementer shall perform the steps
  97. * described in &#167;9.1.2.3:
  98. * </p><p>
  99. * 1. Get the extension from the part name by taking the substring to the
  100. * right of the rightmost occurrence of the dot character (.) from the
  101. * rightmost segment.
  102. * </p><p>
  103. * 2. If a part name has no extension, a corresponding Override element
  104. * shall be added to the Content Types stream.
  105. * </p><p>
  106. * 3. Compare the resulting extension with the values specified for the
  107. * Extension attributes of the Default elements in the Content Types stream.
  108. * The comparison shall be case-insensitive ASCII.
  109. * </p><p>
  110. * 4. If there is a Default element with a matching Extension attribute,
  111. * then the content type of the new part shall be compared with the value of
  112. * the ContentType attribute. The comparison might be case-sensitive and
  113. * include every character regardless of the role it plays in the
  114. * content-type grammar of RFC 2616, or it might follow the grammar of RFC
  115. * 2616.
  116. * </p><p>
  117. * a. If the content types match, no further action is required.
  118. * </p><p>
  119. * b. If the content types do not match, a new Override element shall be
  120. * added to the Content Types stream. .
  121. * </p><p>
  122. * 5. If there is no Default element with a matching Extension attribute, a
  123. * new Default element or Override element shall be added to the Content
  124. * Types stream.
  125. * </p>
  126. */
  127. public void addContentType(PackagePartName partName, String contentType) {
  128. boolean defaultCTExists = this.defaultContentType.containsValue(contentType);
  129. String extension = partName.getExtension().toLowerCase();
  130. if ((extension.length() == 0)
  131. || (this.defaultContentType.containsKey(extension) && !defaultCTExists))
  132. this.addOverrideContentType(partName, contentType);
  133. else if (!defaultCTExists)
  134. this.addDefaultContentType(extension, contentType);
  135. }
  136. /**
  137. * Add an override content type for a specific part.
  138. *
  139. * @param partName
  140. * Name of the part.
  141. * @param contentType
  142. * Content type of the part.
  143. */
  144. private void addOverrideContentType(PackagePartName partName,
  145. String contentType) {
  146. if (overrideContentType == null)
  147. overrideContentType = new TreeMap<PackagePartName, String>();
  148. overrideContentType.put(partName, contentType);
  149. }
  150. /**
  151. * Add a content type associated with the specified extension.
  152. *
  153. * @param extension
  154. * The part name extension to bind to a content type.
  155. * @param contentType
  156. * The content type associated with the specified extension.
  157. */
  158. private void addDefaultContentType(String extension, String contentType) {
  159. // Remark : Originally the latest parameter was :
  160. // contentType.toLowerCase(). Change due to a request ID 1996748.
  161. defaultContentType.put(extension.toLowerCase(), contentType);
  162. }
  163. /**
  164. * <p>
  165. * Delete a content type based on the specified part name. If the specified
  166. * part name is register with an override content type, then this content
  167. * type is remove, else the content type is remove in the default content
  168. * type list if it exists and if no part is associated with it yet.
  169. * </p><p>
  170. * Check rule M2.4: The package implementer shall require that the Content
  171. * Types stream contain one of the following for every part in the package:
  172. * One matching Default element One matching Override element Both a
  173. * matching Default element and a matching Override element, in which case
  174. * the Override element takes precedence.
  175. * </p>
  176. * @param partName
  177. * The part URI associated with the override content type to
  178. * delete.
  179. * @exception InvalidOperationException
  180. * Throws if
  181. */
  182. public void removeContentType(PackagePartName partName)
  183. throws InvalidOperationException {
  184. if (partName == null)
  185. throw new IllegalArgumentException("partName");
  186. /* Override content type */
  187. if (this.overrideContentType != null
  188. && (this.overrideContentType.get(partName) != null)) {
  189. // Remove the override definition for the specified part.
  190. this.overrideContentType.remove(partName);
  191. return;
  192. }
  193. /* Default content type */
  194. String extensionToDelete = partName.getExtension();
  195. boolean deleteDefaultContentTypeFlag = true;
  196. if (this.container != null) {
  197. try {
  198. for (PackagePart part : this.container.getParts()) {
  199. if (!part.getPartName().equals(partName)
  200. && part.getPartName().getExtension()
  201. .equalsIgnoreCase(extensionToDelete)) {
  202. deleteDefaultContentTypeFlag = false;
  203. break;
  204. }
  205. }
  206. } catch (InvalidFormatException e) {
  207. throw new InvalidOperationException(e.getMessage());
  208. }
  209. }
  210. // Remove the default content type, no other part use this content type.
  211. if (deleteDefaultContentTypeFlag) {
  212. this.defaultContentType.remove(extensionToDelete);
  213. }
  214. /*
  215. * Check rule 2.4: The package implementer shall require that the
  216. * Content Types stream contain one of the following for every part in
  217. * the package: One matching Default element One matching Override
  218. * element Both a matching Default element and a matching Override
  219. * element, in which case the Override element takes precedence.
  220. */
  221. if (this.container != null) {
  222. try {
  223. for (PackagePart part : this.container.getParts()) {
  224. if (!part.getPartName().equals(partName)
  225. && this.getContentType(part.getPartName()) == null)
  226. throw new InvalidOperationException(
  227. "Rule M2.4 is not respected: Nor a default element or override element is associated with the part: "
  228. + part.getPartName().getName());
  229. }
  230. } catch (InvalidFormatException e) {
  231. throw new InvalidOperationException(e.getMessage());
  232. }
  233. }
  234. }
  235. /**
  236. * Check if the specified content type is already register.
  237. *
  238. * @param contentType
  239. * The content type to check.
  240. * @return <code>true</code> if the specified content type is already
  241. * register, then <code>false</code>.
  242. */
  243. public boolean isContentTypeRegister(String contentType) {
  244. if (contentType == null)
  245. throw new IllegalArgumentException("contentType");
  246. return (this.defaultContentType.values().contains(contentType) || (this.overrideContentType != null && this.overrideContentType
  247. .values().contains(contentType)));
  248. }
  249. /**
  250. * Get the content type for the specified part, if any.
  251. * <p>
  252. * Rule [M2.9]: To get the content type of a part, the package implementer
  253. * shall perform the steps described in &#167;9.1.2.4:
  254. * </p><p>
  255. * 1. Compare the part name with the values specified for the PartName
  256. * attribute of the Override elements. The comparison shall be
  257. * case-insensitive ASCII.
  258. * </p><p>
  259. * 2. If there is an Override element with a matching PartName attribute,
  260. * return the value of its ContentType attribute. No further action is
  261. * required.
  262. * </p><p>
  263. * 3. If there is no Override element with a matching PartName attribute,
  264. * then a. Get the extension from the part name by taking the substring to
  265. * the right of the rightmost occurrence of the dot character (.) from the
  266. * rightmost segment. b. Check the Default elements of the Content Types
  267. * stream, comparing the extension with the value of the Extension
  268. * attribute. The comparison shall be case-insensitive ASCII.
  269. * </p><p>
  270. * 4. If there is a Default element with a matching Extension attribute,
  271. * return the value of its ContentType attribute. No further action is
  272. * required.
  273. * </p><p>
  274. * 5. If neither Override nor Default elements with matching attributes are
  275. * found for the specified part name, the implementation shall not map this
  276. * part name to a part.
  277. * </p>
  278. * @param partName
  279. * The URI part to check.
  280. * @return The content type associated with the URI (in case of an override
  281. * content type) or the extension (in case of default content type),
  282. * else <code>null</code>.
  283. *
  284. * @exception OpenXML4JRuntimeException
  285. * Throws if the content type manager is not able to find the
  286. * content from an existing part.
  287. */
  288. public String getContentType(PackagePartName partName) {
  289. if (partName == null)
  290. throw new IllegalArgumentException("partName");
  291. if ((this.overrideContentType != null)
  292. && this.overrideContentType.containsKey(partName))
  293. return this.overrideContentType.get(partName);
  294. String extension = partName.getExtension().toLowerCase();
  295. if (this.defaultContentType.containsKey(extension))
  296. return this.defaultContentType.get(extension);
  297. /*
  298. * [M2.4] : The package implementer shall require that the Content Types
  299. * stream contain one of the following for every part in the package:
  300. * One matching Default element, One matching Override element, Both a
  301. * matching Default element and a matching Override element, in which
  302. * case the Override element takes precedence.
  303. */
  304. if (this.container != null && this.container.getPart(partName) != null) {
  305. throw new OpenXML4JRuntimeException(
  306. "Rule M2.4 exception : this error should NEVER happen, if so please send a mail to the developers team, thanks !");
  307. }
  308. return null;
  309. }
  310. /**
  311. * Clear all content types.
  312. */
  313. public void clearAll() {
  314. this.defaultContentType.clear();
  315. if (this.overrideContentType != null)
  316. this.overrideContentType.clear();
  317. }
  318. /**
  319. * Clear all override content types.
  320. *
  321. */
  322. public void clearOverrideContentTypes() {
  323. if (this.overrideContentType != null)
  324. this.overrideContentType.clear();
  325. }
  326. /**
  327. * Parse the content types part.
  328. *
  329. * @throws InvalidFormatException
  330. * Throws if the content type doesn't exist or the XML format is
  331. * invalid.
  332. */
  333. private void parseContentTypesFile(InputStream in)
  334. throws InvalidFormatException {
  335. try {
  336. Document xmlContentTypetDoc = DocumentHelper.readDocument(in);
  337. // Default content types
  338. NodeList defaultTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagName(DEFAULT_TAG_NAME);
  339. int defaultTypeCount = defaultTypes.getLength();
  340. for (int i = 0; i < defaultTypeCount; i++) {
  341. Element element = (Element) defaultTypes.item(i);
  342. String extension = element.getAttribute(EXTENSION_ATTRIBUTE_NAME);
  343. String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
  344. addDefaultContentType(extension, contentType);
  345. }
  346. // Overriden content types
  347. NodeList overrideTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagName(OVERRIDE_TAG_NAME);
  348. int overrideTypeCount = overrideTypes.getLength();
  349. for (int i = 0; i < overrideTypeCount; i++) {
  350. Element element = (Element) overrideTypes.item(i);
  351. URI uri = new URI(element.getAttribute(PART_NAME_ATTRIBUTE_NAME));
  352. PackagePartName partName = PackagingURIHelper.createPartName(uri);
  353. String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
  354. addOverrideContentType(partName, contentType);
  355. }
  356. } catch (URISyntaxException urie) {
  357. throw new InvalidFormatException(urie.getMessage());
  358. } catch (SAXException e) {
  359. throw new InvalidFormatException(e.getMessage());
  360. } catch (IOException e) {
  361. throw new InvalidFormatException(e.getMessage());
  362. }
  363. }
  364. /**
  365. * Save the contents type part.
  366. *
  367. * @param outStream
  368. * The output stream use to save the XML content of the content
  369. * types part.
  370. * @return <b>true</b> if the operation success, else <b>false</b>.
  371. */
  372. public boolean save(OutputStream outStream) {
  373. Document xmlOutDoc = DocumentHelper.createDocument();
  374. // Building namespace
  375. Element typesElem = xmlOutDoc.createElementNS(TYPES_NAMESPACE_URI, TYPES_TAG_NAME);
  376. xmlOutDoc.appendChild(typesElem);
  377. // Adding default types
  378. for (Entry<String, String> entry : defaultContentType.entrySet()) {
  379. appendDefaultType(typesElem, entry);
  380. }
  381. // Adding specific types if any exist
  382. if (overrideContentType != null) {
  383. for (Entry<PackagePartName, String> entry : overrideContentType
  384. .entrySet()) {
  385. appendSpecificTypes(typesElem, entry);
  386. }
  387. }
  388. xmlOutDoc.normalize();
  389. // Save content in the specified output stream
  390. return this.saveImpl(xmlOutDoc, outStream);
  391. }
  392. /**
  393. * Use to append specific type XML elements, use by the save() method.
  394. *
  395. * @param root
  396. * XML parent element use to append this override type element.
  397. * @param entry
  398. * The values to append.
  399. * @see #save(java.io.OutputStream)
  400. */
  401. private void appendSpecificTypes(Element root,
  402. Entry<PackagePartName, String> entry) {
  403. Element specificType = root.getOwnerDocument().createElement(OVERRIDE_TAG_NAME);
  404. specificType.setAttribute(PART_NAME_ATTRIBUTE_NAME, entry.getKey().getName());
  405. specificType.setAttribute(CONTENT_TYPE_ATTRIBUTE_NAME, entry.getValue());
  406. root.appendChild(specificType);
  407. }
  408. /**
  409. * Use to append default types XML elements, use by the save() method.
  410. *
  411. * @param root
  412. * XML parent element use to append this default type element.
  413. * @param entry
  414. * The values to append.
  415. * @see #save(java.io.OutputStream)
  416. */
  417. private void appendDefaultType(Element root, Entry<String, String> entry) {
  418. Element defaultType = root.getOwnerDocument().createElement(DEFAULT_TAG_NAME);
  419. defaultType.setAttribute(EXTENSION_ATTRIBUTE_NAME, entry.getKey());
  420. defaultType.setAttribute(CONTENT_TYPE_ATTRIBUTE_NAME, entry.getValue());
  421. root.appendChild(defaultType);
  422. }
  423. /**
  424. * Specific implementation of the save method. Call by the save() method,
  425. * call before exiting.
  426. *
  427. * @param out
  428. * The output stream use to write the content type XML.
  429. */
  430. public abstract boolean saveImpl(Document content, OutputStream out);
  431. }