Example usage for com.lowagie.text ElementTags URL

List of usage examples for com.lowagie.text ElementTags URL

Introduction

In this page you can find the example usage for com.lowagie.text ElementTags URL.

Prototype

String URL

To view the source code for com.lowagie.text ElementTags URL.

Click Source Link

Document

attribute of the image and annotation tag

Usage

From source file:org.apache.maven.doxia.module.itext.ITextSink.java

License:Apache License

/**
 * If the <code>name</code> is a relative link, the internal link will used a System property
 * <code>itext.basedir</code>, or the class loader.
 * {@inheritDoc}/* w w w. j  a va2  s. com*/
 */
public void figureGraphics(String name) {
    String urlName = null;
    File nameFile = null;
    if ((name.toLowerCase(Locale.ENGLISH).startsWith("http://"))
            || (name.toLowerCase(Locale.ENGLISH).startsWith("https://"))) {
        urlName = name;
    } else {
        if (System.getProperty("itext.basedir") != null) {
            try {
                nameFile = new File(System.getProperty("itext.basedir"), name);
                urlName = nameFile.toURI().toURL().toString();
            } catch (MalformedURLException e) {
                getLog().error("MalformedURLException: " + e.getMessage(), e);
            }
        } else {
            if (getClassLoader() != null) {
                if (getClassLoader().getResource(name) != null) {
                    urlName = getClassLoader().getResource(name).toString();
                }
            } else {
                if (ITextSink.class.getClassLoader().getResource(name) != null) {
                    urlName = ITextSink.class.getClassLoader().getResource(name).toString();
                }
            }
        }
    }

    if (urlName == null) {
        String msg = "No image '" + name
                + "' found in the class loader. Try to call setClassLoader(ClassLoader) before.";
        logMessage("imageNotFound", msg);

        return;
    }

    if (nameFile != null && !nameFile.exists()) {
        String msg = "No image '" + nameFile + "' found in your system, check the path.";
        logMessage("imageNotFound", msg);

        return;
    }

    boolean figureCalled = figureDefined;
    if (!figureCalled) {
        figure();
    }

    float width = 0;
    float height = 0;
    try {
        Image image = Image.getInstance(new URL(urlName));
        image.scaleToFit(ITextUtil.getDefaultPageSize().width() / 2,
                ITextUtil.getDefaultPageSize().height() / 2);
        width = image.plainWidth();
        height = image.plainHeight();
    } catch (BadElementException e) {
        getLog().error("BadElementException: " + e.getMessage(), e);
    } catch (MalformedURLException e) {
        getLog().error("MalformedURLException: " + e.getMessage(), e);
    } catch (IOException e) {
        getLog().error("IOException: " + e.getMessage(), e);
    }

    writeAddAttribute(ElementTags.URL, urlName);
    writeAddAttribute(ElementTags.ALIGN, ElementTags.ALIGN_MIDDLE);
    writeAddAttribute(ElementTags.PLAINWIDTH, String.valueOf(width));
    writeAddAttribute(ElementTags.PLAINHEIGHT, String.valueOf(height));

    actionContext.setAction(SinkActionContext.FIGURE_GRAPHICS);

    if (!figureCalled) {
        figure_();
    }
}