Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: XMLWorkerHelper.java 359 2012-08-06 09:59:51Z eugenemark $
   *
   * This file is part of the iText (R) project.
   * Copyright (c) 1998-2012 1T3XT
   * BVBA Authors: Balder Van Camp, Emiel Ackermann, et al.
   *
   * This program is free software; you can redistribute it and/or modify it under
   * the terms of the GNU Affero General Public License version 3 as published by
  * the Free Software Foundation with the addition of the following permission
  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
  * WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 1T3XT DISCLAIMS THE WARRANTY
  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  *
  * 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 Affero General Public License for more
  * details. You should have received a copy of the GNU Affero General Public
  * License along with this program; if not, see http://www.gnu.org/licenses or
  * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  * http://itextpdf.com/terms-of-use/
  *
  * The interactive user interfaces in modified source and object code versions
  * of this program must display Appropriate Legal Notices, as required under
  * Section 5 of the GNU Affero General Public License.
  *
  * In accordance with Section 7(b) of the GNU Affero General Public License, a
  * covered work must retain the producer line in every PDF that is created or
  * manipulated using iText.
  *
  * You can be released from the requirements of the license by purchasing a
  * commercial license. Buying such a license is mandatory as soon as you develop
  * commercial activities involving the iText software without disclosing the
  * source code of your own applications. These activities include: offering paid
  * services to customers as an ASP, serving PDFs on the fly in a web
  * application, shipping iText with a closed source product.
  *
  * For more information, please contact iText Software Corp. at this address:
  * sales@itextpdf.com
  */
 package com.itextpdf.tool.xml;
 
 
 import java.io.Reader;
A helper class for parsing XHTML/CSS or XML flow to PDF.

