File: ContentToken.java

package info (click to toggle)
lib-xp-java 0.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,652 kB
  • ctags: 2,424
  • sloc: java: 8,085; makefile: 53; sh: 17; xml: 7
file content (130 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.jclark.xml.tok;

/**
 * Represents information returned by <code>Encoding.tokenizeContent</code>.
 * @see Encoding#tokenizeContent
 * @version $Revision: 1.3 $ $Date: 1998/02/17 04:23:51 $
 */
public class ContentToken extends Token {
  private static final int INIT_ATT_COUNT = 8;
  private int attCount = 0;
  private int[] attNameStart = new int[INIT_ATT_COUNT];
  private int[] attNameEnd = new int[INIT_ATT_COUNT];
  private int[] attValueStart = new int[INIT_ATT_COUNT];
  private int[] attValueEnd = new int[INIT_ATT_COUNT];
  private boolean[] attNormalized = new boolean[INIT_ATT_COUNT];

  /**
   * Returns the number of attributes specified in the start-tag
   * or empty element tag.
   */
  public final int getAttributeSpecifiedCount() {
    return attCount;
  }

  /**
   * Returns the index of the first character of the name of the
   * attribute index <code>i</code>.
   */
  public final int getAttributeNameStart(int i) {
    if (i >= attCount)
      throw new IndexOutOfBoundsException();
    return attNameStart[i];
  }

  /**
   * Returns the index following the last character of the name of the
   * attribute index <code>i</code>.
   */
  public final int getAttributeNameEnd(int i) {
    if (i >= attCount)
      throw new IndexOutOfBoundsException();
    return attNameEnd[i];
  }

  /**
   * Returns the index of the character following the opening quote of
   * attribute index <code>i</code>.
   */
  public final int getAttributeValueStart(int i) {
    if (i >= attCount)
      throw new IndexOutOfBoundsException();
    return attValueStart[i];
  }

  /**
   * Returns the index of the closing quote attribute index <code>i</code>.
   */
  public final int getAttributeValueEnd(int i) {
    if (i >= attCount)
      throw new IndexOutOfBoundsException();
    return attValueEnd[i];
  }

  /**
   * Returns true if attribute index <code>i</code> does not need to
   * be normalized.  This is an optimization that allows further processing
   * of the attribute to be avoided when it is known that normalization
   * cannot change the value of the attribute.
   */
  public final boolean isAttributeNormalized(int i) {
    if (i >= attCount)
      throw new IndexOutOfBoundsException();
    return attNormalized[i];
  }

  final void clearAttributes() {
    attCount = 0;
  }
  
  final void appendAttribute(int nameStart, int nameEnd,
			     int valueStart, int valueEnd,
			     boolean normalized) {
    if (attCount == attNameStart.length) {
      attNameStart = grow(attNameStart);
      attNameEnd = grow(attNameEnd);
      attValueStart = grow(attValueStart);
      attValueEnd = grow(attValueEnd);
      attNormalized = grow(attNormalized);
    }
    attNameStart[attCount] = nameStart;
    attNameEnd[attCount] = nameEnd;
    attValueStart[attCount] = valueStart;
    attValueEnd[attCount] = valueEnd;
    attNormalized[attCount] = normalized;
    ++attCount;
  }

  final void checkAttributeUniqueness(byte[] buf) throws InvalidTokenException {
    for (int i = 1; i < attCount; i++) {
      int len = attNameEnd[i] - attNameStart[i];
      for (int j = 0; j < i; j++) {
	if (attNameEnd[j] - attNameStart[j] == len) {
	  int n = len;
	  int s1 = attNameStart[i];
	  int s2 = attNameStart[j];
	  do {
	    if (--n < 0)
	      throw new InvalidTokenException(attNameStart[i],
					      InvalidTokenException.DUPLICATE_ATTRIBUTE);
	  } while (buf[s1++] == buf[s2++]);
	}
      }
    }
  }

  private static final int[] grow(int[] v) {
    int[] tem = v;
    v = new int[tem.length << 1];
    System.arraycopy(tem, 0, v, 0, tem.length);
    return v;
  }

  private static final boolean[] grow(boolean[] v) {
    boolean[] tem = v;
    v = new boolean[tem.length << 1];
    System.arraycopy(tem, 0, v, 0, tem.length);
    return v;
  }

}