Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * PlainText.java * Copyright (C) 2010-2016 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer.pdfproclet; import adams.core.Utils; import adams.core.base.BaseString; import adams.core.io.FileUtils; import adams.core.io.PdfFont; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.ColumnText; import java.awt.Color; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** <!-- globalinfo-start --> * Adds plain-text files line by line. * <br><br> <!-- globalinfo-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-regexp-filename <adams.core.base.BaseRegExp> (property: regExpFilename) * The regular expression that the filename must match. * default: .* * </pre> * * <pre>-add-filename <boolean> (property: addFilename) * Whether to add the file name before the actual file content as separate * paragraph. * default: false * </pre> * * <pre>-font-filename <adams.core.io.PdfFont> (property: fontFilename) * The font to use for printing the file name header. * default: Helvetica-Bold-12 * </pre> * * <pre>-color-filename <java.awt.Color> (property: colorFilename) * The color to use for printing the file name header. * default: #000000 * </pre> * * <pre>-page-break-before <boolean> (property: pageBreakBefore) * If true, then a page-break is added before the content of the file is inserted. * default: false * </pre> * * <pre>-page-break-after <boolean> (property: pageBreakAfter) * If true, then a page-break is added after the content of the file is inserted. * default: false * </pre> * * <pre>-num-files <int> (property: numFilesPerPage) * The number of files to put on a page before adding an automatic page break; * use -1 for unlimited. * default: -1 * minimum: -1 * </pre> * * <pre>-font-content <adams.core.io.PdfFont> (property: fontContent) * The font to use for the file content. * default: Helvetica-Normal-12 * </pre> * * <pre>-color-content <java.awt.Color> (property: colorContent) * The color to use for the content. * default: #000000 * </pre> * * <pre>-extension <adams.core.base.BaseString> [-extension ...] (property: extensions) * The file extension(s) that the processor will be used for. * default: txt * </pre> * * <pre>-use-absolute-position <boolean> (property: useAbsolutePosition) * If enabled, the absolute position is used (from bottom-left corner). * default: false * </pre> * * <pre>-x <float> (property: X) * The absolute X position. * default: 0.0 * minimum: 0.0 * </pre> * * <pre>-y <float> (property: Y) * The absolute Y position. * default: 0.0 * minimum: 0.0 * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class PlainText extends AbstractPdfProcletWithPageBreaks implements PdfProcletWithVariableFileExtension, PdfProcletWithOptionalAbsolutePosition { /** for serialization. */ private static final long serialVersionUID = 3962046484864891107L; /** the font for the content. */ protected PdfFont m_FontContent; /** the color for the content. */ protected Color m_ColorContent; /** the file extensions. */ protected BaseString[] m_Extensions; /** whether to use absolute position. */ protected boolean m_UseAbsolutePosition; /** the absolute X position. */ protected float m_X; /** the absolute Y position. */ protected float m_Y; /** * Returns a short description of the writer. * * @return a description of the writer */ @Override public String globalInfo() { return "Adds plain-text files line by line."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("font-content", "fontContent", new PdfFont(PdfFont.HELVETICA, PdfFont.NORMAL, 12.0f)); m_OptionManager.add("color-content", "colorContent", Color.BLACK); m_OptionManager.add("extension", "extensions", new BaseString[] { new BaseString("txt") }); m_OptionManager.add("use-absolute-position", "useAbsolutePosition", false); m_OptionManager.add("x", "X", 0.0f, 0.0f, null); m_OptionManager.add("y", "Y", 0.0f, 0.0f, null); } /** * Sets the font to use for adding the content. * * @param value the font */ public void setFontContent(PdfFont value) { m_FontContent = value; reset(); } /** * Returns the font to use for adding the content. * * @return the font */ public PdfFont getFontContent() { return m_FontContent; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String fontContentTipText() { return "The font to use for the file content."; } /** * Sets the color to use for the content. * * @param value the color */ public void setColorContent(Color value) { m_ColorContent = value; reset(); } /** * Returns the color to use for the content. * * @return the color */ public Color getColorContent() { return m_ColorContent; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String colorContentTipText() { return "The color to use for the content."; } /** * Returns the extensions that the processor can process. * * @return the extensions (no dot) */ @Override public BaseString[] getExtensions() { return m_Extensions; } /** * Sets the extensions that the processor can process. * * @param value the extensions (no dot) */ @Override public void setExtensions(BaseString[] value) { m_Extensions = value; reset(); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ @Override public String extensionsTipText() { return "The file extension(s) that the processor will be used for."; } /** * Sets whether to use absolute positioning (from bottom-left corner). * * @param value true if absolute */ public void setUseAbsolutePosition(boolean value) { m_UseAbsolutePosition = value; reset(); } /** * Returns whether absolute positioning is used (from bottom-left corner). * * @return true if absolute */ public boolean getUseAbsolutePosition() { return m_UseAbsolutePosition; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String useAbsolutePositionTipText() { return "If enabled, the absolute position is used (from bottom-left corner)."; } /** * Sets the absolute X position. * * @param value the X position */ public void setX(float value) { if (getOptionManager().isValid("X", value)) { m_X = value; reset(); } } /** * Returns the absolute X position. * * @return the X position */ public float getX() { return m_X; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String XTipText() { return "The absolute X position."; } /** * Sets the absolute Y position. * * @param value the Y position */ public void setY(float value) { if (getOptionManager().isValid("Y", value)) { m_Y = value; reset(); } } /** * Returns the absolute Y position. * * @return the Y position */ public float getY() { return m_Y; } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ public String YTipText() { return "The absolute Y position."; } /** * The actual processing of the document. * * @param generator the context * @param paragraphs the paragraphs to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, List<String> paragraphs) throws Exception { boolean result; ColumnText ct; result = true; if (m_UseAbsolutePosition) { ct = addColumnTextAt(generator, m_X, m_Y); ct.addElement(new Paragraph(Utils.flatten(paragraphs, "\n"), m_FontContent.toFont(m_ColorContent))); ct.go(); generator.getState().contentAdded(); } else { result = addElement(generator, new Paragraph(Utils.flatten(paragraphs, "\n"), m_FontContent.toFont(m_ColorContent))); } return result; } /** * The actual processing of the document. * * @param generator the context * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ @Override protected boolean doProcess(PDFGenerator generator, File file) throws Exception { boolean result; List<String> paragraphs; result = addFilename(generator, file); if (result) { paragraphs = FileUtils.loadFromFile(file); result = doProcess(generator, paragraphs); } return result; } /** * Whether the processor can handle this particular object. * * @param generator the context * @param obj the object to check * @return true if the object can be handled */ public boolean canProcess(PDFGenerator generator, Object obj) { return true; } /** * The actual processing of the document. * * @param generator the context * @param obj the object to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, Object obj) throws Exception { List<String> paragraphs; paragraphs = new ArrayList<>(Arrays.asList(obj.toString().split("\n"))); return doProcess(generator, paragraphs); } }