Package java.text

Examples of java.text.StringCharacterIterator


   *  <li>any other character is printed as-is</li>
   * </ul>
   */
  static String escapeText(String input) {
    StringBuilder builder = new StringBuilder(input.length());
    CharacterIterator iter = new StringCharacterIterator(input);
    for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
      switch(c) {
        case '\b':
          builder.append("\\b");
          break;
        case '\f':
          builder.append("\\f");
          break;
        case '\n':
          builder.append("\\n");
          break;
        case '\r':
          builder.append("\\r");
          break;
        case '\t':
          builder.append("\\t");
          break;
        case '\\':
          builder.append("\\\\");
          break;
        case '"':
          builder.append("\\\"");
          break;
        default:
          // Check for other control characters
          if(c >= 0x0000 && c <= 0x001F) {
            appendEscapedUnicode(builder, c);
          } else if(Character.isHighSurrogate(c)) {
            // Encode the surrogate pair using 2 six-character sequence (\\uXXXX\\uXXXX)
            appendEscapedUnicode(builder, c);
            c = iter.next();
            if(c == CharacterIterator.DONE) throw new IllegalArgumentException("invalid unicode string: unexpected high surrogate pair value without corresponding low value.");
            appendEscapedUnicode(builder, c);
          } else {
            // Anything else can be printed as-is
            builder.append(c);
View Full Code Here


  public void test (TestHarness harness)
    {
      harness.checkPoint ("spot checks");

      String recherche = "recherche";
      StringCharacterIterator sci = new StringCharacterIterator (recherche);

      harness.check (sci.getIndex (), 0);
      harness.check (sci.current (), 'r');
      harness.check (sci.getIndex (), 0);

      harness.check (sci.previous (), CharacterIterator.DONE);
      harness.check (sci.getIndex (), 0);

      int idx = recherche.length () - 1;
      harness.check (sci.setIndex (idx), 'e');
      harness.check (sci.getIndex (), idx);
      harness.check (sci.next (), CharacterIterator.DONE);
      harness.check (sci.current (), CharacterIterator.DONE);
      harness.check (sci.getIndex (), recherche.length ());

      harness.check (sci.first (), 'r');
      harness.check (sci.getIndex (), 0);

      harness.checkPoint ("full iteration");
      for (int i = 0; i < recherche.length () - 1; ++i)
  harness.check (sci.next (), recherche.charAt (i + 1));
      harness.check (sci.next (), CharacterIterator.DONE);
      harness.check (sci.setIndex (sci.getEndIndex ()),
         CharacterIterator.DONE);

      sci = new StringCharacterIterator ("");
      // 1.2, not 1.1.
      harness.check (sci.current (), CharacterIterator.DONE);
      harness.check (sci.previous (), CharacterIterator.DONE);
      harness.check (sci.next (), CharacterIterator.DONE);
    }
View Full Code Here

public class constructor implements Testlet
{
  public void test (TestHarness harness)
    {
      StringCharacterIterator sci = null;
      harness.checkPoint ("failing constructors");

      try
  {
    sci = new StringCharacterIterator (null);
  }
      catch (NullPointerException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator (null, 0);
  }
      catch (NullPointerException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator (null, 0, 0, 0);
  }
      catch (NullPointerException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator (null, 0);
  }
      catch (NullPointerException x)
  {
  }
          harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator (null, 0);
  }
      catch (NullPointerException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", -1);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", 9);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", -9, 0, 1);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", 0, -5, 1);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", 0, 1, -1);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", 5, 2, 3);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      sci = null;
      try
  {
    sci = new StringCharacterIterator ("ontology", 2, 5, 7);
  }
      catch (IllegalArgumentException x)
  {
  }
      harness.check (sci, null);

      // You could add a few more failure tests to be a bit more
      // complete, I suppose.  Feel free to add more to regression
      // test your implementation.

      harness.checkPoint ("successful constructors");

      sci = new StringCharacterIterator ("ontology");
      harness.check (sci.getBeginIndex (), 0);
      harness.check (sci.getEndIndex (), 8);
      harness.check (sci.getIndex (), 0);

      sci = new StringCharacterIterator ("ontology", 5);
      harness.check (sci.getBeginIndex (), 0);
      harness.check (sci.getEndIndex (), 8);
      harness.check (sci.getIndex (), 5);

      sci = new StringCharacterIterator ("ontology", 0, 7, 3);
      harness.check (sci.getBeginIndex (), 0);
      harness.check (sci.getEndIndex (), 7);
      harness.check (sci.getIndex (), 3);

      harness.checkPoint ("clone");
      StringCharacterIterator s2 = (StringCharacterIterator) sci.clone ();
      harness.check (s2.getBeginIndex (), 0);
      harness.check (s2.getEndIndex (), 7);
      harness.check (s2.getIndex (), 3);
      harness.check (sci.equals (s2));
    }
View Full Code Here

                            lineWidth = 0;
                            lastWhitespaceIndex = -1;

                            // Append the current line
                            if ((i - 1) - start > 0) {
                                StringCharacterIterator line = new StringCharacterIterator(text, start, i, start);
                                GlyphVector glyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, line);
                                glyphVectors.add(glyphVector);

                                Rectangle2D textBounds = glyphVector.getLogicalBounds();
                                textHeight += textBounds.getHeight();
                            }

                            start = i + 1;
                        }

                        i++;
                    }

                    // Append the final line
                    if ((i - 1) - start > 0) {
                        StringCharacterIterator line = new StringCharacterIterator(text, start, i, start);
                        GlyphVector glyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, line);
                        glyphVectors.add(glyphVector);

                        Rectangle2D textBounds = glyphVector.getLogicalBounds();
                        textHeight += textBounds.getHeight();
