Example usage for java.text AttributedCharacterIterator setIndex

List of usage examples for java.text AttributedCharacterIterator setIndex

Introduction

In this page you can find the example usage for java.text AttributedCharacterIterator setIndex.

Prototype

public char setIndex(int position);

Source Link

Document

Sets the position to the specified position in the text and returns that character.

Usage

From source file:Main.java

/**
 * Serialises an <code>AttributedString</code> object.
 *
 * @param as  the attributed string object (<code>null</code> permitted).
 * @param stream  the output stream (<code>null</code> not permitted).
 *
 * @throws IOException if there is an I/O error.
 *///from   w  ww .  j  a v  a 2  s  . c o  m
public static void writeAttributedString(AttributedString as, ObjectOutputStream stream) throws IOException {

    if (stream == null) {
        throw new IllegalArgumentException("Null 'stream' argument.");
    }
    if (as != null) {
        stream.writeBoolean(false);
        AttributedCharacterIterator aci = as.getIterator();
        // build a plain string from aci
        // then write the string
        StringBuffer plainStr = new StringBuffer();
        char current = aci.first();
        while (current != CharacterIterator.DONE) {
            plainStr = plainStr.append(current);
            current = aci.next();
        }
        stream.writeObject(plainStr.toString());

        // then write the attributes and limits for each run
        current = aci.first();
        int begin = aci.getBeginIndex();
        while (current != CharacterIterator.DONE) {
            // write the current character - when the reader sees that this
            // is not CharacterIterator.DONE, it will know to read the
            // run limits and attributes
            stream.writeChar(current);

            // now write the limit, adjusted as if beginIndex is zero
            int limit = aci.getRunLimit();
            stream.writeInt(limit - begin);

            // now write the attribute set
            Map atts = new HashMap(aci.getAttributes());
            stream.writeObject(atts);
            current = aci.setIndex(limit);
        }
        // write a character that signals to the reader that all runs
        // are done...
        stream.writeChar(CharacterIterator.DONE);
    } else {
        // write a flag that indicates a null
        stream.writeBoolean(true);
    }

}

From source file:net.sf.jasperreports.engine.util.JRStyledTextParser.java

/**
 * Outputs a styled text String given a set of element-level styled text
 * attributes and a styled text in the form of a String text and an iterator
 * of style attributes./*ww w. j a  va  2 s  .  c om*/
 * 
 * @param parentAttrs the element-level styled text attributes
 * @param iterator iterator of styled text attributes
 * @param text the text
 * @return the String styled text representation
 */
public String write(Map<Attribute, Object> parentAttrs, AttributedCharacterIterator iterator, String text) {
    StringBuilder sb = new StringBuilder();

    int runLimit = 0;

    while (runLimit < iterator.getEndIndex() && (runLimit = iterator.getRunLimit()) <= iterator.getEndIndex()) {
        String chunk = text.substring(iterator.getIndex(), runLimit);
        Map<Attribute, Object> attrs = iterator.getAttributes();

        StringBuilder styleBuilder = writeStyleAttributes(parentAttrs, attrs);
        if (styleBuilder.length() > 0) {
            sb.append(LESS);
            sb.append(NODE_style);
            sb.append(styleBuilder.toString());
            sb.append(GREATER);
            writeChunk(sb, parentAttrs, attrs, chunk);
            sb.append(LESS_SLASH);
            sb.append(NODE_style);
            sb.append(GREATER);
        } else {
            writeChunk(sb, parentAttrs, attrs, chunk);
        }

        iterator.setIndex(runLimit);
    }

    return sb.toString();
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter.java

/**
 *
 *///from  w w  w.j a v a 2  s.  c  o  m
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, boolean isStyledText) {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), locale,
                invalidCharReplacement, isStyledText);

        iterator.setIndex(runLimit);
    }
}

From source file:com.siteview.ecc.report.xls.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {/*from   www. j av  a  2  s .  c o m*/
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getNearestColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRDocxExporter.java

/**
 *
 *///from ww w.j a va2s . c  om
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, boolean hiddenText,
        boolean startedHyperlink, boolean isNewLineJustified) {
    Color elementBackcolor = null;
    Map<AttributedCharacterIterator.Attribute, Object> globalAttributes = styledText.getGlobalAttributes();
    if (globalAttributes != null) {
        elementBackcolor = (Color) styledText.getGlobalAttributes().get(TextAttribute.BACKGROUND);
    }

    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();

        boolean localHyperlink = false;

        if (!startedHyperlink) {
            JRPrintHyperlink hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK);
            if (hyperlink != null) {
                localHyperlink = startHyperlink(hyperlink, true);
            }
        }

        runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), locale,
                hiddenText, invalidCharReplacement, elementBackcolor, isNewLineJustified);

        if (localHyperlink) {
            endHyperlink(true);
        }

        iterator.setIndex(runLimit);
    }
}

