Search in sources :

Example 6 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project es6draft by anba.

the class DateTimeFormatConstructor method PartitionDateTimePattern.

/**
 * 12.1.6 PartitionDateTimePattern ( dateTimeFormat, x )
 *
 * @param dateTimeFormat
 *            the date format object
 * @param date
 *            the date object
 * @return the formatted date-time object
 */
private static List<Map.Entry<String, String>> PartitionDateTimePattern(DateTimeFormatObject dateTimeFormat, Date date) {
    ArrayList<Map.Entry<String, String>> parts = new ArrayList<>();
    DateFormat dateFormat = dateTimeFormat.getDateFormat();
    AttributedCharacterIterator iterator = dateFormat.formatToCharacterIterator(date);
    StringBuilder sb = new StringBuilder();
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        sb.append(ch);
        if (iterator.getIndex() + 1 == iterator.getRunLimit()) {
            Iterator<Attribute> keyIterator = iterator.getAttributes().keySet().iterator();
            String key;
            if (keyIterator.hasNext()) {
                key = fieldToString((DateFormat.Field) keyIterator.next());
            } else {
                key = "literal";
            }
            String value = sb.toString();
            sb.setLength(0);
            parts.add(new AbstractMap.SimpleImmutableEntry<>(key, value));
        }
    }
    return parts;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) ArrayList(java.util.ArrayList) AttributedCharacterIterator(java.text.AttributedCharacterIterator) AbstractMap(java.util.AbstractMap) DateField(com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.DateField) DateFormat(com.ibm.icu.text.DateFormat) SimpleDateFormat(com.ibm.icu.text.SimpleDateFormat)

Example 7 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project j2objc by google.

the class AttributedCharacterIteratorAttributeTest method test_readResolve.

/**
 * @tests java.text.AttributedCharacterIterator$Attribute#readResolve()
 */
public void test_readResolve() {
    // test for method java.lang.Object readResolve()
    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        out = new ObjectOutputStream(bytes);
        AttributedCharacterIterator.Attribute dattribute, dattribute2;
        MyAttribute attribute;
        // a regular instance of DateFormat.Field
        dattribute = AttributedCharacterIterator.Attribute.LANGUAGE;
        // a subclass instance with null name
        attribute = new MyAttribute(null);
        out.writeObject(dattribute);
        try {
            out.writeObject(attribute);
        } catch (NotSerializableException e) {
        }
        in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
        try {
            dattribute2 = (AttributedCharacterIterator.Attribute) in.readObject();
            assertSame("resolved incorrectly", dattribute, dattribute2);
        } catch (IllegalArgumentException e) {
            fail("Unexpected IllegalArgumentException: " + e);
        }
    } catch (IOException e) {
        fail("unexpected IOException" + e);
    } catch (ClassNotFoundException e) {
        fail("unexpected ClassNotFoundException" + e);
    } finally {
        try {
            if (out != null)
                out.close();
            if (in != null)
                in.close();
        } catch (IOException e) {
        }
    }
}
Also used : NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Attribute(java.text.AttributedCharacterIterator.Attribute) ObjectInputStream(java.io.ObjectInputStream) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 8 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project sis by apache.

the class FormattedCharacterIteratorTest method testAttributes.

/**
 * Tests an iteration with attributes, optionally having {@code INTEGER} attributes
 * overlapping the {@code DEGREES}/{@code MINUTES}/{@code SECONDS} ones. The given
 * iterator shall iterates over the {@code "45°30′15.0″N"} characters.
 *
 * <p>This test is leveraged by {@link AngleFormatTest#testFormatToCharacterIterator()}.</p>
 */
