All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.text.AutoFormatter Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2012 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.day.text;

import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Properties;

/**
 * The AutoFormatter class implements the automatic conversion
 * of line endings to <br> HTML lists. This is the formatting
 * subset of the UBM feature of the former Communiqué 2 system.
 * 

* This implementation only supports automatically converting bulleted and * numbered lists as well as line breaking. Therefore these formatting * options are not implemented here : {@link #FORMAT_NOISODEC}, * {@link #FORMAT_ISOENC}, {@link #FORMAT_AUTOLINK}, and {@link #FORMAT_URLENC}. * For replacements, use the {@link CodeISO} and/or java.net.URLEncoder * classes. */ public class AutoFormatter extends Format { //---------- format() modifier constants ----------------------------------- /** * ISO encodes the <, " and & characters. * @deprecated not implemented here, use {@link CodeISO} instead. */ public static final int FORMAT_NOISODEC = 0x0100; /** * ISO encode characters according to HTTP standard. * @deprecated not implemented here, use {@link CodeISO} instead. */ public static final int FORMAT_ISOENC = 0x0200; /** Automatically create ordered and unordered lists. */ public static final int FORMAT_AUTOLISTS = 0x0800; /** * Autmatic link formatting (?). * @deprecated this is not implemented and has no effects */ public static final int FORMAT_AUTOLINK = 0x1000; /** Automatically convert line breaks to <br> tags */ public static final int FORMAT_BR = 0x2000; /** * Encodes characters illegal in URLs. * @deprecated not implemented here, use {@link CodeISO} or * java.net.URLEncoder#encode(String) instead. */ public static final int FORMAT_URLENC = 0x4000; /** Combination of line break handling and automatic lists. */ public static final int FORMAT_AUTOBR = FORMAT_BR | FORMAT_AUTOLISTS; //---------- default values for AUTOBR handling ---------------------------- /** The default line beaking tag */ private static final String DEFAULT_BR = "
"; /** The default opening tag for ordered lists */ private static final String DEFAULT_TAG_OL_OPEN = "

    "; /** The default closing tag for ordered lists */ private static final String DEFAULT_TAG_OL_CLOSE = "
"; /** The default opening tag for items in ordered lists */ private static final String DEFAULT_TAG_OL_ITEM_OPEN = "
  • "; /** The default closing tag for items in ordered lists */ private static final String DEFAULT_TAG_OL_ITEM_CLOSE = "
  • "; /** The default opening tag for unordered lists */ private static final String DEFAULT_TAG_UL_OPEN = "
      "; /** The default closing tag for unordered lists */ private static final String DEFAULT_TAG_UL_CLOSE = "
    "; /** The default opening tag for items in unordered lists */ private static final String DEFAULT_TAG_UL_ITEM_OPEN = "
  • "; /** The default closing tag for items in unordered lists */ private static final String DEFAULT_TAG_UL_ITEM_CLOSE = "
  • "; /** The normal text formatting state - no list in action */ private static final int STATE_NORMAL = 1; /** State where a <ol> tag is currently open */ private static final int STATE_OL = 3; /** State where a <ul> tag is currently open */ private static final int STATE_UL = 4; //---------- from configuration -------------------------------------------- /** The tag used for line breaks, default {@link #DEFAULT_BR} */ private final String tagBr; /** The tag used to start ordered lists, default {@link #DEFAULT_TAG_OL_OPEN} */ private final String tagOlOpen; /** The tag used to end ordered lists, default {@link #DEFAULT_TAG_OL_CLOSE} */ private final String tagOlClose; /** The tag used to start items in ordered lists, default {@link #DEFAULT_TAG_OL_ITEM_OPEN} */ private final String tagOlItemOpen; /** The tag used to end items in ordered lists, default {@link #DEFAULT_TAG_OL_ITEM_CLOSE} */ private final String tagOlItemClose; /** The tag used to start unordered lists, default {@link #DEFAULT_TAG_UL_OPEN} */ private final String tagUlOpen; /** The tag used to end ordered lists, default {@link #DEFAULT_TAG_UL_OPEN} */ private final String tagUlClose; /** The tag used to start items in unordered lists, default {@link #DEFAULT_TAG_UL_ITEM_OPEN} */ private final String tagUlItemStart; /** The tag used to end items in unordered lists, default {@link #DEFAULT_TAG_UL_ITEM_CLOSE} */ private final String tagUlItemClose; /** * The default modifiers used by the * {@link #format(Object, StringBuffer, FieldPosition )} method. */ private final int DEFAULT_MODS = FORMAT_AUTOBR; /** * The system default formatter. Use this instance when formatting with * no special configuration is needed. */ public static final AutoFormatter DEFAULT_FORMATTER = new AutoFormatter(null); /** * Creates a new AutoFormatter object with the default * configuration. * * @deprecated as of echidna. To get an instance with default settings use * the {@link #DEFAULT_FORMATTER}. */ public AutoFormatter() { this(null); } /** * Creates a new AutoFormatter object with the given * configuration. *

    * The configuration contained in the config parameter is * assumed to have the following structure, where config.getName() * would return auto or config.getChild("auto") is * used as the configuration (this is the same structure as was used in * Communiqué 2 to configure automatic formatting. *

             
                
      " end="
    " >
    *
    *

    * All values are replaced as-is except for the ol.start * attribute which gets the string %s replaced with the number * which is used in the input to defined the ordered list. If the %s * string is missing that number is of course not inserted. * * @param config The configuration for the automatic formatting replacement. * If this is null, the defualt configuration is * used. */ public AutoFormatter(Properties config) { super(); // assert config is not null if (config == null) { config = new Properties(); } // configure from received or empty config tagBr = config.getProperty("/br.begin", DEFAULT_BR); tagOlOpen = config.getProperty("/ol.begin", DEFAULT_TAG_OL_OPEN); tagOlClose = config.getProperty("/ol.end", DEFAULT_TAG_OL_CLOSE); tagOlItemOpen = config.getProperty("/ol/item.begin", DEFAULT_TAG_OL_ITEM_OPEN); tagOlItemClose = config.getProperty("/ol/item.end", DEFAULT_TAG_OL_ITEM_CLOSE); tagUlOpen = config.getProperty("/ul.begin", DEFAULT_TAG_UL_OPEN); tagUlClose = config.getProperty("/ul.end", DEFAULT_TAG_UL_CLOSE); tagUlItemStart = config.getProperty("/ul/item.begin", DEFAULT_TAG_UL_ITEM_OPEN); tagUlItemClose = config.getProperty("/ul/item.end", DEFAULT_TAG_UL_ITEM_CLOSE); } //---------- Format abstract methods --------------------------------------- /** * Formats the object according to the standard modifiers, which are * automatic line breaks and list formatting. This implementation only * supports strings and completely ignores the pos parameter. * * @param obj The object to format, which must be a String or a * ClassCastException will be thrown. * @param toAppendTo Where to append the formatted data. If null, * a new string buffer is allocated. * @param pos Formatting position information. Not used or obeyed. * * @return a StringBuffer containing the formatted string. This * is either the same as toAppendTo or a newly * allocated StringBuffer if the parameter is * null. * * @throws ClassCastException if the object is not a String. */ public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { return format((String)obj, toAppendTo, DEFAULT_MODS); } /** * The AutoFormatter class does not support parsing, so an * UnsupportedOperationException is thrown when trying to * parse. * * @param source The source string to parse. Ignored. * @param status The position to define parsing. Ignored. * * @throws UnsupportedOperationException as it is not yet implemented. */ public Object parseObject(String source, ParsePosition status) { throw new UnsupportedOperationException("parse not implemented yet"); } //--------- AutoFormatter specialities ------------------------------------- /** * Formats the string according to the modifier set. The modifier set may be * any combination of the FORMAT constants defined. * * @param str The string to be formatted. * @param mods The modifier set controlling the format operation. * * @return the formatted string. */ public String format(String str, int mods) { return format(str, new StringBuffer(), mods).toString(); } /** * Formats the string according to the modifier set. The modifier set may be * any combination of the FORMAT constants defined. *

    * Implementation Note: Only line break processing and automatic * lists are currently supported. * * @param str The string to be formatted. * @param toAppendTo The string buffer to append the result to. If this * null a new string buffer is created and returned, * else the result of the method will be this string buffer. * @param mods The modifier set controlling the format operation. * * @return the string buffer to which the formatted result has been * appended. This is either the input string buffer or a newly * created string buffer, if null has been supplied. */ public StringBuffer format(String str, StringBuffer toAppendTo, int mods) { int beginCopy = 0; int state = STATE_NORMAL; boolean lineStart = true; char[] buffer = str.toCharArray(); int bufferLen = buffer.length; // assert the string buffer if (toAppendTo == null) toAppendTo = new StringBuffer(); for (int i=0; i='0' && c<='9'; c=buffer[++j]) { start.append(c); } if (j>i && j+1





    © 2015 - 2024 Weber Informatics LLC | Privacy Policy