Example usage for org.apache.poi.hwpf.model TextPiece getStringBuffer

List of usage examples for org.apache.poi.hwpf.model TextPiece getStringBuffer

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.model TextPiece getStringBuffer.

Prototype

@Deprecated
    public StringBuffer getStringBuffer() 

Source Link

Usage

From source file:b01.officeLink.ExtendedWordDocument.java

License:Apache License

public void replace(String oldString, String newString) {
    // hack to get the ending cp of the document, Have to revisit this.
    if (getHwpfDocument() != null) {
        TextPieceTable text = getHwpfDocument().getTextTable();
        List list = text.getTextPieces();

        for (int i = 0; i < list.size(); i++) {
            PropertyNode p = (PropertyNode) list.get(i);

            if (p instanceof TextPiece) {
                TextPiece tp = (TextPiece) p;
                StringBuffer buffer = tp.getStringBuffer();

                int start = buffer.indexOf(oldString);
                int end = start + oldString.length();

                replace(tp, start, end, newString);
            }/*from   ww  w  . j  av a  2  s .  co  m*/
        }
    }

}

From source file:b01.officeLink.ExtendedWordDocument.java

License:Apache License

public void replace(TextPiece textPiece, int start, int end, String newText) {
    int lengthToReplace = end - start;
    if (newText.length() < lengthToReplace) {
        int gap = lengthToReplace - newText.length();
        for (int i = 0; i < gap; i++) {
            if (i < gap / 2) {
                newText = " " + newText;
            } else {
                newText = newText + " ";
            }/*from www .ja v  a  2s  . com*/
        }
    }

    StringBuffer buffer = textPiece.getStringBuffer();

    buffer.replace(start, end, newText.substring(0, lengthToReplace));
    Range range = new Range(start, end, getHwpfDocument());
    range.insertAfter(newText.substring(lengthToReplace));
}

From source file:b01.officeLink.ExtendedWordDocument.java

License:Apache License

public void replace_XXXXX(String oldString, String newString) {
    // hack to get the ending cp of the document, Have to revisit this.
    if (getHwpfDocument() != null && getHwpfDocument().getTextTable() != null) {
        java.util.List text = getHwpfDocument().getTextTable().getTextPieces();
        for (int i = 0; i < text.size(); i++) {
            PropertyNode p = (PropertyNode) text.get(i);
            System.out.println("Property Node " + i + " = " + p);
            System.out.println("Start = " + p.getStart() + " End = " + p.getEnd());
            System.out.println("Class = " + p.getClass().getName());

            if (p instanceof TextPiece) {
                TextPiece tp = (TextPiece) p;
                StringBuffer buffer = tp.getStringBuffer();
                System.out.println("Buffer = " + buffer);

                int start = buffer.indexOf(oldString);
                int end = start + oldString.length();

                //buffer.indexOf(newString);        
                buffer.replace(start, end, newString);
                tp.setEnd(tp.getEnd() + (newString.length() - oldString.length()));
            }/*from  w ww .ja v  a  2  s  . com*/
        }
    }
}

From source file:b01.officeLink.FocWordDocument.java

License:Apache License

public void export(FocObject object) {
    TextPieceTable text = getTextTable();
    if (text != null) {

        List list = text.getTextPieces();

        for (int i = 0; i < list.size(); i++) {
            PropertyNode propertyNode = (PropertyNode) list.get(i);
            if (propertyNode instanceof TextPiece) {
                TextPiece textPiece = (TextPiece) propertyNode;
                StringBuffer buffer = textPiece.getStringBuffer();
                System.out.println("BEFORE REPLACE");
                System.out.println("Buffer = " + buffer);
                int startIndex = 0;
                int endIndex = 0;
                while (startIndex >= 0 && endIndex >= 0) {
                    startIndex = buffer.indexOf(OfficeLink.FORMULA_START_IDENTIFIER, endIndex);
                    if (startIndex > 0) {
                        endIndex = buffer.indexOf(OfficeLink.FORMULA_END_IDENTIFIER, startIndex);
                        String formulaString = buffer
                                .substring(startIndex + OfficeLink.FORMULA_START_IDENTIFIER.length(), endIndex);

                        /*Formula formula = new Formula(object.getThisFocDesc(), FField.NO_FIELD_ID, formulaString);
                        formula.setCurrentFocObject(object);
                        String formulaResult = (String) formula.compute();
                        replace(textPiece, startIndex, endIndex + OfficeLink.FORMULA_END_IDENTIFIER.length(), formulaResult);*/
                        Formula formula = new Formula(formulaString);
                        FieldFormulaContext context = new FieldFormulaContext(formula, null,
                                object.getThisFocDesc());
                        context.setCurrentFocObject(object);
                        String formulaResult = (String) context.evaluateFormula();
                        replace(textPiece, startIndex, endIndex + OfficeLink.FORMULA_END_IDENTIFIER.length(),
                                formulaResult);
                    }//w w  w  .j  a  v  a 2s.  c  om
                }
                System.out.println("AFTER REPLACE");
                System.out.println("Buffer = " + buffer);
            }
        }
    }
}

From source file:uk.bl.wa.tika.parser.ole2.OLE2Parser.java

License:Open Source License

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    HWPFDocument doc = new HWPFDocument(stream);
    System.out.println("ApplicationName: " + doc.getSummaryInformation().getApplicationName());
    System.out.println("OSVersion: " + doc.getSummaryInformation().getOSVersion());
    System.out.println("# paragraphs: " + doc.getDocumentSummaryInformation().getParCount());
    System.out.println("# bytes: " + doc.getDocumentSummaryInformation().getByteCount());
    System.out.println("# hidden: " + doc.getDocumentSummaryInformation().getHiddenCount());
    System.out.println("# lines: " + doc.getDocumentSummaryInformation().getLineCount());
    System.out.println("# mmclips: " + doc.getDocumentSummaryInformation().getMMClipCount());
    System.out.println("# notes: " + doc.getDocumentSummaryInformation().getNoteCount());
    System.out.println("# sections: " + doc.getDocumentSummaryInformation().getSectionCount());
    System.out.println("# slides: " + doc.getDocumentSummaryInformation().getSlideCount());
    System.out.println("format: " + doc.getDocumentSummaryInformation().getFormat());
    for (TextPiece tp : doc.getTextTable().getTextPieces()) {
        System.out.println("TP: " + tp.getStringBuffer().substring(0, 100));
        System.out.println("TP: " + tp.getPieceDescriptor().isUnicode());
    }/*from  w  w w.  ja  v  a  2 s  .  com*/
    for (Object os : doc.getDocumentSummaryInformation().getSections()) {
        Section s = (Section) os;
        System.out.println("ss# fid: " + s.getFormatID());
        System.out.println("ss# codepage: " + s.getCodepage());
        System.out.println("ss# # properties: " + s.getPropertyCount());
        for (Property sp : s.getProperties()) {
            System.out.println(
                    "ss# property: " + sp.getValue().getClass().getCanonicalName() + " " + sp.getValue());
        }
    }
    for (Ffn f : doc.getFontTable().getFontNames()) {
        System.out.println("Font: " + f.getMainFontName() + ", " + f.getSize() + ", " + f.getWeight());
    }
    parseCompObj(stream);

    // This
    POIFSFileSystem fs = new POIFSFileSystem(stream);

    DirectoryEntry root = fs.getRoot();

    dump(root);

}