org.fit.cssbox.css.DOMAnalyzer Java Examples

The following examples show how to use org.fit.cssbox.css.DOMAnalyzer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImageRenderer.java    From WebVector with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Renders a document using the PDF engine.
 * @param docSource
 * @param da
 * @param pageFormat
 * @return
 */
protected PDFEngine renderPdf(DocumentSource docSource, DOMAnalyzer da, String pageFormat)
{
    PDFEngine engine = new PDFEngine(pageFormat, da.getRoot(), da, docSource.getURL());
    engine.setAutoMediaUpdate(false); //we have a correct media specification, do not update
    engine.getConfig().setClipViewport(cropWindow);
    engine.getConfig().setLoadImages(loadImages);
    engine.getConfig().setLoadBackgroundImages(loadBackgroundImages);
    defineLogicalFonts(engine.getConfig());
    engine.createLayout(windowSize);
    return engine;
}
 
Example #2
Source File: ImageRenderer.java    From WebVector with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Renders a document using the graphics engine. 
 * @param docSource
 * @param da
 * @param useFractionalMetrics
 * @param setDefaultFonts
 * @return
 */
protected GraphicsEngine renderGraphics(DocumentSource docSource, DOMAnalyzer da, boolean useFractionalMetrics, boolean setDefaultFonts)
{
    GraphicsEngine engine = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
    engine.setUseFractionalMetrics(useFractionalMetrics); //fractional metrics are useful for vector output 
    engine.setAutoMediaUpdate(false); //we have a correct media specification, do not update
    engine.getConfig().setClipViewport(cropWindow);
    engine.getConfig().setLoadImages(loadImages);
    engine.getConfig().setLoadBackgroundImages(loadBackgroundImages);
    if (setDefaultFonts)
        defineLogicalFonts(engine.getConfig());
    engine.createLayout(windowSize);
    return engine;
}
 
Example #3
Source File: StyleImport.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
public StyleImport(String urlstring) throws IOException, SAXException
{
    //Open the network connection 
    DocumentSource docSource = new DefaultDocumentSource(urlstring);
    
    //Parse the input document
    DOMSource parser = new DefaultDOMSource(docSource);
    doc = parser.parse();
    
    //Create the CSS analyzer
    DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
    da.getStyleSheets(); //load the author style sheets
    da.localizeStyles(); //put the style sheets into the document header
    
    docSource.close();
}
 
Example #4
Source File: SimpleBrowser.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** 
 * Creates a new application window and displays the rendered document
 * @param root The root DOM element of the document body
 * @param baseurl The base URL of the document used for completing the relative paths
 * @param decoder The CSS analyzer that provides the effective style of the elements 
 */
public SimpleBrowser(Element root, URL baseurl, DOMAnalyzer decoder)
{
    docroot = root;
    this.decoder = decoder;
    initComponents(baseurl);
}
 
Example #5
Source File: DefaultAnalyzer.java    From SwingBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Viewport analyze(DocumentSource docSource, Dimension dim)
        throws Exception
{
    DOMSource parser = new DefaultDOMSource(docSource);
    w3cdoc = parser.parse();

    // Create the CSS analyzer
    DOMAnalyzer da = new DOMAnalyzer(w3cdoc, docSource.getURL());
    da.attributesToStyles();
    da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
    da.getStyleSheets();
    
    BufferedImage tmpImg = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    canvas = new BrowserCanvas(da.getRoot(), da, docSource.getURL());
    canvas.setImage(tmpImg);
    canvas.getConfig().setLoadImages(true);
    canvas.getConfig().setLoadBackgroundImages(true);
    canvas.createLayout(dim);

    return canvas.getViewport();
}
 
Example #6
Source File: Engine.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
/** 
 * Creates a new instance of the browser engine for a document. After creating the engine,
 * the layout itself may be computed by calling {@link #createLayout(Dimension)}.
 * @param root the <body> element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param baseurl the document base URL   
 */
