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.

ExternalLinksTable.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.xssf.model;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.poi.ooxml.POIXMLDocumentPart;
  23. import org.apache.poi.openxml4j.opc.PackagePart;
  24. import org.apache.poi.openxml4j.opc.PackageRelationship;
  25. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  26. import org.apache.poi.openxml4j.opc.TargetMode;
  27. import org.apache.poi.ss.usermodel.Name;
  28. import org.apache.xmlbeans.XmlException;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalDefinedName;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalLink;
  31. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalSheetName;
  32. import org.openxmlformats.schemas.spreadsheetml.x2006.main.ExternalLinkDocument;
  33. /**
  34. * Holds details of links to parts of other workbooks (eg named ranges),
  35. * along with the most recently seen values for what they point to.
  36. */
  37. public class ExternalLinksTable extends POIXMLDocumentPart {
  38. private CTExternalLink link;
  39. public ExternalLinksTable() {
  40. super();
  41. link = CTExternalLink.Factory.newInstance();
  42. link.addNewExternalBook();
  43. }
  44. /**
  45. * @since POI 3.14-Beta1
  46. */
  47. public ExternalLinksTable(PackagePart part) throws IOException {
  48. super(part);
  49. readFrom(part.getInputStream());
  50. }
  51. public void readFrom(InputStream is) throws IOException {
  52. try {
  53. ExternalLinkDocument doc = ExternalLinkDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
  54. link = doc.getExternalLink();
  55. } catch (XmlException e) {
  56. throw new IOException(e.getLocalizedMessage());
  57. }
  58. }
  59. public void writeTo(OutputStream out) throws IOException {
  60. ExternalLinkDocument doc = ExternalLinkDocument.Factory.newInstance();
  61. doc.setExternalLink(link);
  62. doc.save(out, DEFAULT_XML_OPTIONS);
  63. }
  64. @Override
  65. protected void commit() throws IOException {
  66. PackagePart part = getPackagePart();
  67. OutputStream out = part.getOutputStream();
  68. writeTo(out);
  69. out.close();
  70. }
  71. /**
  72. * Returns the underlying xmlbeans object for the external
  73. * link table
  74. */
  75. public CTExternalLink getCTExternalLink(){
  76. return link;
  77. }
  78. /**
  79. * Returns the last recorded name of the file that this
  80. * is linked to
  81. */
  82. public String getLinkedFileName() {
  83. String rId = link.getExternalBook().getId();
  84. PackageRelationship rel = getPackagePart().getRelationship(rId);
  85. if (rel != null && rel.getTargetMode() == TargetMode.EXTERNAL) {
  86. return rel.getTargetURI().toString();
  87. } else {
  88. return null;
  89. }
  90. }
  91. /**
  92. * Updates the last recorded name for the file that this links to
  93. */
  94. public void setLinkedFileName(String target) {
  95. String rId = link.getExternalBook().getId();
  96. if (rId == null || rId.isEmpty()) {
  97. // We're a new External Link Table, so nothing to remove
  98. } else {
  99. // Relationships can't be changed, so remove the old one
  100. getPackagePart().removeRelationship(rId);
  101. }
  102. // Have a new one added
  103. PackageRelationship newRel = getPackagePart().addExternalRelationship(
  104. target, PackageRelationshipTypes.EXTERNAL_LINK_PATH);
  105. link.getExternalBook().setId(newRel.getId());
  106. }
  107. public List<String> getSheetNames() {
  108. CTExternalSheetName[] sheetNames =
  109. link.getExternalBook().getSheetNames().getSheetNameArray();
  110. List<String> names = new ArrayList<>(sheetNames.length);
  111. for (CTExternalSheetName name : sheetNames) {
  112. names.add(name.getVal());
  113. }
  114. return names;
  115. }
  116. public List<Name> getDefinedNames() {
  117. CTExternalDefinedName[] extNames =
  118. link.getExternalBook().getDefinedNames().getDefinedNameArray();
  119. List<Name> names = new ArrayList<>(extNames.length);
  120. for (CTExternalDefinedName extName : extNames) {
  121. names.add(new ExternalName(extName));
  122. }
  123. return names;
  124. }
  125. // TODO Last seen data
  126. protected class ExternalName implements Name {
  127. private final CTExternalDefinedName name;
  128. protected ExternalName(CTExternalDefinedName name) {
  129. this.name = name;
  130. }
  131. @Override
  132. public String getNameName() {
  133. return name.getName();
  134. }
  135. @Override
  136. public void setNameName(String name) {
  137. this.name.setName(name);
  138. }
  139. @Override
  140. public String getSheetName() {
  141. int sheetId = getSheetIndex();
  142. if (sheetId >= 0) {
  143. return getSheetNames().get(sheetId);
  144. } else {
  145. return null;
  146. }
  147. }
  148. @Override
  149. public int getSheetIndex() {
  150. if (name.isSetSheetId()) {
  151. return (int)name.getSheetId();
  152. }
  153. return -1;
  154. }
  155. @Override
  156. public void setSheetIndex(int sheetId) {
  157. name.setSheetId(sheetId);
  158. }
  159. @Override
  160. public String getRefersToFormula() {
  161. // Return, without the leading =
  162. return name.getRefersTo().substring(1);
  163. }
  164. @Override
  165. public void setRefersToFormula(String formulaText) {
  166. // Save with leading =
  167. name.setRefersTo('=' + formulaText);
  168. }
  169. @Override
  170. public boolean isFunctionName() {
  171. return false;
  172. }
  173. @Override
  174. public boolean isDeleted() {
  175. return false;
  176. }
  177. @Override
  178. public boolean isHidden() {
  179. return false;
  180. }
  181. @Override
  182. public String getComment() {
  183. return null;
  184. }
  185. @Override
  186. public void setComment(String comment) {
  187. throw new IllegalStateException("Not Supported");
  188. }
  189. @Override
  190. public void setFunction(boolean value) {
  191. throw new IllegalStateException("Not Supported");
  192. }
  193. }
  194. }