You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XmlVisioDocument.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.xdgf.usermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import org.apache.poi.POIXMLDocument;
  22. import org.apache.poi.POIXMLDocumentPart;
  23. import org.apache.poi.POIXMLException;
  24. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  25. import org.apache.poi.openxml4j.opc.OPCPackage;
  26. import org.apache.poi.openxml4j.opc.PackagePart;
  27. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  28. import org.apache.poi.util.PackageHelper;
  29. import org.apache.xmlbeans.XmlException;
  30. import com.microsoft.schemas.office.visio.x2012.main.VisioDocumentDocument1;
  31. import com.microsoft.schemas.office.visio.x2012.main.VisioDocumentType;
  32. /**
  33. * This is your high-level starting point for working with Visio XML
  34. * documents (.vsdx).
  35. *
  36. * Currently, only read support has been implemented, and the API is
  37. * not mature and is subject to change.
  38. *
  39. * For more information about the visio XML format (with an XSD 1.0
  40. * schema), you can find documentation at
  41. * https://msdn.microsoft.com/en-us/library/hh645006(v=office.12).aspx
  42. *
  43. * That document lacks in some areas, but you can find additional
  44. * documentation and an updated XSD 1.1 schema at
  45. * https://msdn.microsoft.com/en-us/library/office/jj684209(v=office.15).aspx
  46. *
  47. * Each provides different details, but the SharePoint reference
  48. * has better documentation and is more useful.
  49. */
  50. public class XmlVisioDocument extends POIXMLDocument {
  51. protected XDGFPages _pages;
  52. protected XDGFMasters _masters;
  53. protected XDGFDocument _document;
  54. public XmlVisioDocument(OPCPackage pkg) throws IOException {
  55. super(pkg, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
  56. VisioDocumentType document;
  57. try {
  58. document = VisioDocumentDocument1.Factory.parse(getPackagePart().getInputStream()).getVisioDocument();
  59. } catch (XmlException e) {
  60. throw new POIXMLException(e);
  61. } catch (IOException e) {
  62. throw new POIXMLException(e);
  63. }
  64. _document = new XDGFDocument(document);
  65. //build a tree of POIXMLDocumentParts, this document being the root
  66. load(new XDGFFactory(_document));
  67. }
  68. public XmlVisioDocument(InputStream is) throws IOException {
  69. this(PackageHelper.open(is));
  70. }
  71. @Override
  72. protected void onDocumentRead() throws IOException {
  73. // by the time this gets called, all other document parts should
  74. // have been loaded, so it's safe to build the document structure
  75. // note that in other onDocumentRead(), relations/etc may not have
  76. // loaded yet, so it's not quite safe
  77. for (POIXMLDocumentPart part : getRelations()) {
  78. // organize the document pieces
  79. if (part instanceof XDGFPages)
  80. _pages = (XDGFPages) part;
  81. else if (part instanceof XDGFMasters)
  82. _masters = (XDGFMasters) part;
  83. }
  84. if (_masters != null)
  85. _masters.onDocumentRead();
  86. _pages.onDocumentRead();
  87. }
  88. /**
  89. * Not currently implemented
  90. */
  91. @Override
  92. public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
  93. return new ArrayList<PackagePart>();
  94. }
  95. //
  96. // Useful public API goes here
  97. //
  98. /**
  99. * @return pages ordered by page number
  100. */
  101. public Collection<XDGFPage> getPages() {
  102. return _pages.getPageList();
  103. }
  104. public XDGFStyleSheet getStyleById(long id) {
  105. return _document.getStyleById(id);
  106. }
  107. }