Example usage for javax.swing.text DefaultStyledDocument getLength

List of usage examples for javax.swing.text DefaultStyledDocument getLength

Introduction

In this page you can find the example usage for javax.swing.text DefaultStyledDocument getLength.

Prototype

public int getLength() 

Source Link

Document

Returns the length of the data.

Usage

From source file:com.liferay.portal.util.LuceneFields.java

public static Field getFile(String field, File file, String fileExt) throws IOException {

    fileExt = fileExt.toLowerCase();/*from   www.  j  av  a 2  s.c  om*/

    FileInputStream fis = new FileInputStream(file);
    Reader reader = new BufferedReader(new InputStreamReader(fis));

    String text = null;

    if (fileExt.equals(".doc")) {
        try {
            WordDocument wordDocument = new WordDocument(fis);

            StringWriter stringWriter = new StringWriter();

            wordDocument.writeAllText(stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".htm") || fileExt.equals(".html")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
            htmlEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".pdf")) {
        try {
            PDFParser parser = new PDFParser(fis);
            parser.parse();

            PDDocument pdDoc = parser.getPDDocument();

            StringWriter stringWriter = new StringWriter();

            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setLineSeparator("\n");
            stripper.writeText(pdDoc, stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
            pdDoc.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".rtf")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            RTFEditorKit rtfEditorKit = new RTFEditorKit();
            rtfEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".xls")) {
        try {
            XLSTextStripper stripper = new XLSTextStripper(fis);

            text = stripper.getText();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    }

    if (text != null) {
        return new Field(field, text, Field.Store.YES, Field.Index.NOT_ANALYZED);
    } else {
        return new Field(field, reader);
    }
}

From source file:com.stimulus.archiva.extraction.RTFExtractor.java

public Reader getText(InputStream is, Charset charset, IndexInfo indexInfo) throws ExtractionException {

    Reader reader = null;// w  w  w. j  a v a2  s. c  o m
    FileWriter writer = null;
    File file = null;
    try {
        reader = new InputStreamReader(is);
        file = File.createTempFile("extract_rtf", ".tmp");
        indexInfo.addDeleteFile(file);
        writer = new FileWriter(file);
        DefaultStyledDocument doc = new DefaultStyledDocument();
        new RTFEditorKit().read(reader, doc, 0);
        writer.write(doc.getText(0, doc.getLength()));
    } catch (Throwable ioe) {
        throw new ExtractionException("failed to parse rtf document", ioe, logger);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
            }
        }

        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ioe) {
            }
        }
    }
    try {
        Reader outReader = new FileReader(file);
        indexInfo.addReader(outReader);
        return outReader;
    } catch (Exception ex) {
        throw new ExtractionException("failed to extract text from powerpoint document", ex, logger,
                ChainedException.Level.DEBUG);
    }

}

From source file:framework.retrieval.engine.index.create.impl.file.parse.RTFFileContentParser.java

public String getContent(RFileDocument document, String charsetName) {
    String content = "";
    InputStream fileInputStream = null;
    try {//from   w  w w  .  j a v a2  s .  c o m
        fileInputStream = new FileInputStream(document.getFile());
        DefaultStyledDocument styledDoc = new DefaultStyledDocument();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();
        rtfEditorKit.read(fileInputStream, styledDoc, 0);
        content = styledDoc.getText(0, styledDoc.getLength());
    } catch (Exception e) {
        RetrievalUtil.errorLog(log, document.getFile().getAbsolutePath(), e);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (Exception e) {
            RetrievalUtil.errorLog(log, e);
        }
    }
    return content;
}

From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.HTMLExtractor.java

/**
 * Gets the text from file content /* w w  w  .  j a  v a2 s.  co m*/
 * @param file
 * @param fileExtension
 * @return
 */
