PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/ojc-core/encodersl/encoder-custom/src/com/sun/encoder/custom/runtime/provider/Delim.java

https://bitbucket.org/pymma/openesb-components
Java | 407 lines | 160 code | 36 blank | 211 comment | 39 complexity | aab807bd90ac7204b63dbb2c32e32b35 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)Delim.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.encoder.custom.runtime.provider;
  30. import com.sun.encoder.runtime.provider.Misc;
  31. import java.nio.ByteBuffer;
  32. /**
  33. * Class to represent a single delimiter for input data parsing.
  34. * A delimiter is a byte sequence that will be matched in input data
  35. * to find the end of a field. Each delimiter has a precedence to
  36. * resolve conflicts where one delimiter is a prefix of another one.
  37. * Instances of this class are immutable.
  38. *
  39. * @author Michael Libourel
  40. * @author Jun Xu
  41. */
  42. public final class Delim implements com.sun.encoder.custom.runtime.Delim {
  43. /**
  44. * The delimiter type, a constant from Delim.Type.
  45. */
  46. public final Delim.Type mType;
  47. /**
  48. * The use of the delimiter as a terminater; see Delim.Mode.
  49. */
  50. public final Delim.Mode mTermMode;
  51. /**
  52. * The use of the delimiter for absent fields; see Delim.Mode.
  53. */
  54. public final Delim.Mode mOptMode;
  55. /**
  56. * For delimiters derived from the input message (a.k.a. embedded
  57. * delimiters) this contains an index into an OTD instance specific
  58. * run-time set of delimiter strings found in the input. For constant
  59. * delimiters, it is -1.
  60. */
  61. public short mSlotIndex;
  62. /**
  63. * The bytes that make up the delimiter. Minimum length is 1.
  64. * For embedded delimiters, this will be null, and the sequence is taken
  65. * from the input slot given by mSlotIndex instead.
  66. */
  67. public byte[] mBytes;
  68. /**
  69. * The delimiter precedence. In case of conflict (i.e. input will
  70. * match two or more delimiters), the one with the highest precedence
  71. * wins. Precedence being equal, the innermost (last addition) wins.
  72. */
  73. public final byte mPrecedence;
  74. /**
  75. * If multiple consecutive delimiters should be skipped. For example,
  76. * assuming data is " AAA ", the delimiter is ' ' (Space character) and
  77. * the current position is at 0, if this flag is true, the match will not
  78. * stop at position 0, but for begin delimiter it will stop at position 3
  79. * (all consecutive begin delimiters will be skipped) and for end delimiter
  80. * it will stop at position 6 (all consecutive leading end delimiters will
  81. * be skipped and the data will be parsed and delimiter scan stops at the
  82. * first delimiter after the data).
  83. *
  84. * |0123456|
  85. * | AAA |
  86. */
  87. public final boolean mIsSkipLeading;
  88. /**
  89. * If multiple end delimiters should be collapsed into one. For example,
  90. * assuming data is "AAA XYZ ", the delimiter is ' ' (Space) and the
  91. * current position is at 0, if this flag is on, the delimiter scan will
  92. * not stop at position 3, but at position 5. Next scan will stop at
  93. * position 12 instead of 9.
  94. *
  95. * |0123456789012|
  96. * |AAA XYZ |
  97. */
  98. public final boolean mIsCollapse;
  99. /**
  100. * The slot index of the begin delimiter if it is embedded.
  101. */
  102. public short mBeginSlotIndex;
  103. /**
  104. * The bytes of begin delimiter.
  105. */
  106. public byte[] mBeginDelim;
  107. /**
  108. * Flag indicates if the begin delimiter must be anchored.
  109. */
  110. public boolean mIsBeginDelimAnchored;
  111. /**
  112. * Flag indicates if the end delimiter must be anchored.
  113. */
  114. public boolean mEndDelimAnchored;
  115. /**
  116. * An embedded delimiter slot describes a delimiter that is taken from
  117. * the input message itself, at a fixed absolute offset, preceding
  118. * any use of the delimiter in the message.
  119. * Instances of this class are immutable.
  120. */
  121. public static class Slot {
  122. public final long mOffset;
  123. public final int mLength;
  124. /**
  125. * Creates from offset and length.
  126. *
  127. * @param offset the absolute offset, in bytes
  128. * @param length the length, a positive number
  129. */
  130. public Slot(final long offset, final int length) {
  131. if (offset < 0) {
  132. throw new IllegalArgumentException("negative offset: "
  133. + offset);
  134. }
  135. if (length <= 0) {
  136. throw new IllegalArgumentException("non-positive length: "
  137. + length);
  138. }
  139. mOffset = offset;
  140. mLength = length;
  141. }
  142. @Override
  143. public String toString() {
  144. StringBuffer buff = new StringBuffer("Slot@")
  145. .append(Integer.toHexString(hashCode()));
  146. buff.append(" offset=").append(mOffset);
  147. buff.append(" length=").append(mLength);
  148. return buff.toString();
  149. }
  150. }
  151. /**
  152. * Contructs from delimiter data and precedence level.
  153. *
  154. * @param type the delimiter type, from Delim.Type
  155. * @param precedence non-negative precedence value
  156. * @param termMode terminator use, from Delim.Mode
  157. * @param optMode use en lieu of absent field, from Delim.Mode
  158. * @param slotIndex the OtdDelim.mSlots[] index, for embedded delimiters, or -1
  159. * @param data the non-empty delimiter, for constant delimiters, or null
  160. * @param isSkipLeading if multiple consecutive begin delimiters should be
  161. * skipped
  162. * @param isCollapse if multiple consecutive end delimiters should be collapsed
  163. * @param beginSlotIndex the OtdDelim.mSlots[] index, for embedded begin
  164. * delimiters, or -1
  165. * @param beginDelim the non-empty begin delimiter, for constant delimiters,
  166. * or null
  167. * @param beginDelimAnchored if begin delimiter is anchored
  168. * @param endDelimAnchored if end delimiter is anchored
  169. */
  170. public Delim(Delim.Type type, byte precedence, Delim.Mode termMode,
  171. Delim.Mode optMode, short slotIndex, byte[] data, boolean isSkipLeading,
  172. boolean isCollapse, short beginSlotIndex, byte[] beginDelim,
  173. boolean beginDelimAnchored, boolean endDelimAnchored) {
  174. if (slotIndex < 0 && beginSlotIndex < 0 && data == null && beginDelim == null) {
  175. throw new NullPointerException("No delimiter data provided.");
  176. }
  177. if (data != null && data.length == 0) {
  178. throw new IllegalArgumentException("End delimiter must have at least 1 byte.");
  179. }
  180. if (beginDelim != null && beginDelim.length == 0) {
  181. throw new IllegalArgumentException("begin delimiter must have at least 1 byte.");
  182. }
  183. if (precedence < 0) {
  184. throw new IllegalArgumentException("Negative precedence is not allowed.");
  185. }
  186. mType = type;
  187. mTermMode = termMode;
  188. mOptMode = optMode;
  189. mSlotIndex = slotIndex;
  190. mBytes = data;
  191. mPrecedence = precedence;
  192. mIsSkipLeading = isSkipLeading;
  193. mIsCollapse = isCollapse;
  194. mBeginSlotIndex = beginSlotIndex;
  195. mBeginDelim = beginDelim;
  196. mIsBeginDelimAnchored = beginDelimAnchored;
  197. mEndDelimAnchored = endDelimAnchored;
  198. }
  199. @Override
  200. public String toString() {
  201. StringBuffer buf = new StringBuffer("Delim@")
  202. .append(Integer.toHexString(hashCode()));
  203. buf.append(" bytes=").append(Misc.printable(mBytes));
  204. if (mType != null) {
  205. buf.append(" type=").append(mType);
  206. }
  207. if (mTermMode != null) {
  208. buf.append(" termMode=").append(mTermMode);
  209. }
  210. if (mOptMode != null) {
  211. buf.append(" optMode=").append(mOptMode);
  212. }
  213. if (mSlotIndex >= 0) {
  214. buf.append(" slotIndex=").append(mSlotIndex);
  215. }
  216. buf.append(" precedence=").append(mPrecedence);
  217. buf.append(" isSkipLeading=").append(mIsSkipLeading);
  218. buf.append(" isCollapse=").append(mIsCollapse);
  219. if (mBeginSlotIndex >= 0) {
  220. buf.append(" beginSlotIndex=").append(mBeginSlotIndex);
  221. }
  222. if (mBeginDelim != null && mBeginDelim.length > 0) {
  223. buf.append(" beginDelim=").append(Misc.printable(mBeginDelim));
  224. buf.append(" beginDelimAnchored=").append(mIsBeginDelimAnchored);
  225. }
  226. buf.append(" endDelimAnchored=").append(mEndDelimAnchored);
  227. return buf.toString();
  228. }
  229. /**
  230. * Dump as shorter description of this delimiter.
  231. * @return shorter description of this delimiter.
  232. */
  233. public String dump() {
  234. StringBuffer buf = new StringBuffer("bytes=");
  235. if (isEmbedded()) {
  236. buf.append("embed#").append(mSlotIndex);
  237. } else {
  238. buf.append(Misc.printable(mBytes));
  239. }
  240. buf.append(" type=").append(mType);
  241. buf.append(" prec=").append(mPrecedence);
  242. buf.append(" termMode=").append(termMode());
  243. buf.append(" optMode=").append(optMode());
  244. if (mBeginDelim != null && mBeginDelim.length > 0) {
  245. buf.append(" begDelim=").append(Misc.printable(mBeginDelim));
  246. }
  247. return buf.toString();
  248. }
  249. /**
  250. * Constructs from delimiter type, data and precedence level.
  251. * This defaults to regular separator delimiters.
  252. *
  253. * @param type the delimiter type, from Delim.Type.
  254. * @param precedence non-negative precedence value
  255. * @param data the non-empty delimiter, embedded as ISO-8859-1 string
  256. */
  257. public Delim(final Delim.Type type, final byte precedence,
  258. final byte[] data) {
  259. this(type, precedence, Mode.NEVER, Mode.NEVER, (short) -1, data,
  260. false, false, (short) -1, null, true, true);
  261. }
  262. /**
  263. * Constructs from delimiter data and precedence level.
  264. * This defaults to regular separator delimiters.
  265. *
  266. * @param precedence non-negative precedence value
  267. * @param data the non-empty delimiter, embedded as ISO-8859-1 string
  268. */
  269. public Delim(final byte precedence, final byte[] data) {
  270. this(Type.NORMAL, precedence, data);
  271. }
  272. /**
  273. * Constructs from delimiter data and precedence level.
  274. * This defaults to regular separator delimiters.
  275. *
  276. * @param precedence non-negative precedence value
  277. * @param data the non-empty delimiter, embedded as ISO-8859-1 string
  278. */
  279. public Delim(final byte precedence, final String data) {
  280. this(precedence, Misc.str2bytes(data));
  281. }
  282. /**
  283. * Returns this Delim's type.
  284. * @return this Delim's type.
  285. */
  286. public Delim.Type type() {
  287. return mType;
  288. }
  289. /**
  290. * Returns this Delim's terminator mode.
  291. * @return this Delim's terminator mode.
  292. */
  293. public Delim.Mode termMode() {
  294. return mTermMode;
  295. }
  296. /**
  297. * Returns this Delim's optional mode.
  298. * @return this Delim's optional mode.
  299. */
  300. public Delim.Mode optMode() {
  301. return mOptMode;
  302. }
  303. /**
  304. * Tests whether delimiter is embedded.
  305. * Convenience functions for testing mSlotIndex.
  306. *
  307. * @return true if embedded, false if constant
  308. */
  309. public boolean isEmbedded () {
  310. return mSlotIndex >= 0;
  311. }
  312. /**
  313. * Tests whether leading delimiters should be skipped.
  314. *
  315. * @return <code>true</code> if should skip,
  316. * otherwise <code>false</code>
  317. */
  318. public boolean isSkipLeading() {
  319. return mIsSkipLeading;
  320. }
  321. /**
  322. * Tests whether should collapse multiple consecutive
  323. * delimiters into one.
  324. *
  325. * @return <code>true</code> if should isCollapse,
  326. * otherwise <code>false</code>
  327. */
  328. public boolean isCollapse() {
  329. return mIsCollapse;
  330. }
  331. /**
  332. * Tests if the delimiter has beginning bytes.
  333. *
  334. * @return <code>true</code> if has beginning bytes,
  335. * otherwise <code>false</code>
  336. */
  337. public boolean hasBeginBytes() {
  338. return mBeginDelim != null || mBeginSlotIndex >= 0;
  339. }
  340. /**
  341. * Tests if the delimiter has ending bytes.
  342. *
  343. * @return <code>true</code> if has ending bytes,
  344. * otherwise <code>false</code>
  345. */
  346. public boolean hasEndBytes() {
  347. return mBytes != null || mSlotIndex >= 0;
  348. }
  349. /**
  350. * Tests whether the begin delimiter is embedded.
  351. *
  352. * @return true if embedded, false if constant
  353. */
  354. public boolean isBeginDelimEmbedded() {
  355. return mBeginSlotIndex >= 0;
  356. }
  357. /**
  358. * Tests whether the Delim's terminator mode is <code>FORCE</code>.
  359. * @return true if the Delim's terminator mode is <code>FORCE</code>.
  360. */
  361. public boolean isForceTerm() {
  362. return mTermMode == com.sun.encoder.custom.runtime.Delim.Mode.FORCE;
  363. }
  364. /**
  365. * Tests whether the Delim's type is <code>ESCAPE_NEXT</code>.
  366. * @return true if the Delim's type is <code>ESCAPE_NEXT</code>.
  367. */
  368. public boolean isEscapeNext() {
  369. return mType == com.sun.encoder.custom.runtime.Delim.Type.ESCAPE_NEXT;
  370. }
  371. }