View Full Code Here

                    StringBuilder buf = new StringBuilder(n);
                    for (int i = 0; i < n; i++) {
                        buf.append(BULLET);
                    }

                    ci = new StringCharacterIterator(buf.toString());
                } else {
                    ci= textNode.getCharacterIterator();
                }

                glyphVector = font.createGlyphVector(FONT_RENDER_CONTEXT, ci);
View Full Code Here

            // Not big enough to have an encoded sequence
            return encodedText;
        }
        StringBuilder sb = new StringBuilder();
        char[] digits = new char[4];
        CharacterIterator iter = new StringCharacterIterator(encodedText);
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if (c == '_') {
                // Read the next character, if there is one ...
                char next = iter.next();
                if (next == CharacterIterator.DONE) {
                    sb.append(c);
                    break;
                }
                // If the next character is not 'x', then these are just regular characters ...
                if (next != 'x') {
                    sb.append(c).append(next);
                    continue;
                }
                // Read the next 4 characters (digits) and another '_' character ...
                digits[0] = iter.next();
                if (digits[0] == CharacterIterator.DONE) {
                    sb.append(c).append(next);
                    break;
                }
                digits[1] = iter.next();
                if (digits[1] == CharacterIterator.DONE) {
                    sb.append(c).append(next).append(digits, 0, 1);
                    break;
                }
                digits[2] = iter.next();
                if (digits[2] == CharacterIterator.DONE) {
                    sb.append(c).append(next).append(digits, 0, 2);
                    break;
                }
                digits[3] = iter.next();
                if (digits[3] == CharacterIterator.DONE) {
                    sb.append(c).append(next).append(digits, 0, 3);
                    break;
                }
                char underscore = iter.next();
                if (underscore != '_') { // includes DONE
                    sb.append(c).append(next).append(digits, 0, 4);
                    if (underscore == CharacterIterator.DONE) break;
                    sb.append(underscore);
                    continue;
View Full Code Here

    public String encode( String text ) {
        if (text == null) return null;
        if (text.length() == 0) return text;
        StringBuilder sb = new StringBuilder();
        String hex = null;
        CharacterIterator iter = new StringCharacterIterator(text);
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if (c == '_') {
                // Read the next character (if there is one) ...
                char next = iter.next();
                if (next == CharacterIterator.DONE) {
                    sb.append(c);
                    break;
                }
                // If the next character is not 'x', then these are just regular characters ...
View Full Code Here

     * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String)
     */
    public String encode( String text ) {
        if (text == null) return null;
        StringBuilder sb = new StringBuilder();
        CharacterIterator iter = new StringCharacterIterator(text);
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            switch (c) {
                case '&':
                    sb.append("&amp;");
                    break;
                case '"':
View Full Code Here

     * @see org.jboss.dna.common.text.TextDecoder#decode(java.lang.String)
     */
    public String decode( String encodedText ) {
        if (encodedText == null) return null;
        StringBuilder sb = new StringBuilder();
        CharacterIterator iter = new StringCharacterIterator(encodedText);
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if (c == '&') {
                int index = iter.getIndex();
               
                do {
                    c = iter.next();
                }
                while (c != CharacterIterator.DONE && c != ';');

                // We found a closing semicolon
                if (c == ';') {
                    String s = encodedText.substring(index + 1, iter.getIndex());
                   
                    if (SPECIAL_ENTITIES.containsKey(s)) {
                        sb.append(SPECIAL_ENTITIES.get(s));
                        continue;
                       
                    }
                   
                    if (s.length() > 0 && s.charAt(0) == '#') {
                        try {
                            sb.append((char) Short.parseShort(s.substring(1, s.length())));
                            continue;
                        }
                        catch (NumberFormatException nfe) {
                            // This is possible in malformed encodings, but let it fall through
                        }
                    }
                }
               
                // Malformed encoding, restore state and pass poorly encoded data back
                c = '&';
                iter.setIndex(index);                           
            }

            sb.append(c);

        }
View Full Code Here

     * {@inheritDoc}
     */
    public String encode( String publicName ) {
        if (publicName == null) return null;
        StringBuilder sb = new StringBuilder();
        CharacterIterator iter = new StringCharacterIterator(publicName);
        for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            char mapped = c;
            if (c == '*') {
                mapped = '\uF02A';
            } else if (c == '/') {
                mapped = '\uF02F';
View Full Code Here

TOP

Related Classes of java.text.StringCharacterIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.