Revision 46854 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.api/src/main/java/org/gvsig/expressionevaluator/ExpressionUtils.java
ExpressionUtils.java | ||
---|---|---|
4 | 4 |
import java.io.File; |
5 | 5 |
import java.net.MalformedURLException; |
6 | 6 |
import java.net.URL; |
7 |
import java.nio.charset.StandardCharsets; |
|
7 | 8 |
import java.util.List; |
8 | 9 |
import java.util.regex.Matcher; |
9 | 10 |
import java.util.regex.Pattern; |
11 |
import org.apache.commons.io.IOUtils; |
|
10 | 12 |
import org.apache.commons.lang3.ArrayUtils; |
11 | 13 |
import org.apache.commons.lang3.StringUtils; |
12 | 14 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
... | ... | |
14 | 16 |
import static org.gvsig.expressionevaluator.ExpressionEvaluatorManager.DYNAMICTEXT_STARTTAG; |
15 | 17 |
import org.gvsig.expressionevaluator.spi.DynObjectSymbolTable; |
16 | 18 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
19 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
17 | 20 |
import org.gvsig.tools.dynobject.DynObject; |
21 |
import org.gvsig.tools.resourcesstorage.ResourcesStorage; |
|
22 |
import org.gvsig.tools.script.Script; |
|
18 | 23 |
import org.gvsig.tools.util.ListBuilder; |
24 |
import org.slf4j.Logger; |
|
25 |
import org.slf4j.LoggerFactory; |
|
19 | 26 |
|
20 | 27 |
/** |
21 | 28 |
* |
... | ... | |
23 | 30 |
*/ |
24 | 31 |
@SuppressWarnings("UseSpecificCatch") |
25 | 32 |
public class ExpressionUtils { |
33 |
|
|
34 |
private static final Logger LOGGER = LoggerFactory.getLogger(ExpressionUtils.class); |
|
26 | 35 |
|
27 | 36 |
public static boolean isEmpty(Expression expression) { |
28 | 37 |
return expression == null || expression.isEmpty(); |
... | ... | |
653 | 662 |
String s = expression.getPhrase(); |
654 | 663 |
return StringUtils.defaultIfBlank(s, null); |
655 | 664 |
} |
665 |
|
|
666 |
public static Script createScript(byte[] bytes, String name) { |
|
667 |
try { |
|
668 |
if( StringUtils.isBlank(name)) { |
|
669 |
return null; |
|
670 |
} |
|
671 |
String source = ""; |
|
672 |
if( !ArrayUtils.isEmpty(bytes) ) { |
|
673 |
source = IOUtils.toString(bytes, StandardCharsets.UTF_8.name()); |
|
674 |
} |
|
675 |
ExpressionEvaluatorManager scriptManager = ExpressionEvaluatorLocator.getManager(); |
|
676 |
Script script = scriptManager.createScript(name, source, null); |
|
677 |
return script; |
|
678 |
} catch (Exception ex) { |
|
679 |
LOGGER.warn("Can't create script "+name+".", ex); |
|
680 |
return null; |
|
681 |
} |
|
682 |
} |
|
683 |
|
|
684 |
public static Script getScript(ResourcesStorage resources, String resourceName) { |
|
685 |
ResourcesStorage.Resource res = null; |
|
686 |
try { |
|
687 |
if( resources==null || StringUtils.isBlank(resourceName)) { |
|
688 |
return null; |
|
689 |
} |
|
690 |
res = resources.getResource(resourceName); |
|
691 |
if( res == null || !res.exists() ) { |
|
692 |
return null; |
|
693 |
} |
|
694 |
String source = IOUtils.toString(res.asInputStream(), StandardCharsets.UTF_8); |
|
695 |
if( StringUtils.isEmpty(source) ) { |
|
696 |
return null; |
|
697 |
} |
|
698 |
ExpressionEvaluatorManager scriptManager = ExpressionEvaluatorLocator.getManager(); |
|
699 |
Script script = scriptManager.createScript(resourceName, source, null); |
|
700 |
return script; |
|
701 |
} catch (Exception ex) { |
|
702 |
LOGGER.warn("Can't load script from resource "+resourceName+".", ex); |
|
703 |
return null; |
|
704 |
} finally { |
|
705 |
DisposeUtils.disposeQuietly(res); |
|
706 |
} |
|
707 |
} |
|
656 | 708 |
} |
657 | 709 |
|
Also available in: Unified diff