From source file:net.sf.jasperreports.engine.export.ooxml.JRPptxExporter.java

/**
 *
 *///  w ww.j a  v a2  s .  c o  m
protected void exportStyledText(JRStyle style, JRStyledText styledText, Locale locale, String fieldType,
        String uuid) {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        if (fieldType != null) {
            runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit),
                    locale, invalidCharReplacement, fieldType, uuid);
        } else {
            runHelper.export(style, iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit),
                    locale, invalidCharReplacement);

        }

        iterator.setIndex(runLimit);
    }
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

protected HSSFRichTextString getRichTextString(JRStyledText styledText, short forecolor, JRFont defaultFont,
        Locale locale) {//from ww w .  j a  v  a 2 s .  co m
    String text = styledText.getText();
    HSSFRichTextString richTextStr = new HSSFRichTextString(text);
    int runLimit = 0;
    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();
        JRFont runFont = attributes.isEmpty() ? defaultFont : new JRBaseFont(attributes);
        short runForecolor = attributes.get(TextAttribute.FOREGROUND) != null
                ? getWorkbookColor((Color) attributes.get(TextAttribute.FOREGROUND)).getIndex()
                : forecolor;
        HSSFFont font = getLoadedFont(runFont, runForecolor, attributes, locale);
        richTextStr.applyFont(iterator.getIndex(), runLimit, font);
        iterator.setIndex(runLimit);
    }
    return richTextStr;
}

From source file:net.sf.jasperreports.engine.export.JRHtmlExporter.java

/**
 *
 *///from  w w w .  j  a v  a  2 s . c o m
protected void exportStyledText(JRPrintText printText, JRStyledText styledText, String tooltip)
        throws IOException {
    Locale locale = getTextLocale(printText);
    LineSpacingEnum lineSpacing = printText.getParagraph().getLineSpacing();
    Float lineSpacingSize = printText.getParagraph().getLineSpacingSize();
    float lineSpacingFactor = printText.getLineSpacingFactor();
    Color backcolor = printText.getBackcolor();

    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    boolean first = true;
    boolean startedSpan = false;
    while (runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
        //if there are several text runs, write the tooltip into a parent <span>
        if (first && runLimit < styledText.length() && tooltip != null) {
            startedSpan = true;
            writer.write("<span title=\"");
            writer.write(JRStringUtil.xmlEncode(tooltip));
            writer.write("\">");
            //reset the tooltip so that inner <span>s to not use it
            tooltip = null;
        }
        first = false;

        exportStyledTextRun(iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), tooltip,
                locale, lineSpacing, lineSpacingSize, lineSpacingFactor, backcolor);

        iterator.setIndex(runLimit);
    }

    if (startedSpan) {
        writer.write("</span>");
    }
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

/**
 *
 *///from w w  w. ja  va 2  s . c  o  m
protected Phrase getPhrase(AttributedString as, String text, JRPrintText textElement) {
    Phrase phrase = new Phrase();
    int runLimit = 0;

    AttributedCharacterIterator iterator = as.getIterator();
    Locale locale = getTextLocale(textElement);

    boolean firstChunk = true;
    while (runLimit < text.length() && (runLimit = iterator.getRunLimit()) <= text.length()) {
        Map<Attribute, Object> attributes = iterator.getAttributes();
        Chunk chunk = getChunk(attributes, text.substring(iterator.getIndex(), runLimit), locale);

        if (firstChunk) {
            // only set anchor + bookmark for the first chunk in the text
            setAnchor(chunk, textElement, textElement);
        }

        JRPrintHyperlink hyperlink = textElement;
        if (hyperlink.getHyperlinkTypeValue() == HyperlinkTypeEnum.NONE) {
            hyperlink = (JRPrintHyperlink) attributes.get(JRTextAttribute.HYPERLINK);
        }

        setHyperlinkInfo(chunk, hyperlink);
        phrase.add(chunk);

        iterator.setIndex(runLimit);
        firstChunk = false;
    }

    return phrase;
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

protected boolean canUseGlyphRendering(JRPrintText text, JRStyledText styledText) {
    Locale locale = getTextLocale(text);
    AttributedCharacterIterator attributesIterator = styledText.getAttributedString().getIterator();
    int index = 0;
    while (index < styledText.length()) {
        FontKey fontKey = extractFontKey(attributesIterator.getAttributes(), locale);
        if (!fontKey.fontAttribute.hasAttribute()) {
            return false;
        }/* ww  w . j av  a  2s.co  m*/

        Boolean canUse = glyphRendererFonts.get(fontKey);
        if (canUse == null) {
            canUse = canUseGlyphRendering(fontKey);
            glyphRendererFonts.put(fontKey, canUse);
        }

        if (!canUse) {
            return false;
        }

        index = attributesIterator.getRunLimit();
        attributesIterator.setIndex(index);
    }
    return true;
}