List of usage examples for javax.swing.text DefaultStyledDocument DefaultStyledDocument
public DefaultStyledDocument()
From source file:com.stimulus.archiva.extraction.RTFExtractor.java
public Reader getText(InputStream is, Charset charset, IndexInfo indexInfo) throws ExtractionException { Reader reader = null;/*from ww w . j a va 2s . co 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:com.aurel.track.lucene.index.associatedFields.textExctractor.RTFExtractor.java
/** * Gets the text from file content /* www .j a va2s .c o 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:com.aurel.track.lucene.index.associatedFields.textExctractor.HTMLExtractor.java
/** * Gets the text from file content /*from w w w . j a va 2 s . c om*/ * @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:edu.ur.ir.index.DefaultRtfTextExtractor.java
/** * Extract text from the Rich text file document * @throws Exception //from www .ja v a 2s . c om * * @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:com.liferay.portal.util.LuceneFields.java
public static Field getFile(String field, File file, String fileExt) throws IOException { fileExt = fileExt.toLowerCase();//from ww w. j a v a 2s.co m 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.mirth.connect.server.util.FileUtil.java
public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText;/*from w w w. j a v a2s . c o m*/ // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }
From source file:com.mirth.connect.server.userutil.FileUtil.java
/** * Converts an RTF into plain text using the Swing RTFEditorKit. * //w ww.j ava2 s . c o m * @param message * The RTF message to convert. * @param replaceLinebreaksWith * If not null, any line breaks in the converted message will be replaced with this * string. * @return The converted plain text message. * @throws IOException * @throws BadLocationException */ public static String rtfToPlainText(String message, String replaceLinebreaksWith) throws IOException, BadLocationException { String convertedPlainText; // Reading the RTF content string Reader in = new StringReader(message); // creating a default blank styled document DefaultStyledDocument styledDoc = new DefaultStyledDocument(); // Creating a RTF Editor kit RTFEditorKit rtfKit = new RTFEditorKit(); // Populating the contents in the blank styled document rtfKit.read(in, styledDoc, 0); // Getting the root document Document doc = styledDoc.getDefaultRootElement().getDocument(); convertedPlainText = doc.getText(0, doc.getLength()); if (replaceLinebreaksWith != null) { convertedPlainText = convertedPlainText.replaceAll("\\n", replaceLinebreaksWith); } return convertedPlainText; }
From source file:fll.subjective.SubjectiveFrame.java
/** * Make sure the data in the table is valid. This checks to make sure that for * all rows, all columns that contain numeric data are actually set, or none * of these columns are set in a row. This avoids the case of partial data. * This method is fail fast in that it will display a dialog box on the first * error it finds./*from w ww. j ava2s . c om*/ * * @return true if everything is ok */ @SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC_ANON", justification = "Static inner class to replace anonomous listener isn't worth the confusion of finding the class definition") private boolean validateData() { stopCellEditors(); final List<String> warnings = new LinkedList<String>(); for (final ScoreCategory subjectiveCategory : getChallengeDescription().getSubjectiveCategories()) { final String category = subjectiveCategory.getName(); final String categoryTitle = subjectiveCategory.getTitle(); final List<AbstractGoal> goals = subjectiveCategory.getGoals(); final List<Element> scoreElements = SubjectiveTableModel.getScoreElements(_scoreDocument, category); for (final Element scoreElement : scoreElements) { int numValues = 0; for (final AbstractGoal goal : goals) { final String goalName = goal.getName(); final Element subEle = SubjectiveUtils.getSubscoreElement(scoreElement, goalName); if (null != subEle) { final String value = subEle.getAttribute("value"); if (!value.isEmpty()) { numValues++; } } } if (numValues != goals.size() && numValues != 0) { warnings.add(categoryTitle + ": " + scoreElement.getAttribute("teamNumber") + " has too few scores (needs all or none): " + numValues); } } } if (!warnings.isEmpty()) { // join the warnings with carriage returns and display them final StyledDocument doc = new DefaultStyledDocument(); for (final String warning : warnings) { try { doc.insertString(doc.getLength(), warning + "\n", null); } catch (final BadLocationException ble) { throw new RuntimeException(ble); } } final JDialog dialog = new JDialog(this, "Warnings"); final Container cpane = dialog.getContentPane(); cpane.setLayout(new BorderLayout()); final JButton okButton = new JButton("Ok"); cpane.add(okButton, BorderLayout.SOUTH); okButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent ae) { dialog.setVisible(false); dialog.dispose(); } }); cpane.add(new JTextPane(doc), BorderLayout.CENTER); dialog.pack(); dialog.setVisible(true); return false; } else { return true; } }
From source file:org.af.gMCP.gui.AboutDialog.java
private DefaultStyledDocument getDocument() { DefaultStyledDocument doc = new DefaultStyledDocument(); logger.info("Creating About-Text."); try {//from w w w .j a va 2 s .co 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; }
From source file:org.af.gMCP.gui.dialogs.TellAboutOnlineUpate.java
private DefaultStyledDocument getDocument() { DefaultStyledDocument doc = new DefaultStyledDocument(); logger.info("Creating About-Text."); try {//w w w . j a v a 2 s . c o m doc.insertString(doc.getLength(), "The gMCP-GUI would like to check online for updates on each start-up.\n", getH1()); doc.insertString(doc.getLength(), "No information about your computer is send.\n" + "Nevertheless you can disable this feature with the following checkbox\n" + "or later from the options dialog.", getT()); doc.setParagraphAttributes(0, doc.getLength(), getC(), true); } catch (BadLocationException ble) { logger.error("BadLocationException was thrown. Should never happen.", ble); } return doc; }