@Override
public String getText(File file, String fileExtension) {
    FileInputStream fis = null;
    Reader reader = null;
    try {
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            LOGGER.info("File " + file.getName() + " not found. " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
            return null;
        }
        reader = new BufferedReader(new InputStreamReader(fis));
        DefaultStyledDocument dsd = new DefaultStyledDocument();
        HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
        htmlEditorKit.read(reader, dsd, 0);
        return dsd.getText(0, dsd.getLength());
    } catch (Exception e) {
        LOGGER.debug("Extracting text from the .htm or .html  file " + file.getName() + " failed with "
                + e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            LOGGER.debug("Closing the reader for file " + file.getName() + " failed with " + e.getMessage());
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
            LOGGER.debug("Closing the FileInputStream for file " + file.getName() + " failed with "
                    + e.getMessage());
        }
    }
    return null;
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

protected void colorStyledDocument(final DefaultStyledDocument document) {
    EventQueue.invokeLater(new Runnable() {

        @Override//w  w  w. j  a v  a 2 s  .c  om
        public void run() {
            String input = "";
            try {
                input = document.getText(0, document.getLength());
            } catch (BadLocationException ex) {
                Logger.getLogger(SimpleOrangeTextPane.class.getName()).log(Level.SEVERE, null, ex);
            }

            StringBuilder inputMut = new StringBuilder(input);
            String[] split = StringUtils.split(inputMut.toString());
            int i = 0;
            for (String string : split) {
                int start = inputMut.indexOf(string);
                int end = start + string.length();
                inputMut.replace(start, end, StringUtils.repeat(" ", string.length()));
                document.setCharacterAttributes(start, string.length(), styles[i++ % styles.length], true);
            }
        }
    });
}

From source file:edu.ur.ir.index.DefaultRtfTextExtractor.java

/**
 * Extract text from the Rich text file document 
 * @throws Exception //from w  ww. j a va2s  .co  m
 * 
 * @see edu.ur.ir.index.FileTextExtractor#getText(java.io.File)
 */
public String getText(File f) throws Exception {
    String text = null;
    // don't even try if the file is too large
    if (isFileTooLarge(f) || f.length() <= 0l) {
        return text;
    }
    DefaultStyledDocument styledDoc = new DefaultStyledDocument();
    RTFEditorKit editorKit = new RTFEditorKit();
    FileInputStream inputStream = null;

    try {
        inputStream = new FileInputStream(f);
        editorKit.read(inputStream, styledDoc, 0);
        String myText = styledDoc.getText(0, styledDoc.getLength());
        if (myText != null && !myText.trim().equals("")) {
            text = myText;
        }
    } catch (OutOfMemoryError oome) {
        text = null;
        log.error("could not extract text", oome);
        throw (oome);
    } catch (Exception e) {
        text = null;
        log.error("could not get text for rich text document " + f.getAbsolutePath(), e);
        throw (e);
    }

    finally {
        closeInputStream(inputStream);
        editorKit = null;
    }

    return text;
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

public void addField() {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) availableFieldsComp.getTree()
            .getLastSelectedPathComponent();
    if (node == null || !node.isLeaf() || !(node.getUserObject() instanceof DataObjDataFieldWrapper)) {
        return; // not really a field that can be added, just empty or a string
    }/*w ww.  j  a  va 2  s.c  o m*/

    Object obj = node.getUserObject();
    if (obj instanceof DataObjDataFieldWrapper) {
        DataObjDataFieldWrapper wrapper = (DataObjDataFieldWrapper) obj;
        String sep = sepText.getText();
        if (StringUtils.isNotEmpty(sep)) {
            try {
                DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
                if (doc.getLength() > 0) {
                    doc.insertString(doc.getLength(), sep, null);
                }
            } catch (BadLocationException ble) {
            }

        }
        insertFieldIntoTextEditor(wrapper);
        setHasChanged(true);
    }
}

From source file:com.aurel.track.lucene.index.associatedFields.textExctractor.RTFExtractor.java

/**
 * Gets the text from file content //from ww  w. j  av a 2 s  .co  m
 * @param file
 * @param fileExtension
 * @return
 */
@Override
public String getText(File file, String fileExtension) {
    FileInputStream fis = null;
    Reader reader = null;
    try {
        DefaultStyledDocument dsd = new DefaultStyledDocument();
        RTFEditorKit rtfEditorKit = new RTFEditorKit();

        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            LOGGER.info("File " + file.getName() + " not found. " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
            return null;
        }
        reader = new BufferedReader(new InputStreamReader(fis));
        rtfEditorKit.read(reader, dsd, 0);
        return dsd.getText(0, dsd.getLength());
    } catch (Exception e) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                    "Extracting text from the .rtf  file " + file.getName() + " failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                LOGGER.debug(
                        "Closing the reader for file " + file.getName() + "  failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                LOGGER.debug("Closing the FileInputStream for file " + file.getName() + " failed with "
                        + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java

/**
 * @param formatter/*  ww  w  . j a va  2 s.  c  om*/
 */
protected void setFormatterFromTextPane(final DataObjSwitchFormatter formatter) {
    // visit every character in the document text looking for fields
    // store characters not associated with components (jbutton) to make up the separator text
    DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument();
    String text = formatEditor.getText();
    int docLen = doc.getLength();
    int lastFieldPos = 0;

    Vector<DataObjDataField> fields = new Vector<DataObjDataField>();
    //int cnt = 0;
    for (int i = 0; i < docLen; ++i) {
        Element element = doc.getCharacterElement(i);
        AttributeSet attrs = element.getAttributes();
        Object obj = attrs.getAttribute(StyleConstants.ComponentAttribute);
        //System.out.print(String.format("i: %d, lastFieldPos: %d cnt: %d, F: %s", i, lastFieldPos, cnt, (obj instanceof FieldDefinitionComp ? "Y" : "N")));
        if (obj instanceof FieldDefinitionComp) {
            //System.out.println(cnt+"  "+(obj instanceof FieldDefinitionComp));
            // found button at the current position
            // create corresponding field
            String sepStr = (lastFieldPos <= i - 1) ? text.substring(lastFieldPos, i) : "";

            FieldDefinitionComp fieldDefBtn = (FieldDefinitionComp) obj;
            DataObjDataField fmtField = fieldDefBtn.getValue();
            fmtField.setSep(sepStr);
            fields.add(fmtField);

            //System.out.print(" Sep: ["+sepStr+"]");

            lastFieldPos = i + 1;
            //cnt++;
        }
    }

    // XXX: what do we do with the remaining of the text? right now we ignore it
    // That's because we can't create an empty formatter field just to use the separator... 

    DataObjDataField[] fieldsArray = new DataObjDataField[fields.size()];
    for (int i = 0; i < fields.size(); ++i) {
        fieldsArray[i] = fields.elementAt(i);
    }

    DataObjDataFieldFormat singleFormatter = fieldsArray.length == 0 ? null
            : new DataObjDataFieldFormat("", tableInfo.getClassObj(), false, "", "", fieldsArray);
    formatter.setSingle(singleFormatter);
}

From source file:org.af.gMCP.gui.AboutDialog.java

private DefaultStyledDocument getDocument() {
    DefaultStyledDocument doc = new DefaultStyledDocument();
    logger.info("Creating About-Text.");
    try {//from   w  ww .  j a  v  a  2 s. c o m
        doc.insertString(doc.getLength(),
                "gMCP " + Configuration.getInstance().getGeneralConfig().getVersionNumber() + "\n\n", getH1());
        doc.insertString(doc.getLength(),
                "by Kornelius Rohmeyer and Florian Klinglmueller is distributed under GPL 2.0." + "\n\n",
                getT());
        doc.insertString(doc.getLength(),
                "This program uses the libraries log4j, JLaTeXMath, POI, iText (2.1.4), jxlayer,\n swingworker, commons logging/lang, JRI and JGoodies Forms.\n",
                getT());
        doc.insertString(doc.getLength(),
                "\n" + "This program is free software; you can redistribute it and/or\n"
                        + "modify it under the terms of the GNU General Public License\n"
                        + "as published by the Free Software Foundation; either version 2\n"
                        + "of the License, or (at your option) any later version.\n" + "\n"
                        + "This program is distributed in the hope that it will be useful,\n"
                        + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
                        + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
                        + "GNU General Public License for more details. It is included\n"
                        + "in the R distribution (in directory share/licenses) or can be\n"
                        + "found at: http://www.gnu.org/licenses/\n",
                getT());
        doc.setParagraphAttributes(0, doc.getLength(), getC(), true);
    } catch (BadLocationException ble) {
        logger.error("BadLocationException was thrown. Should never happen.", ble);
    }
    return doc;
}