Author(s):
redlab_b
 
 public class XMLWorkerHelper {
 
 	private static XMLWorkerHelper myself = new XMLWorkerHelper();

Get a Singleton XMLWorkerHelper

Returns:
a singleton instance of XMLWorkerHelper
 
 	public synchronized static XMLWorkerHelper getInstance() {
 		return ;
 
 	}
 
 	private CssFile defaultCssFile;

 
 	private XMLWorkerHelper() {
 
 	}

Returns:
the default css file.
 
 	public static synchronized CssFile getCSS(InputStream in) {
        CssFile cssFile = null;
        if (null != in) {
            final CssFileProcessor cssFileProcessor = new CssFileProcessor();
            int i = -1;
            try {
                while (-1 != (i = in.read())) {
                    cssFileProcessor.process((chari);
                }
                cssFile = new CSSFileWrapper(cssFileProcessor.getCss(), true);
            } catch (final IOException e) {
                throw new RuntimeWorkerException(e);
            } finally {
                try {
                    in.close();
                } catch (final IOException e) {
                    throw new RuntimeWorkerException(e);
                }
            }
        }
		return cssFile;
	}
    public synchronized CssFile getDefaultCSS() {
        if (null == ) {
             = getCSS(XMLWorkerHelper.class.getResourceAsStream("/default.css"));
        }
        return ;
    }

Parses the xml data in the given reader and sends created com.itextpdf.text.Element s to the defined ElementHandler.
This method configures the XMLWorker and XMLParser to parse (X)HTML/CSS and accept unknown tags.

Parameters:
d the handler
in the reader
Throws:
java.io.IOException thrown when something went wrong with the IO
	public void parseXHtml(final ElementHandler dfinal Reader inthrows IOException {
		CssFilesImpl cssFiles = new CssFilesImpl();
		cssFiles.add(getDefaultCSS());
		StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
		Pipeline<?> pipeline = new CssResolverPipeline(cssResolvernew HtmlPipeline(hpcnew ElementHandlerPipeline(d,
				null)));
		XMLWorker worker = new XMLWorker(pipelinetrue);
		XMLParser p = new XMLParser();
		p.addListener(worker);
		p.parse(in);
	}

Parses the xml data. This method configures the XMLWorker to parse (X)HTML/CSS and accept unknown tags. Writes the output in the given PdfWriter with the given document.

Parameters:
writer the PdfWriter
doc the Document
in the reader
Throws:
java.io.IOException thrown when something went wrong with the IO
	public void parseXHtml(final PdfWriter writerfinal Document docfinal Reader inthrows IOException {
		CssFilesImpl cssFiles = new CssFilesImpl();
		cssFiles.add(getDefaultCSS());
		StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
		Pipeline<?> pipeline = new CssResolverPipeline(cssResolvernew HtmlPipeline(hpcnew PdfWriterPipeline(doc,
				writer)));
		XMLWorker worker = new XMLWorker(pipelinetrue);
		XMLParser p = new XMLParser();
		p.addListener(worker);
		p.parse(in);
	}

Parameters:
writer the writer to use
doc the document to use
in the java.io.InputStream of the XHTML source.
Throws:
java.io.IOException if the java.io.InputStream could not be read.
	public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream inthrows IOException {
		parseXHtml(writer,doc,inXMLWorkerHelper.class.getResourceAsStream("/default.css"), nullnew XMLWorkerFontProvider());
	}

Parameters:
writer the writer to use
doc the document to use
in the java.io.InputStream of the XHTML source.
charset the charset to use
Throws:
java.io.IOException if the java.io.InputStream could not be read.
	public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream infinal Charset charsetthrows IOException {
		parseXHtml(writer,doc,inXMLWorkerHelper.class.getResourceAsStream("/default.css"), charset);
	}

    

Parameters:
writer the writer to use
doc the document to use
in the java.io.InputStream of the XHTML source.
in the com.itextpdf.tool.xml.css.CssFiles of the css files.
charset the charset to use
Throws:
java.io.IOException if the java.io.InputStream could not be read.
	public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream infinal InputStream inCssFilefinal Charset charsetfinal FontProvider fontProviderthrows IOException {
        CssFilesImpl cssFiles = new CssFilesImpl();
        if (inCssFile != null)
            cssFiles.add(getCSS(inCssFile));
        else
            cssFiles.add(getDefaultCSS());
        StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
        HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
        HtmlPipeline htmlPipeline = new HtmlPipeline(hpcnew PdfWriterPipeline(docwriter));
        Pipeline<?> pipeline = new CssResolverPipeline(cssResolverhtmlPipeline);
        XMLWorker worker = new XMLWorker(pipelinetrue);
        XMLParser p = new XMLParser(trueworkercharset);
		if (charset != null)
			p.parse(incharset);
		else
			p.parse(in);
	}
    public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream infinal InputStream inCssFilethrows IOException {
        parseXHtml(writerdocininCssFilenullnew XMLWorkerFontProvider());
	}
    public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream infinal InputStream inCssFilefinal FontProvider fontProviderthrows IOException {
        parseXHtml(writerdocininCssFilenullfontProvider);
	}
    public void parseXHtml(final PdfWriter writerfinal Document docfinal InputStream infinal InputStream inCssFilefinal Charset charsetthrows IOException {
        parseXHtml(writerdocininCssFilecharsetnew XMLWorkerFontProvider());
	}

Parameters:
d the ElementHandler
in the InputStream
charset the charset to use
Throws:
java.io.IOException if something went seriously wrong with IO.
	public void parseXHtml(final ElementHandler dfinal InputStream infinal Charset charsetthrows IOException {
		CssFilesImpl cssFiles = new CssFilesImpl();
		cssFiles.add(getDefaultCSS());
		StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
		Pipeline<?> pipeline = new CssResolverPipeline(cssResolvernew HtmlPipeline(hpcnew ElementHandlerPipeline(d,
				null)));
		XMLWorker worker = new XMLWorker(pipelinetrue);
		XMLParser p = new XMLParser(trueworkercharset);
		if (charset != null)
			p.parse(incharset);
		else
			p.parse(in);
	}

Get a CSSResolver implementation.

Parameters:
addDefaultCss true if the defaultCss should already be added.
Returns:
the default CSSResolver
	public CSSResolver getDefaultCssResolver(final boolean addDefaultCss) {
		CSSResolver resolver = new StyleAttrCSSResolver();
		if (addDefaultCss) {
			resolver.addCss(getDefaultCSS());
		}
		return resolver;
	}

Retrieves the default factory for processing HTML tags from com.itextpdf.tool.xml.html.Tags.getHtmlTagProcessorFactory(). On subsequent calls the same com.itextpdf.tool.xml.html.TagProcessorFactory is returned every time.

Returns:
a DefaultTagProcessorFactory that maps HTML tags to com.itextpdf.tool.xml.html.TagProcessors
		if (null == ) {
		}
		return ;
	}
New to GrepCode? Check out our FAQ X