svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / image / ImageFromHTMLFunction.java @ 46041
History | View | Annotate | Download (5.19 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.image; |
---|---|
2 |
|
3 |
import cz.vutbr.web.css.MediaSpec; |
4 |
import java.awt.image.BufferedImage; |
5 |
import java.io.ByteArrayInputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.net.URL; |
9 |
import org.apache.commons.lang3.Range; |
10 |
import org.fit.cssbox.css.CSSNorm; |
11 |
import org.fit.cssbox.css.DOMAnalyzer; |
12 |
import org.fit.cssbox.io.DOMSource; |
13 |
import org.fit.cssbox.io.DefaultDOMSource; |
14 |
import org.fit.cssbox.io.DefaultDocumentSource; |
15 |
import org.fit.cssbox.io.DocumentSource; |
16 |
import org.fit.cssbox.awt.GraphicsEngine; |
17 |
import org.fit.cssbox.layout.Dimension; |
18 |
import org.gvsig.expressionevaluator.Interpreter; |
19 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
20 |
import org.gvsig.tools.swing.api.SimpleImage; |
21 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
22 |
import org.slf4j.Logger; |
23 |
import org.slf4j.LoggerFactory; |
24 |
import org.w3c.dom.Document; |
25 |
import org.xml.sax.SAXException; |
26 |
|
27 |
@SuppressWarnings("UseSpecificCatch") |
28 |
public class ImageFromHTMLFunction extends AbstractFunction { |
29 |
|
30 |
private static final Logger LOGGER = LoggerFactory.getLogger(ImageFromHTMLFunction.class); |
31 |
|
32 |
public ImageFromHTMLFunction() {
|
33 |
super(
|
34 |
"Image", "IMAGEFROMHTML", Range.is(3), |
35 |
"",
|
36 |
"IMAGEFROMHTML({{with}}, height, source)",
|
37 |
new String[]{ |
38 |
"width - with of the image in pixels",
|
39 |
"height - height of the image in pixels",
|
40 |
"source - HTML source or URL"
|
41 |
}, |
42 |
"SimpleImage",
|
43 |
false
|
44 |
); |
45 |
} |
46 |
|
47 |
@Override
|
48 |
public boolean allowConstantFolding() { |
49 |
return true; |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public Object call(Interpreter interpreter, Object[] args) { |
54 |
int width = this.getInt(args, 0); |
55 |
int height = this.getInt(args, 1); |
56 |
String source = this.getStr(args, 2); |
57 |
URL url = null; |
58 |
try {
|
59 |
url = new URL(source); |
60 |
} catch (Exception ex) { |
61 |
LOGGER.debug("Can't parse url", ex);
|
62 |
} |
63 |
BufferedImage img = createImageFromHTML(width, height, url, source);
|
64 |
if( img == null ) { |
65 |
return null; |
66 |
} |
67 |
SimpleImage simpleimg = ToolsSwingLocator.getToolsSwingManager().createSimpleImage(img); |
68 |
return simpleimg;
|
69 |
} |
70 |
|
71 |
public static BufferedImage createImageFromHTML(int width, int height, URL url, String source) { |
72 |
try {
|
73 |
String mediaType = "screen"; |
74 |
Dimension windowSize = new Dimension(width, height); |
75 |
boolean cropWindow = false; |
76 |
boolean loadImages = true; |
77 |
boolean loadBackgroundImages = true; |
78 |
DocumentSource docSource; |
79 |
if (url != null) { |
80 |
docSource = new DefaultDocumentSource(url.toString());
|
81 |
} else {
|
82 |
InputStream in = new ByteArrayInputStream(source.getBytes()); |
83 |
docSource = new DocumentSource(url) {
|
84 |
@Override
|
85 |
public URL getURL() { |
86 |
return null; |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public String getContentType() { |
91 |
return "text/html"; |
92 |
} |
93 |
|
94 |
@Override
|
95 |
public InputStream getInputStream() throws IOException { |
96 |
return in;
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public void close() throws IOException { |
101 |
in.close(); |
102 |
} |
103 |
}; |
104 |
} |
105 |
|
106 |
//Parse the input document
|
107 |
DOMSource parser = new DefaultDOMSource(docSource); |
108 |
Document doc = parser.parse();
|
109 |
|
110 |
//create the media specification
|
111 |
MediaSpec media = new MediaSpec(mediaType);
|
112 |
media.setDimensions(windowSize.width, windowSize.height); |
113 |
media.setDeviceDimensions(windowSize.width, windowSize.height); |
114 |
|
115 |
//Create the CSS analyzer
|
116 |
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
|
117 |
da.setMediaSpec(media); |
118 |
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
|
119 |
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet |
120 |
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet |
121 |
da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css |
122 |
da.getStyleSheets(); //load the author style sheets
|
123 |
|
124 |
GraphicsEngine contentCanvas = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
|
125 |
contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update |
126 |
contentCanvas.getConfig().setClipViewport(cropWindow); |
127 |
contentCanvas.getConfig().setLoadImages(loadImages); |
128 |
contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages); |
129 |
|
130 |
contentCanvas.createLayout(windowSize); |
131 |
return contentCanvas.getImage();
|
132 |
} catch (IOException | SAXException ex) { |
133 |
return null; |
134 |
} |
135 |
} |
136 |
} |