Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FieldImpl.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 org.apache.poi.hwpf.model.FieldDescriptor;
  17. import org.apache.poi.hwpf.model.PlexOfField;
  18. import org.apache.poi.util.Internal;
  19. /**
  20. * TODO: document me
  21. *
  22. * @author Sergey Vladimirov (vlsergey {at} gmail {dot} com)
  23. */
  24. @Internal
  25. class FieldImpl implements Field
  26. {
  27. private PlexOfField endPlex;
  28. private PlexOfField separatorPlex;
  29. private PlexOfField startPlex;
  30. public FieldImpl( PlexOfField startPlex, PlexOfField separatorPlex,
  31. PlexOfField endPlex )
  32. {
  33. if ( startPlex == null )
  34. throw new IllegalArgumentException( "startPlex == null" );
  35. if ( endPlex == null )
  36. throw new IllegalArgumentException( "endPlex == null" );
  37. if ( startPlex.getFld().getBoundaryType() != FieldDescriptor.FIELD_BEGIN_MARK )
  38. throw new IllegalArgumentException( "startPlex (" + startPlex
  39. + ") is not type of FIELD_BEGIN" );
  40. if ( separatorPlex != null
  41. && separatorPlex.getFld().getBoundaryType() != FieldDescriptor.FIELD_SEPARATOR_MARK )
  42. throw new IllegalArgumentException( "separatorPlex" + separatorPlex
  43. + ") is not type of FIELD_SEPARATOR" );
  44. if ( endPlex.getFld().getBoundaryType() != FieldDescriptor.FIELD_END_MARK )
  45. throw new IllegalArgumentException( "endPlex (" + endPlex
  46. + ") is not type of FIELD_END" );
  47. this.startPlex = startPlex;
  48. this.separatorPlex = separatorPlex;
  49. this.endPlex = endPlex;
  50. }
  51. public Range firstSubrange( Range parent )
  52. {
  53. if ( hasSeparator() )
  54. {
  55. if ( getMarkStartOffset() + 1 == getMarkSeparatorOffset() )
  56. return null;
  57. return new Range( getMarkStartOffset() + 1,
  58. getMarkSeparatorOffset(), parent )
  59. {
  60. @Override
  61. public String toString()
  62. {
  63. return "FieldSubrange1 (" + super.toString() + ")";
  64. }
  65. };
  66. }
  67. if ( getMarkStartOffset() + 1 == getMarkEndOffset() )
  68. return null;
  69. return new Range( getMarkStartOffset() + 1, getMarkEndOffset(), parent )
  70. {
  71. @Override
  72. public String toString()
  73. {
  74. return "FieldSubrange1 (" + super.toString() + ")";
  75. }
  76. };
  77. }
  78. /**
  79. * @return character position of first character after field (i.e.
  80. * {@link #getMarkEndOffset()} + 1)
  81. */
  82. public int getFieldEndOffset()
  83. {
  84. /*
  85. * sometimes plex looks like [100, 2000), where 100 is the position of
  86. * field-end character, and 2000 - some other char position, far away
  87. * from field (not inside). So taking into account only start --sergey
  88. */
  89. return endPlex.getFcStart() + 1;
  90. }
  91. /**
  92. * @return character position of first character in field (i.e.
  93. * {@link #getFieldStartOffset()})
  94. */
  95. public int getFieldStartOffset()
  96. {
  97. return startPlex.getFcStart();
  98. }
  99. public CharacterRun getMarkEndCharacterRun( Range parent )
  100. {
  101. return new Range( getMarkEndOffset(), getMarkEndOffset() + 1, parent )
  102. .getCharacterRun( 0 );
  103. }
  104. /**
  105. * @return character position of end field mark
  106. */
  107. public int getMarkEndOffset()
  108. {
  109. return endPlex.getFcStart();
  110. }
  111. public CharacterRun getMarkSeparatorCharacterRun( Range parent )
  112. {
  113. if ( !hasSeparator() )
  114. return null;
  115. return new Range( getMarkSeparatorOffset(),
  116. getMarkSeparatorOffset() + 1, parent ).getCharacterRun( 0 );
  117. }
  118. /**
  119. * @return character position of separator field mark (if present,
  120. * {@link NullPointerException} otherwise)
  121. */
  122. public int getMarkSeparatorOffset()
  123. {
  124. return separatorPlex.getFcStart();
  125. }
  126. public CharacterRun getMarkStartCharacterRun( Range parent )
  127. {
  128. return new Range( getMarkStartOffset(), getMarkStartOffset() + 1,
  129. parent ).getCharacterRun( 0 );
  130. }
  131. /**
  132. * @return character position of start field mark
  133. */
  134. public int getMarkStartOffset()
  135. {
  136. return startPlex.getFcStart();
  137. }
  138. public int getType()
  139. {
  140. return startPlex.getFld().getFieldType();
  141. }
  142. public boolean hasSeparator()
  143. {
  144. return separatorPlex != null;
  145. }
  146. public boolean isHasSep()
  147. {
  148. return endPlex.getFld().isFHasSep();
  149. }
  150. public boolean isLocked()
  151. {
  152. return endPlex.getFld().isFLocked();
  153. }
  154. public boolean isNested()
  155. {
  156. return endPlex.getFld().isFNested();
  157. }
  158. public boolean isPrivateResult()
  159. {
  160. return endPlex.getFld().isFPrivateResult();
  161. }
  162. public boolean isResultDirty()
  163. {
  164. return endPlex.getFld().isFResultDirty();
  165. }
  166. public boolean isResultEdited()
  167. {
  168. return endPlex.getFld().isFResultEdited();
  169. }
  170. public boolean isZombieEmbed()
  171. {
  172. return endPlex.getFld().isFZombieEmbed();
  173. }
  174. public Range secondSubrange( Range parent )
  175. {
  176. if ( !hasSeparator()
  177. || getMarkSeparatorOffset() + 1 == getMarkEndOffset() )
  178. return null;
  179. return new Range( getMarkSeparatorOffset() + 1, getMarkEndOffset(),
  180. parent )
  181. {
  182. @Override
  183. public String toString()
  184. {
  185. return "FieldSubrange2 (" + super.toString() + ")";
  186. }
  187. };
  188. }
  189. @Override
  190. public String toString()
  191. {
  192. return "Field [" + getFieldStartOffset() + "; " + getFieldEndOffset()
  193. + "] (type: 0x" + Integer.toHexString( getType() ) + " = "
  194. + getType() + " )";
  195. }
  196. }