Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/BulletStyleTest.java66
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/AlphabeticNumeral.java92
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/BulletStyle.java135
-rw-r--r--org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/RomanNumeral.java82
4 files changed, 375 insertions, 0 deletions
diff --git a/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/BulletStyleTest.java b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/BulletStyleTest.java
new file mode 100644
index 00000000..7ec2c65b
--- /dev/null
+++ b/org.eclipse.vex.core.tests/src/org/eclipse/vex/core/internal/css/BulletStyleTest.java
@@ -0,0 +1,66 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.css;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class BulletStyleTest {
+
+ @Test
+ public void decimalBulletText() throws Exception {
+ assertEquals("1.", BulletStyle.toDecimal(0));
+ assertEquals("9.", BulletStyle.toDecimal(8));
+ assertEquals("10.", BulletStyle.toDecimal(9));
+ }
+
+ @Test
+ public void decimalWithLeadingZerosBulletText() throws Exception {
+ assertEquals("1 digit", "1.", BulletStyle.toDecimalWithLeadingZeroes(0, 1));
+ assertEquals("2 digits", "01.", BulletStyle.toDecimalWithLeadingZeroes(0, 12));
+ assertEquals("3 digits", "001.", BulletStyle.toDecimalWithLeadingZeroes(0, 123));
+ assertEquals("4 digits", "0001.", BulletStyle.toDecimalWithLeadingZeroes(0, 1234));
+ }
+
+ @Test
+ public void lowerRomanBulletText() throws Exception {
+ assertEquals("i.", BulletStyle.toLowerRoman(0));
+ assertEquals("ii.", BulletStyle.toLowerRoman(1));
+ assertEquals("ix.", BulletStyle.toLowerRoman(8));
+ assertEquals("x.", BulletStyle.toLowerRoman(9));
+ }
+
+ @Test
+ public void upperRomanBulletText() throws Exception {
+ assertEquals("I.", BulletStyle.toUpperRoman(0));
+ assertEquals("II.", BulletStyle.toUpperRoman(1));
+ assertEquals("IX.", BulletStyle.toUpperRoman(8));
+ assertEquals("X.", BulletStyle.toUpperRoman(9));
+ }
+
+ @Test
+ public void lowerLatinBulletText() throws Exception {
+ assertEquals("a.", BulletStyle.toLowerLatin(0));
+ assertEquals("b.", BulletStyle.toLowerLatin(1));
+ assertEquals("y.", BulletStyle.toLowerLatin(24));
+ assertEquals("z.", BulletStyle.toLowerLatin(25));
+ assertEquals("aa.", BulletStyle.toLowerLatin(26));
+ assertEquals("ab.", BulletStyle.toLowerLatin(27));
+ assertEquals("ba.", BulletStyle.toLowerLatin(52));
+ assertEquals("zz.", BulletStyle.toLowerLatin(701));
+ assertEquals("aaa.", BulletStyle.toLowerLatin(702));
+ assertEquals("azz.", BulletStyle.toLowerLatin(1377));
+ assertEquals("baa.", BulletStyle.toLowerLatin(1378));
+ assertEquals("zzz.", BulletStyle.toLowerLatin(18277));
+ assertEquals("aaaa.", BulletStyle.toLowerLatin(18278));
+ }
+}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/AlphabeticNumeral.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/AlphabeticNumeral.java
new file mode 100644
index 00000000..51b4ba14
--- /dev/null
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/AlphabeticNumeral.java
@@ -0,0 +1,92 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.css;
+
+/**
+ * @author Florian Thienel
+ */
+public class AlphabeticNumeral {
+
+ private static final char[] LATIN_ALPHABET = "abcdefghijklmnopqrstuvwxyz".toCharArray();
+ private static final char[] GREEK_ALPHABET = "αβγδεζηθικλμνξοπρστυφχψω".toCharArray();
+
+ private final int value;
+ private final String numeral;
+
+ public static AlphabeticNumeral toLatin(final int value) {
+ return new AlphabeticNumeral(LATIN_ALPHABET, value);
+ }
+
+ public static AlphabeticNumeral toGreek(final int value) {
+ return new AlphabeticNumeral(GREEK_ALPHABET, value);
+ }
+
+ public AlphabeticNumeral(final char[] alphabet, final int value) {
+ this.value = value;
+ numeral = numeral(alphabet, value);
+ }
+
+ private static String numeral(final char[] alphabet, final int value) {
+ if (value <= 0) {
+ return "";
+ }
+ final StringBuilder numeral = new StringBuilder();
+
+ int i = value;
+ while (i > 0) {
+ final int remainder = (i - 1) % alphabet.length;
+ numeral.insert(0, alphabet[remainder]);
+ i = (i - 1) / alphabet.length;
+ }
+
+ return numeral.toString();
+ }
+
+ public int intValue() {
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return numeral;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + (numeral == null ? 0 : numeral.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final AlphabeticNumeral other = (AlphabeticNumeral) obj;
+ if (numeral == null) {
+ if (other.numeral != null) {
+ return false;
+ }
+ } else if (!numeral.equals(other.numeral)) {
+ return false;
+ }
+ return true;
+ }
+
+}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/BulletStyle.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/BulletStyle.java
new file mode 100644
index 00000000..69f27139
--- /dev/null
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/BulletStyle.java
@@ -0,0 +1,135 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.css;
+
+import org.eclipse.vex.core.internal.core.Image;
+
+public class BulletStyle {
+
+ public final Type type;
+ public final Position position;
+ public final Image image;
+ public final char character;
+
+ public static BulletStyle fromStyles(final Styles styles) {
+ return new BulletStyle(getBulletType(styles), Position.OUTSIDE, null, '\0');
+ }
+
+ private static Type getBulletType(final Styles styles) {
+ final String listStyleType = styles.getListStyleType();
+ if (CSS.DECIMAL.equals(listStyleType)) {
+ return Type.DECIMAL;
+ } else if (CSS.DECIMAL_LEADING_ZERO.equals(listStyleType)) {
+ return Type.DECIMAL_LEADING_ZERO;
+ } else if (CSS.LOWER_ROMAN.equals(listStyleType)) {
+ return Type.LOWER_ROMAN;
+ } else if (CSS.UPPER_ROMAN.equals(listStyleType)) {
+ return Type.UPPER_ROMAN;
+ } else if (CSS.LOWER_ALPHA.equals(listStyleType)) {
+ return Type.LOWER_LATIN;
+ } else if (CSS.UPPER_ALPHA.equals(listStyleType)) {
+ return Type.UPPER_LATIN;
+ } else if (CSS.LOWER_LATIN.equals(listStyleType)) {
+ return Type.LOWER_LATIN;
+ } else if (CSS.UPPER_LATIN.equals(listStyleType)) {
+ return Type.UPPER_LATIN;
+ } else if (CSS.LOWER_GREEK.equals(listStyleType)) {
+ return Type.LOWER_GREEK;
+ } else if (CSS.DISC.equals(listStyleType)) {
+ return Type.DISC;
+ } else if (CSS.CIRCLE.equals(listStyleType)) {
+ return Type.CIRCLE;
+ } else if (CSS.SQUARE.equals(listStyleType)) {
+ return Type.SQUARE;
+ } else {
+ return Type.NONE;
+ }
+ }
+
+ public BulletStyle(final Type type, final Position position, final Image image, final char character) {
+ this.type = type;
+ this.position = position;
+ this.image = image;
+ this.character = character;
+ }
+
+ public String getBulletAsText(final int zeroBasedIndex, final int itemCount) {
+ switch (type) {
+ case DECIMAL:
+ return toDecimal(zeroBasedIndex);
+ case DECIMAL_LEADING_ZERO:
+ return toDecimalWithLeadingZeroes(zeroBasedIndex, itemCount);
+ case LOWER_ROMAN:
+ return toLowerRoman(zeroBasedIndex);
+ case UPPER_ROMAN:
+ return toUpperRoman(zeroBasedIndex);
+ case LOWER_LATIN:
+ return toLowerLatin(zeroBasedIndex);
+ case UPPER_LATIN:
+ return toUpperLatin(zeroBasedIndex);
+ case LOWER_GREEK:
+ return toLowerGreek(zeroBasedIndex);
+ default:
+ return "";
+ }
+ }
+
+ public static String toDecimal(final int zeroBasedIndex) {
+ return String.format("%d.", zeroBasedIndex + 1);
+ }
+
+ public static String toDecimalWithLeadingZeroes(final int zeroBasedIndex, final int itemCount) {
+ final int digitCount = Integer.toString(itemCount).length();
+ return String.format("%0" + digitCount + "d.", zeroBasedIndex + 1);
+ }
+
+ public static String toLowerRoman(final int zeroBasedIndex) {
+ return toUpperRoman(zeroBasedIndex).toLowerCase();
+ }
+
+ public static String toUpperRoman(final int zeroBasedIndex) {
+ return new RomanNumeral(zeroBasedIndex + 1).toString() + ".";
+ }
+
+ public static String toLowerLatin(final int zeroBasedIndex) {
+ return AlphabeticNumeral.toLatin(zeroBasedIndex + 1).toString() + ".";
+ }
+
+ public static String toUpperLatin(final int zeroBasedIndex) {
+ return toLowerLatin(zeroBasedIndex).toUpperCase();
+ }
+
+ public static String toLowerGreek(final int zeroBasedIndex) {
+ return AlphabeticNumeral.toGreek(zeroBasedIndex + 1).toString() + ".";
+ }
+
+ public static enum Type {
+ NONE, DECIMAL, DECIMAL_LEADING_ZERO, LOWER_ROMAN, UPPER_ROMAN, LOWER_LATIN, UPPER_LATIN, LOWER_GREEK, DISC, CIRCLE, SQUARE;
+
+ public boolean isTextual() {
+ if (this == NONE) {
+ return false;
+ }
+ return ordinal() < DISC.ordinal();
+ }
+
+ public boolean isGraphical() {
+ if (this == NONE) {
+ return false;
+ }
+ return ordinal() >= DISC.ordinal();
+ }
+ }
+
+ public static enum Position {
+ INSIDE, OUTSIDE;
+ }
+}
diff --git a/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/RomanNumeral.java b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/RomanNumeral.java
new file mode 100644
index 00000000..da157db4
--- /dev/null
+++ b/org.eclipse.vex.core/src/org/eclipse/vex/core/internal/css/RomanNumeral.java
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * Copyright (c) 2016 Florian Thienel and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Florian Thienel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.vex.core.internal.css;
+
+public class RomanNumeral {
+
+ private final int value;
+ private final String numeral;
+
+ public RomanNumeral(final int value) {
+ this.value = value;
+ numeral = toRoman(value);
+ }
+
+ private static String toRoman(final int value) {
+ for (final Digit digit : Digit.values()) {
+ if (value >= digit.value) {
+ return digit.toString() + toRoman(value - digit.value);
+ }
+ }
+ return "";
+ }
+
+ public int intValue() {
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return numeral;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + (numeral == null ? 0 : numeral.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ final RomanNumeral other = (RomanNumeral) obj;
+ if (numeral == null) {
+ if (other.numeral != null) {
+ return false;
+ }
+ } else if (!numeral.equals(other.numeral)) {
+ return false;
+ }
+ return true;
+ }
+
+ private static enum Digit {
+
+ M(1000), CM(900), D(500), CD(400), C(100), XC(90), L(50), XL(40), X(10), IX(9), V(5), IV(4), I(1);
+
+ public final int value;
+
+ private Digit(final int value) {
+ this.value = value;
+ }
+ }
+
+}

Back to the top