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.

HemfExtractor.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.hemf.extractor;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.Iterator;
  19. import org.apache.poi.hemf.record.HemfHeader;
  20. import org.apache.poi.hemf.record.HemfRecord;
  21. import org.apache.poi.hemf.record.HemfRecordType;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndianInputStream;
  24. import org.apache.poi.util.RecordFormatException;
  25. /**
  26. * Read-only EMF extractor. Lots remain
  27. */
  28. @Internal
  29. public class HemfExtractor implements Iterable<HemfRecord> {
  30. private HemfHeader header;
  31. private final LittleEndianInputStream stream;
  32. public HemfExtractor(InputStream is) throws IOException {
  33. stream = new LittleEndianInputStream(is);
  34. header = new HemfHeader();
  35. long recordId = stream.readUInt();
  36. long recordSize = stream.readUInt();
  37. header = new HemfHeader();
  38. header.init(stream, recordId, recordSize-8);
  39. }
  40. @Override
  41. public Iterator<HemfRecord> iterator() {
  42. return new HemfRecordIterator();
  43. }
  44. public HemfHeader getHeader() {
  45. return header;
  46. }
  47. private class HemfRecordIterator implements Iterator<HemfRecord> {
  48. private HemfRecord currentRecord;
  49. HemfRecordIterator() {
  50. //queue the first non-header record
  51. currentRecord = _next();
  52. }
  53. @Override
  54. public boolean hasNext() {
  55. return currentRecord != null;
  56. }
  57. @Override
  58. public HemfRecord next() {
  59. HemfRecord toReturn = currentRecord;
  60. currentRecord = _next();
  61. return toReturn;
  62. }
  63. private HemfRecord _next() {
  64. if (currentRecord != null && currentRecord.getRecordType().equals(HemfRecordType.eof)) {
  65. return null;
  66. }
  67. long recordId = stream.readUInt();
  68. long recordSize = stream.readUInt();
  69. HemfRecord record = null;
  70. HemfRecordType type = HemfRecordType.getById(recordId);
  71. if (type == null) {
  72. throw new RuntimeException("Undefined record of type:"+recordId);
  73. }
  74. try {
  75. record = type.clazz.newInstance();
  76. } catch (InstantiationException e) {
  77. throw new RuntimeException(e);
  78. } catch (IllegalAccessException e) {
  79. throw new RuntimeException(e);
  80. }
  81. try {
  82. record.init(stream, recordId, recordSize-8);
  83. } catch (IOException e) {
  84. throw new RecordFormatException(e);
  85. }
  86. return record;
  87. }
  88. @Override
  89. public void remove() {
  90. throw new UnsupportedOperationException("Remove not supported");
  91. }
  92. }
  93. }