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.

FieldsImpl.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.hwpf.usermodel;
  16. import java.io.Serializable;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.poi.hwpf.model.FieldDescriptor;
  25. import org.apache.poi.hwpf.model.FieldsDocumentPart;
  26. import org.apache.poi.hwpf.model.FieldsTables;
  27. import org.apache.poi.hwpf.model.PlexOfField;
  28. import org.apache.poi.util.Internal;
  29. /**
  30. * Default implementation of {@link Field}
  31. *
  32. * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
  33. */
  34. @Internal
  35. public class FieldsImpl implements Fields
  36. {
  37. /**
  38. * This is port and adaptation of Arrays.binarySearch from Java 6 (Apache
  39. * Harmony).
  40. */
  41. private static int binarySearch( List<PlexOfField> list,
  42. int startIndex, int endIndex, int requiredStartOffset )
  43. {
  44. checkIndexForBinarySearch( list.size(), startIndex, endIndex );
  45. int low = startIndex, mid = -1, high = endIndex - 1;
  46. while ( low <= high )
  47. {
  48. mid = ( low + high ) >>> 1;
  49. int midStart = list.get( mid ).getFcStart();
  50. if ( midStart == requiredStartOffset )
  51. {
  52. return mid;
  53. }
  54. else if ( midStart < requiredStartOffset )
  55. {
  56. low = mid + 1;
  57. }
  58. else
  59. {
  60. high = mid - 1;
  61. }
  62. }
  63. if ( mid < 0 )
  64. {
  65. int insertPoint = endIndex;
  66. for ( int index = startIndex; index < endIndex; index++ )
  67. {
  68. if ( requiredStartOffset < list.get( index ).getFcStart() )
  69. {
  70. insertPoint = index;
  71. }
  72. }
  73. return -insertPoint - 1;
  74. }
  75. return -mid - 1;
  76. }
  77. private static void checkIndexForBinarySearch( int length, int start,
  78. int end )
  79. {
  80. if ( start > end )
  81. {
  82. throw new IllegalArgumentException();
  83. }
  84. if ( length < end || 0 > start )
  85. {
  86. throw new ArrayIndexOutOfBoundsException();
  87. }
  88. }
  89. private Map<FieldsDocumentPart, Map<Integer, FieldImpl>> _fieldsByOffset;
  90. private PlexOfFieldComparator comparator = new PlexOfFieldComparator();
  91. public FieldsImpl( FieldsTables fieldsTables )
  92. {
  93. _fieldsByOffset = new HashMap<>(
  94. FieldsDocumentPart.values().length);
  95. for ( FieldsDocumentPart part : FieldsDocumentPart.values() )
  96. {
  97. List<PlexOfField> plexOfCps = fieldsTables.getFieldsPLCF( part );
  98. _fieldsByOffset.put( part, parseFieldStructure( plexOfCps ) );
  99. }
  100. }
  101. public Collection<Field> getFields( FieldsDocumentPart part )
  102. {
  103. Map<Integer, FieldImpl> map = _fieldsByOffset.get( part );
  104. if ( map == null || map.isEmpty() )
  105. return Collections.emptySet();
  106. return Collections.unmodifiableCollection( map.values() );
  107. }
  108. public FieldImpl getFieldByStartOffset( FieldsDocumentPart documentPart,
  109. int offset )
  110. {
  111. Map<Integer, FieldImpl> map = _fieldsByOffset.get( documentPart );
  112. if ( map == null || map.isEmpty() )
  113. return null;
  114. return map.get(offset);
  115. }
  116. private Map<Integer, FieldImpl> parseFieldStructure(
  117. List<PlexOfField> plexOfFields )
  118. {
  119. if ( plexOfFields == null || plexOfFields.isEmpty() )
  120. return new HashMap<>();
  121. plexOfFields.sort(comparator);
  122. List<FieldImpl> fields = new ArrayList<>(
  123. plexOfFields.size() / 3 + 1);
  124. parseFieldStructureImpl( plexOfFields, 0, plexOfFields.size(), fields );
  125. HashMap<Integer, FieldImpl> result = new HashMap<>(
  126. fields.size());
  127. for ( FieldImpl field : fields )
  128. {
  129. result.put(field.getFieldStartOffset(), field );
  130. }
  131. return result;
  132. }
  133. @SuppressWarnings("UnnecessaryContinue")
  134. private void parseFieldStructureImpl(List<PlexOfField> plexOfFields,
  135. int startOffsetInclusive, int endOffsetExclusive,
  136. List<FieldImpl> result )
  137. {
  138. int next = startOffsetInclusive;
  139. while ( next < endOffsetExclusive )
  140. {
  141. PlexOfField startPlexOfField = plexOfFields.get( next );
  142. if ( startPlexOfField.getFld().getBoundaryType() != FieldDescriptor.FIELD_BEGIN_MARK )
  143. {
  144. /* Start mark seems to be missing */
  145. next++;
  146. continue;
  147. }
  148. /*
  149. * we have start node. end offset points to next node, separator or
  150. * end
  151. */
  152. int nextNodePositionInList = binarySearch( plexOfFields, next + 1,
  153. endOffsetExclusive, startPlexOfField.getFcEnd() );
  154. if ( nextNodePositionInList < 0 )
  155. {
  156. /*
  157. * too bad, this start field mark doesn't have corresponding end
  158. * field mark or separator field mark in fields table
  159. */
  160. next++;
  161. continue;
  162. }
  163. PlexOfField nextPlexOfField = plexOfFields
  164. .get( nextNodePositionInList );
  165. switch ( nextPlexOfField.getFld().getBoundaryType() )
  166. {
  167. case FieldDescriptor.FIELD_SEPARATOR_MARK:
  168. {
  169. int endNodePositionInList = binarySearch( plexOfFields,
  170. nextNodePositionInList, endOffsetExclusive,
  171. nextPlexOfField.getFcEnd() );
  172. if ( endNodePositionInList < 0 )
  173. {
  174. /*
  175. * too bad, this separator field mark doesn't have
  176. * corresponding end field mark in fields table
  177. */
  178. next++;
  179. continue;
  180. }
  181. PlexOfField endPlexOfField = plexOfFields
  182. .get( endNodePositionInList );
  183. if ( endPlexOfField.getFld().getBoundaryType() != FieldDescriptor.FIELD_END_MARK )
  184. {
  185. /* Not and ending mark */
  186. next++;
  187. continue;
  188. }
  189. FieldImpl field = new FieldImpl( startPlexOfField,
  190. nextPlexOfField, endPlexOfField );
  191. result.add( field );
  192. // adding included fields
  193. if ( startPlexOfField.getFcStart() + 1 < nextPlexOfField
  194. .getFcStart() - 1 )
  195. {
  196. parseFieldStructureImpl( plexOfFields, next + 1,
  197. nextNodePositionInList, result );
  198. }
  199. if ( nextPlexOfField.getFcStart() + 1 < endPlexOfField
  200. .getFcStart() - 1 )
  201. {
  202. parseFieldStructureImpl( plexOfFields,
  203. nextNodePositionInList + 1, endNodePositionInList,
  204. result );
  205. }
  206. next = endNodePositionInList + 1;
  207. break;
  208. }
  209. case FieldDescriptor.FIELD_END_MARK:
  210. {
  211. // we have no separator
  212. FieldImpl field = new FieldImpl( startPlexOfField, null,
  213. nextPlexOfField );
  214. result.add( field );
  215. // adding included fields
  216. if ( startPlexOfField.getFcStart() + 1 < nextPlexOfField
  217. .getFcStart() - 1 )
  218. {
  219. parseFieldStructureImpl( plexOfFields, next + 1,
  220. nextNodePositionInList, result );
  221. }
  222. next = nextNodePositionInList + 1;
  223. break;
  224. }
  225. case FieldDescriptor.FIELD_BEGIN_MARK:
  226. default:
  227. {
  228. /* something is wrong, ignoring this mark along with start mark */
  229. next++;
  230. continue;
  231. }
  232. }
  233. }
  234. }
  235. private static final class PlexOfFieldComparator implements Comparator<PlexOfField>, Serializable {
  236. public int compare( PlexOfField o1, PlexOfField o2 )
  237. {
  238. int thisVal = o1.getFcStart();
  239. int anotherVal = o2.getFcStart();
  240. return Integer.compare(thisVal, anotherVal);
  241. }
  242. }
  243. }