public Engine(org.w3c.dom.Element root, DOMAnalyzer decoder, URL baseurl)
{
    this.root = root;
    this.decoder = decoder;
    this.baseurl = baseurl;
    this.config = new BrowserConfig();
    this.autoSizeUpdate = true;
    this.autoMediaUpdate = true;
}
 
Example #7
Source File: ReferenceTestCase.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
private BufferedImage renderDocument(Document doc)
{
    //create the media specification
    MediaSpec media = new MediaSpec(mediaType);
    media.setDimensions(windowSize.width, windowSize.height);
    media.setDeviceDimensions(windowSize.width, windowSize.height);

    //Create the CSS analyzer
    DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
    da.setMediaSpec(media);
    da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
    da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
    da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
    da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
    da.getStyleSheets(); //load the author style sheets
    
    GraphicsEngine engine = new GraphicsEngine(da.getRoot(), da, docSource.getURL()) {
        @Override
        protected void setupGraphics(Graphics2D g)
        {
            // disable antialiasing for testing in order to compare the resulting images more precisely
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
            g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        }
    };
    engine.setUseKerning(false);
    engine.setAutoMediaUpdate(false); //we have a correct media specification, do not update
    engine.getConfig().setClipViewport(cropWindow);
    engine.getConfig().setLoadImages(loadImages);
    engine.getConfig().setLoadBackgroundImages(loadBackgroundImages);

    engine.createLayout(windowSize);
    return engine.getImage();
}
 
Example #8
Source File: BoxFactory.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create a new factory.
 * @param decoder The CSS decoder used for obtaining the DOM styles.
 * @param baseurl Base URL used for completing the relative URLs in the document.
 */
public BoxFactory(DOMAnalyzer decoder, URL baseurl)
{
    this.decoder = decoder;
    this.baseurl = baseurl;
    this.next_order = 0;
    this.overflowPropagated = false;
    this.config = new BrowserConfig();
    this.html = new HTMLBoxFactory(this);
}
 
Example #9
Source File: ReplacedText.java    From CSSBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void createDecoder()
{
    decoder = new DOMAnalyzer(doc, base);
    if (encoding == null)
        encoding = decoder.getCharacterEncoding();
    decoder.setDefaultEncoding(encoding);
    decoder.attributesToStyles();
    decoder.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);
    decoder.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);
    decoder.getStyleSheets();
}
 
Example #10
Source File: TextBoxes.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * main method
 */
public static void main(String[] args)
{
    if (args.length != 1)
    {
        System.err.println("Usage: TextBoxes <url>");
        System.exit(0);
    }
    
    try {
        //Open the network connection 
        DocumentSource docSource = new DefaultDocumentSource(args[0]);
        
        //Parse the input document
        DOMSource parser = new DefaultDOMSource(docSource);
        Document doc = parser.parse();
        
        //Create the CSS analyzer
        DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
        da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
        da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
        da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
        da.getStyleSheets(); //load the author style sheets
        
        //Create the browser canvas
        GraphicsEngine browser = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
        //Disable the image loading
        browser.getConfig().setLoadImages(false);
        browser.getConfig().setLoadBackgroundImages(false);
        
        //Create the layout for 1000x600 pixels
        browser.createLayout(new Dimension(1000, 600));
        
        //Display the result
        printTextBoxes(browser.getViewport());
        
        docSource.close();
        
    } catch (Exception e) {
        System.err.println("Error: "+e.getMessage());
        e.printStackTrace();
    }

}
 
Example #11
Source File: SimpleBrowser.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args the command line arguments
 */
public static void main(String args[]) 
{
	if (args.length != 1)
	{
		System.err.println("Usage: SimpleBrowser <url>");
		System.exit(0);
	}
	
    try {
        //Open the network connection 
        DocumentSource docSource = new DefaultDocumentSource(args[0]);
        
        //Parse the input document
        DOMSource parser = new DefaultDOMSource(docSource);
        Document doc = parser.parse();
        
        //Create the CSS analyzer
        DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
        da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
        da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
        da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
        da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
        da.getStyleSheets(); //load the author style sheets
        
        //Display the result
        SimpleBrowser test = new SimpleBrowser(da.getRoot(), docSource.getURL(), da);
        test.setSize(1275, 750);
        test.setVisible(true);
        
        docSource.close();
        
    } catch (Exception e) {
        System.out.println("Error: "+e.getMessage());
        e.printStackTrace();
    }
}
 
