Revision 46041
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/symboltable/ProgrammingSymbolTable.java | ||
---|---|---|
12 | 12 |
import org.gvsig.expressionevaluator.impl.function.programming.CallStaticMethodFunction; |
13 | 13 |
import org.gvsig.expressionevaluator.impl.function.programming.CodeBlockWithExceptFunction; |
14 | 14 |
import org.gvsig.expressionevaluator.impl.function.programming.CreateFnFunction; |
15 |
import org.gvsig.expressionevaluator.impl.function.programming.CreateHtmlPanelFunction; |
|
15 | 16 |
import org.gvsig.expressionevaluator.impl.function.programming.EvalFunction; |
16 | 17 |
import org.gvsig.expressionevaluator.impl.function.programming.FileFunction; |
17 | 18 |
import org.gvsig.expressionevaluator.impl.function.programming.GetattrFunction; |
... | ... | |
81 | 82 |
this.addFunction(new VarFunction()); |
82 | 83 |
this.addFunction(new $$ConstantFunction()); |
83 | 84 |
this.addFunction(new $$IdentifierFunction()); |
85 |
this.addFunction(new CreateHtmlPanelFunction()); |
|
84 | 86 |
} |
85 | 87 |
|
86 | 88 |
private void addOperator(Function operator) { |
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/symboltable/BasicImageSymbolTable.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import org.gvsig.expressionevaluator.Function; |
4 | 4 |
import org.gvsig.expressionevaluator.impl.function.image.AffineTransformFunction; |
5 |
import org.gvsig.expressionevaluator.impl.function.image.ImageFromHTMLFunction; |
|
5 | 6 |
import org.gvsig.expressionevaluator.impl.function.image.ImageHeightFunction; |
6 | 7 |
import org.gvsig.expressionevaluator.impl.function.image.ImageHorizontalflipFunction; |
7 | 8 |
import org.gvsig.expressionevaluator.impl.function.image.ImageReadFunction; |
... | ... | |
35 | 36 |
this.addFunction(new ImageTransformFunction()); |
36 | 37 |
this.addFunction(new ImageVerticalflipFunction()); |
37 | 38 |
this.addFunction(new ImageWidthFunction()); |
39 |
this.addFunction(new ImageFromHTMLFunction()); |
|
38 | 40 |
} |
39 | 41 |
|
40 | 42 |
private void addOperator(Function operator) { |
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 | ||
---|---|---|
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 |
} |
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/programming/CreateHtmlPanelFunction.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
|
2 |
|
|
3 |
import java.awt.Dimension; |
|
4 |
import java.awt.image.BufferedImage; |
|
5 |
import java.net.URL; |
|
6 |
import javax.swing.ImageIcon; |
|
7 |
import javax.swing.JLabel; |
|
8 |
import org.apache.commons.lang3.Range; |
|
9 |
import org.gvsig.expressionevaluator.Function; |
|
10 |
import org.gvsig.expressionevaluator.Interpreter; |
|
11 |
import org.gvsig.expressionevaluator.impl.function.image.ImageFromHTMLFunction; |
|
12 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
13 |
import org.slf4j.Logger; |
|
14 |
import org.slf4j.LoggerFactory; |
|
15 |
|
|
16 |
public class CreateHtmlPanelFunction extends AbstractFunction { |
|
17 |
private static final Logger LOGGER = LoggerFactory.getLogger(CreateHtmlPanelFunction.class); |
|
18 |
|
|
19 |
public CreateHtmlPanelFunction() { |
|
20 |
super(Function.GROUP_PROGRAMMING, |
|
21 |
"CREATE_HTMLPANEL", |
|
22 |
Range.is(1), |
|
23 |
null, |
|
24 |
"CREATE_HTMLPANEL"+ "({{with}}, height, source)", |
|
25 |
new String[]{ |
|
26 |
"width - with of the image in pixels", |
|
27 |
"height - height of the image in pixels", |
|
28 |
"source - HTML source or URL" |
|
29 |
}, |
|
30 |
"JPanel", |
|
31 |
false |
|
32 |
); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public boolean allowConstantFolding() { |
|
37 |
return true; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
42 |
int width = this.getInt(args, 0); |
|
43 |
int height = this.getInt(args, 1); |
|
44 |
String source = this.getStr(args, 2); |
|
45 |
URL url = null; |
|
46 |
try { |
|
47 |
url = new URL(source); |
|
48 |
} catch (Exception ex) { |
|
49 |
LOGGER.debug("Can't parse url", ex); |
|
50 |
} |
|
51 |
BufferedImage img = ImageFromHTMLFunction.createImageFromHTML(width, height, url, source); |
|
52 |
if( img == null ) { |
|
53 |
return null; |
|
54 |
} |
|
55 |
JLabel comp = new JLabel(new ImageIcon(img)); |
|
56 |
comp.setPreferredSize(new Dimension(width, height)); |
|
57 |
comp.setSize(new Dimension(width, height)); |
|
58 |
comp.setOpaque(false); |
|
59 |
return comp; |
|
60 |
} |
|
61 |
} |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/pom.xml | ||
---|---|---|
50 | 50 |
<artifactId>org.gvsig.tools.util.impl</artifactId> |
51 | 51 |
<scope>test</scope> |
52 | 52 |
</dependency> |
53 |
<dependency> |
|
54 |
<groupId>net.sf.cssbox</groupId> |
|
55 |
<artifactId>cssbox</artifactId> |
|
56 |
<version>5.0.0</version> |
|
57 |
<scope>compile</scope> |
|
58 |
</dependency> |
|
53 | 59 |
</dependencies> |
54 | 60 |
|
55 | 61 |
<build> |
Also available in: Unified diff