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.

ExObjList.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. package org.apache.poi.hslf.record;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.ArrayList;
  21. import org.apache.poi.util.LittleEndian;
  22. /**
  23. * This class holds the links to exernal objects referenced
  24. * from the document.
  25. * @author Nick Burch
  26. */
  27. public class ExObjList extends RecordContainer {
  28. private byte[] _header;
  29. private static final long _type = RecordTypes.ExObjList.typeID;
  30. // Links to our more interesting children
  31. private ExObjListAtom exObjListAtom;
  32. /**
  33. * Returns the ExObjListAtom of this list
  34. */
  35. public ExObjListAtom getExObjListAtom() { return exObjListAtom; }
  36. /**
  37. * Returns all the ExHyperlinks
  38. */
  39. public ExHyperlink[] getExHyperlinks() {
  40. ArrayList<ExHyperlink> links = new ArrayList<>();
  41. for(int i=0; i<_children.length; i++) {
  42. if(_children[i] instanceof ExHyperlink) {
  43. links.add( (ExHyperlink)_children[i] );
  44. }
  45. }
  46. return links.toArray(new ExHyperlink[0]);
  47. }
  48. /**
  49. * Set things up, and find our more interesting children
  50. */
  51. protected ExObjList(byte[] source, int start, int len) {
  52. // Grab the header
  53. _header = new byte[8];
  54. System.arraycopy(source,start,_header,0,8);
  55. // Find our children
  56. _children = Record.findChildRecords(source,start+8,len-8);
  57. findInterestingChildren();
  58. }
  59. /**
  60. * Go through our child records, picking out the ones that are
  61. * interesting, and saving those for use by the easy helper
  62. * methods.
  63. */
  64. private void findInterestingChildren() {
  65. // First child should be the atom
  66. if(_children[0] instanceof ExObjListAtom) {
  67. exObjListAtom = (ExObjListAtom)_children[0];
  68. } else {
  69. throw new IllegalStateException("First child record wasn't a ExObjListAtom, was of type " + _children[0].getRecordType());
  70. }
  71. }
  72. /**
  73. * Create a new ExObjList, with blank fields
  74. */
  75. public ExObjList() {
  76. _header = new byte[8];
  77. _children = new org.apache.poi.hslf.record.Record[1];
  78. // Setup our header block
  79. _header[0] = 0x0f; // We are a container record
  80. LittleEndian.putShort(_header, 2, (short)_type);
  81. // Setup our child records
  82. _children[0] = new ExObjListAtom();
  83. findInterestingChildren();
  84. }
  85. /**
  86. * We are of type 1033
  87. */
  88. public long getRecordType() { return _type; }
  89. /**
  90. * Write the contents of the record back, so it can be written
  91. * to disk
  92. */
  93. public void writeOut(OutputStream out) throws IOException {
  94. writeOut(_header[0],_header[1],_type,_children,out);
  95. }
  96. /**
  97. * Lookup a hyperlink by its unique id
  98. *
  99. * @param id hyperlink id
  100. * @return found <code>ExHyperlink</code> or <code>null</code>
  101. */
  102. public ExHyperlink get(int id){
  103. for(int i=0; i<_children.length; i++) {
  104. if(_children[i] instanceof ExHyperlink) {
  105. ExHyperlink rec = (ExHyperlink)_children[i];
  106. if (rec.getExHyperlinkAtom().getNumber() == id){
  107. return rec;
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113. }