Example #12
Source File: Engine.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Gets the DOMAnalyzer used for decoding the CSS styles.
 * @return the used DOMAnalyzer
 */
public DOMAnalyzer getDecoder()
{
    return decoder;
}
 
Example #13
Source File: BoxFactory.java    From CSSBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Obtains the CSS analyzer and decoder used by the boxes.
 * @return A DOMAnalyzer
 */
public DOMAnalyzer getDecoder()
{
    return decoder;
}
 
Example #14
Source File: BrowserCanvas.java    From CSSBox with GNU Lesser General Public License v3.0 3 votes vote down vote up
/** 
 * Creates a browser canvas together with a new instance of the browser engine for a document
 * and creates the layout.
 * 
 * @param root the &lt;body&gt; element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param dim the preferred canvas dimensions
 * @param baseurl the document base URL   
 */
public BrowserCanvas(org.w3c.dom.Element root,
                     DOMAnalyzer decoder,
                     Dimension dim, URL baseurl)
{
    this(new GraphicsEngine(root, decoder, dim, baseurl));
}
 
Example #15
Source File: Engine.java    From CSSBox with GNU Lesser General Public License v3.0 3 votes vote down vote up
/** 
 * Creates a new instance of the browser engine for a document and creates the layout.
 * 
 * @param root the &lt;body&gt; element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param dim the preferred canvas dimensions
 * @param baseurl the document base URL   
 */
public Engine(org.w3c.dom.Element root,
                     DOMAnalyzer decoder,
                     Dimension dim, URL baseurl)
{
    this(root, decoder, baseurl);
    createLayout(dim);
}
 
Example #16
Source File: PreviewImageServiceImpl.java    From openemm with GNU Affero General Public License v3.0 3 votes vote down vote up
private BufferedImage renderDocumentWithCssBox(String url) throws IOException, SAXException {
	DocumentSource docSource = new DefaultDocumentSource(ensureUrlHasProtocol(url));
    
    final Dimension windowSize = new Dimension(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
    final String mediaType = "screen";

    // Parse the input document
    DOMSource parser = new DefaultDOMSource(docSource);
    Document doc = parser.parse();

    // Create the media specification
    MediaSpec media = new MediaSpec(mediaType);
    media.setDimensions(windowSize.width, windowSize.height);
    media.setDeviceDimensions(windowSize.width, windowSize.height);

    // Create the CSS analyzer
    DOMAnalyzer analyzer = new DOMAnalyzer(doc, docSource.getURL());

    analyzer.setMediaSpec(media);

    // Convert the HTML presentation attributes to inline styles
    analyzer.attributesToStyles();

    // Use the standard style sheet
    analyzer.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT);

    // Use the additional style sheet
    analyzer.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT);

    // Render form fields using css
    analyzer.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT);

    // Load the author style sheets
    analyzer.getStyleSheets();

    BrowserCanvas contentCanvas = new BrowserCanvas(analyzer.getRoot(), analyzer, docSource.getURL());
    // We have a correct media specification, do not update
    contentCanvas.setAutoMediaUpdate(false);
    contentCanvas.getConfig().setClipViewport(false);
    contentCanvas.getConfig().setLoadImages(true);
    contentCanvas.getConfig().setLoadBackgroundImages(true);
    contentCanvas.getConfig().setImageLoadTimeout((int) TimeUnit.SECONDS.toMillis(IMAGE_LOADING_TIMEOUT));

    contentCanvas.createLayout(windowSize);

    return contentCanvas.getImage();
}
 