@SuppressWarnings("fallthrough")
static void testAttributes(final AttributedCharacterIterator it, final boolean withNumberFields) {
    assertEquals(getAllAttributeKeys(withNumberFields), it.getAllAttributeKeys());
    assertEquals(0, it.getIndex());
    assertEquals(3, it.getRunLimit(MINUTES));
    assertEquals(6, it.getRunLimit(SECONDS));
    for (char c = it.first(); c != DONE; c = it.next()) {
        final int index = it.getIndex();
        final AngleFormat.Field key;
        final Comparable<?> value;
        final int start, limit;
        if (index < 3) {
            key = DEGREES;
            value = 45;
            start = 0;
            limit = 3;
        } else if (index < 6) {
            key = MINUTES;
            value = 30;
            start = 3;
            limit = 6;
        } else if (index < 11) {
            key = SECONDS;
            value = 15f;
            start = 6;
            limit = 11;
        } else {
            key = HEMISPHERE;
            value = 'N';
            start = 11;
            limit = 12;
        }
        /*
             * Expected values when asking for a NumberFormat field.
             * Initialized to the values when no such field exists,
             * then updated if overlapping fields are allowed.
             */
        boolean isInteger = false;
        boolean isSeparator = false;
        boolean isFraction = false;
        int startInteger = 0;
        int limitInteger = 12;
        int startSeparator = 0;
        int limitSeparator = 12;
        int startFraction = 0;
        int limitFraction = 12;
        int numAttributes = 1;
        if (withNumberFields) {
            /*
                 * Update the above expected values when the current position
                 * is inside a NumberFormat field.
                 */
            switch(index) {
                // Degrees
                case 0:
                // Degrees
                case 1:
                // Minutes
                case 3:
                // Minutes
                case 4:
                case 6:
                case 7:
                    isInteger = true;
                    numAttributes = 2;
                    startInteger = start;
                    limitInteger = 2 + start;
                    break;
                case 8:
                    isSeparator = true;
                    numAttributes = 2;
                    startSeparator = 8;
                    limitSeparator = 9;
                    break;
                case 9:
                    isFraction = true;
                    numAttributes = 2;
                    startFraction = 9;
                    limitFraction = 10;
                    break;
            }
            /*
                 * Update the expected values for fields which are not at the current position.
                 * A search for a field should give the position where the next field start, or
                 * where the previous field ended.
                 */
            if (!isInteger) {
                if (index < 7) {
                    // End of previous integer field.
                    startInteger = index;
                    // Start of next integer field.
                    limitInteger = index + 1;
                } else {
                    // End of last integer field.
                    startInteger = 8;
                }
            }
            if (!isSeparator) {
                if (// Start of next separator field.
                index < 8)
                    // Start of next separator field.
                    limitSeparator = 8;
                else
                    // End of previous separator field.
                    startSeparator = 9;
            }
            if (!isFraction) {
                if (// Start of next fraction field.
                index < 9)
                    // Start of next fraction field.
                    limitFraction = 9;
                else
                    // End of previous fraction field.
                    startFraction = 10;
            }
        }
        final Map<Attribute, Object> attributes = it.getAttributes();
        assertEquals("attributes.size", numAttributes, attributes.size());
        assertEquals("attributes.get", value, attributes.get(key));
        assertEquals("attributes.get", isInteger, attributes.get(INTEGER) != null);
        assertEquals("attributes.get", isFraction, attributes.get(FRACTION) != null);
        assertEquals("attributes.get", isSeparator, attributes.get(DECIMAL_SEPARATOR) != null);
        assertEquals("getRunStart", start, it.getRunStart(key));
        assertEquals("getRunLimit", limit, it.getRunLimit(key));
        assertEquals("getRunStart", startInteger, it.getRunStart(INTEGER));
        assertEquals("getRunLimit", limitInteger, it.getRunLimit(INTEGER));
        assertEquals("getRunStart", startFraction, it.getRunStart(FRACTION));
        assertEquals("getRunLimit", limitFraction, it.getRunLimit(FRACTION));
        assertEquals("getRunStart", startSeparator, it.getRunStart(DECIMAL_SEPARATOR));
        assertEquals("getRunLimit", limitSeparator, it.getRunLimit(DECIMAL_SEPARATOR));
        assertEquals("getRunStart", max(max(max(startInteger, startSeparator), startFraction), start), it.getRunStart());
        assertEquals("getRunLimit", min(min(min(limitInteger, limitSeparator), limitFraction), limit), it.getRunLimit());
    }
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) Field(org.apache.sis.measure.AngleFormat.Field)

Example 9 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project j2objc by google.

the class Support_Format method findFields.

/**
 * finds attributes with regards to char index in this
 * AttributedCharacterIterator, and puts them in a vector
 *
 * @param iterator
 * @return a vector, each entry in this vector are of type FieldContainer,
 *       which stores start and end indexes and an attribute this range has
 */
protected static Vector<FieldContainer> findFields(AttributedCharacterIterator iterator) {
    Vector<FieldContainer> result = new Vector<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
        int start = iterator.getRunStart();
        int end = iterator.getRunLimit();
        Iterator<Attribute> it = iterator.getAttributes().keySet().iterator();
        while (it.hasNext()) {
            AttributedCharacterIterator.Attribute attribute = it.next();
            Object value = iterator.getAttribute(attribute);
            result.add(new FieldContainer(start, end, attribute, value));
        // System.out.println(start + " " + end + ": " + attribute + ",
        // " + value );
        // System.out.println("v.add(new FieldContainer(" + start +"," +
        // end +"," + attribute+ "," + value+ "));");
        }
        iterator.setIndex(end);
    }
    return result;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute) Attribute(java.text.AttributedCharacterIterator.Attribute) Vector(java.util.Vector) AttributedCharacterIterator(java.text.AttributedCharacterIterator)

Example 10 with Attribute

use of java.text.AttributedCharacterIterator.Attribute in project j2objc by google.

the class AttributedCharacterIteratorAttributeTest method test_fields.

/**
 * @tests java.text.AttributedCharacterIterator$Attribute#LANGUAGE
 * java.text.AttributedCharacterIterator$Attribute#READING
 * java.text.AttributedCharacterIterator$Attribute#INPUT_METHOD_SEGMENT
 */
public void test_fields() {
    // Just check that the fields are accessible as all
    // methods are protected
    Attribute language = Attribute.LANGUAGE;
    Attribute reading = Attribute.READING;
    Attribute inputMethodSegment = Attribute.INPUT_METHOD_SEGMENT;
}
Also used : Attribute(java.text.AttributedCharacterIterator.Attribute)

Aggregations

Attribute (java.text.AttributedCharacterIterator.Attribute)11 AttributedCharacterIterator (java.text.AttributedCharacterIterator)7 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)3 DateField (com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.DateField)2 DateFormat (com.ibm.icu.text.DateFormat)2 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Vector (java.util.Vector)2 NumberFormat (com.ibm.icu.text.NumberFormat)1 Toolkit (java.awt.Toolkit)1 TextAttribute (java.awt.font.TextAttribute)1 InputMethodHighlight (java.awt.im.InputMethodHighlight)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1