Example #17
Source File: ComputeStyles.java    From CSSBox with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args)
{
    if (args.length != 2)
    {
        System.err.println("Usage: ComputeStyles <url> <output_file>");
        System.exit(0);
    }
    
    try {
        //Open the network connection 
        DocumentSource docSource = new DefaultDocumentSource(args[0]);
        
        //Parse the input document
        DOMSource parser = new DefaultDOMSource(docSource);
        Document doc = parser.parse();
        
        //Create the CSS analyzer
        DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
        da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
        da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
        da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
        da.getStyleSheets(); //load the author style sheets
        
        //Compute the styles
        System.err.println("Computing style...");
        da.stylesToDomInherited();
        
        //Save the output
        OutputStream os = new FileOutputStream(args[1]);
        Output out = new NormalOutput(doc);
        out.dumpTo(os);
        os.close();
        
        docSource.close();
        
        System.err.println("Done.");
        
    } catch (Exception e) {
        System.err.println("Error: "+e.getMessage());
        e.printStackTrace();
    }

}
 
Example #18
Source File: ImageRenderer.java    From CSSBox with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Renders the URL and prints the result to the specified output stream in the specified
 * format.
 * @param urlstring the source URL
 * @param out output stream
 * @return true in case of success, false otherwise
 * @throws SAXException 
 */
public boolean renderURL(String urlstring, OutputStream out) throws IOException, SAXException
{
    if (!urlstring.startsWith("http:") &&
        !urlstring.startsWith("https:") &&
        !urlstring.startsWith("ftp:") &&
        !urlstring.startsWith("file:"))
            urlstring = "http://" + urlstring;
    
    //Open the network connection 
    DocumentSource docSource = new DefaultDocumentSource(urlstring);
    
    //Parse the input document
    DOMSource parser = new DefaultDOMSource(docSource);
    Document doc = parser.parse();
    
    //create the media specification
    MediaSpec media = new MediaSpec(mediaType);
    media.setDimensions(windowSize.width, windowSize.height);
    media.setDeviceDimensions(windowSize.width, windowSize.height);

    //Create the CSS analyzer
    DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
    da.setMediaSpec(media);
    da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
    da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
    da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
    da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
    da.getStyleSheets(); //load the author style sheets
    
    GraphicsEngine contentCanvas = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
    contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update
    contentCanvas.getConfig().setClipViewport(cropWindow);
    contentCanvas.getConfig().setLoadImages(loadImages);
    contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);

    contentCanvas.createLayout(windowSize);
    ImageIO.write(contentCanvas.getImage(), "png", out);
    
    docSource.close();

    return true;
}
 
Example #19
Source File: BrowserCanvas.java    From CSSBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Creates a browser canvas together with a new instance of the browser engine for a document.
 * After creating the engine, the layout itself may be computed by 
 * calling {@link #createLayout(Dimension)}.
 * @param root the &lt;body&gt; element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param baseurl the document base URL   
 */
public BrowserCanvas(org.w3c.dom.Element root, DOMAnalyzer decoder, URL baseurl)
{
    this(new GraphicsEngine(root, decoder, baseurl));
}
 
Example #20
Source File: GraphicsEngine.java    From CSSBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/** 
 * Creates a new instance of the browser engine for a document and creates the layout.
 * 
 * @param root the &lt;body&gt; element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param dim the preferred canvas dimensions
 * @param baseurl the document base URL   
 */
public GraphicsEngine(Element root, DOMAnalyzer decoder, Dimension dim, URL baseurl)
{
    super(root, decoder, dim, baseurl);
    this.createImage = true;
}
 
Example #21
Source File: GraphicsEngine.java    From CSSBox with GNU Lesser General Public License v3.0 2 votes vote down vote up
/** 
 * Creates a new instance of the browser engine for a document. After creating the engine,
 * the layout itself may be computed by calling {@link #createLayout(Dimension)}.
 * @param root the &lt;body&gt; element of the document to be rendered
 * @param decoder the CSS decoder used to compute the style
 * @param baseurl the document base URL   
 */
public GraphicsEngine(Element root, DOMAnalyzer decoder, URL baseurl)
{
    super(root, decoder, baseurl);
    this.createImage = true;
}