use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class XPath method isMatching0.
protected boolean isMatching0(Connector connector) {
NodeList nodeList = null;
int length = 0;
HtmlConnector htmlConnector = (HtmlConnector) connector;
Document xmlDocument = htmlConnector.getCurrentXmlDocument();
TwsCachedXPathAPI xpathApi = htmlConnector.context.getXpathApi();
if ((xmlDocument == null) || (xpathApi == null)) {
Engine.logBeans.warn((xmlDocument == null) ? "(XPath) Current DOM of HtmlConnector is Null!" : "TwsCachedXPathAPI of HtmlConnector is Null!");
return false;
}
try {
nodeList = xpathApi.selectNodeList(xmlDocument, xpath);
} catch (TransformerException e) {
return false;
}
length = nodeList.getLength();
return (length > 0) ? true : false;
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class Set method getServiceResult.
protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
Element root = document.getDocumentElement();
Document post = null;
Element response = document.createElement("response");
try {
Map<String, DatabaseObject> map = com.twinsoft.convertigo.engine.admin.services.projects.Get.getDatabaseObjectByQName(request);
xpath = new TwsCachedXPathAPI();
post = XMLUtils.parseDOM(request.getInputStream());
postElt = document.importNode(post.getFirstChild(), true);
String objectQName = xpath.selectSingleNode(postElt, "./@qname").getNodeValue();
DatabaseObject object = map.get(objectQName);
if (object instanceof Project) {
Project project = (Project) object;
String objectNewName = getPropertyValue(object, "name").toString();
Engine.theApp.databaseObjectsManager.renameProject(project, objectNewName);
map.remove(objectQName);
map.put(project.getQName(), project);
}
BeanInfo bi = CachedIntrospector.getBeanInfo(object.getClass());
PropertyDescriptor[] propertyDescriptors = bi.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String propertyName = propertyDescriptor.getName();
Method setter = propertyDescriptor.getWriteMethod();
Class<?> propertyTypeClass = propertyDescriptor.getReadMethod().getReturnType();
if (propertyTypeClass.isPrimitive()) {
propertyTypeClass = ClassUtils.primitiveToWrapper(propertyTypeClass);
}
try {
String propertyValue = getPropertyValue(object, propertyName).toString();
Object oPropertyValue = createObject(propertyTypeClass, propertyValue);
if (object.isCipheredProperty(propertyName)) {
Method getter = propertyDescriptor.getReadMethod();
String initialValue = (String) getter.invoke(object, (Object[]) null);
if (oPropertyValue.equals(initialValue) || DatabaseObject.encryptPropertyValue(initialValue).equals(oPropertyValue)) {
oPropertyValue = initialValue;
} else {
object.hasChanged = true;
}
}
if (oPropertyValue != null) {
Object[] args = { oPropertyValue };
setter.invoke(object, args);
}
} catch (IllegalArgumentException e) {
}
}
Engine.theApp.databaseObjectsManager.exportProject(object.getProject());
response.setAttribute("state", "success");
response.setAttribute("message", "Project have been successfully updated!");
} catch (Exception e) {
Engine.logAdmin.error("Error during saving the properties!\n" + e.getMessage());
response.setAttribute("state", "error");
response.setAttribute("message", "Error during saving the properties!");
Element stackTrace = document.createElement("stackTrace");
stackTrace.setTextContent(e.getMessage());
root.appendChild(stackTrace);
} finally {
xpath.resetCache();
}
root.appendChild(response);
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class BeansDefaultValues method updateBeansDefaultValues.
private static Document updateBeansDefaultValues(Document document) throws Exception {
try (InputStream dbInputstream = BeansDefaultValues.class.getResourceAsStream("/com/twinsoft/convertigo/beans/database_objects.xml")) {
Document documentBeansXmlDatabase = XMLUtils.getDefaultDocumentBuilder().parse(dbInputstream);
TwsCachedXPathAPI xpath = new TwsCachedXPathAPI();
EnginePropertiesManager.initProperties();
SortedSet<Node> nodes = new TreeSet<Node>((n1, n2) -> {
return n1.getNodeValue().compareTo(n2.getNodeValue());
});
nodes.addAll(xpath.selectList(documentBeansXmlDatabase, "//bean/@classname"));
Element beans = document.getDocumentElement();
if (beans == null) {
beans = document.createElement("beans");
document.appendChild(beans);
}
String nVersion = VersionUtils.normalizeVersionString(ProductVersion.productVersion);
for (Node node : nodes) {
String classname = node.getNodeValue();
DatabaseObject dbo = (DatabaseObject) Class.forName(classname).getConstructor().newInstance();
Element def = dbo.toXml(document);
if (def.hasAttribute("priority")) {
def.setAttribute("priority", "0");
}
Element eBean = getBeanForVersion(xpath, beans, classname, nVersion);
if (eBean != null) {
String eVersion = eBean.getAttribute("version");
eBean.removeAttribute("version");
if (!checkIsSame(def, eBean)) {
if (eVersion.equals(nVersion)) {
beans.removeChild(eBean);
}
def.setAttribute("version", nVersion);
beans.insertBefore(def, beans.getFirstChild());
}
eBean.setAttribute("version", eVersion);
} else {
def.setAttribute("version", nVersion);
beans.insertBefore(def, beans.getFirstChild());
}
}
;
return document;
}
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class RequestableStep method getPostQuery.
protected String getPostQuery(Scriptable scope) throws EngineException {
StepVariable stepVariable;
String postQuery = "";
int len = numberOfVariables();
String variableName;
int variableVisibility;
for (int i = 0; i < len; i++) {
stepVariable = (StepVariable) getVariable(i);
variableName = stepVariable.getName();
variableVisibility = stepVariable.getVisibility();
try {
// Source value
Object variableValue = stepVariable.getSourceValue();
if (variableValue instanceof NodeList && ((NodeList) variableValue).getLength() == 0) {
if (getProject().isStrictMode() && !stepVariable.isMultiValued()) {
// override with null (fix #24)
variableValue = null;
}
}
if (variableValue != null) {
Engine.logBeans.trace("(RequestableStep) found value from source: " + Visibility.Logs.printValue(variableVisibility, variableValue));
}
// Otherwise Scope parameter
if (variableValue == null) {
Scriptable searchScope = scope;
while ((variableValue == null) && (searchScope != null)) {
variableValue = searchScope.get(variableName, searchScope);
Engine.logBeans.trace("(RequestableStep) found value from scope: " + Visibility.Logs.printValue(variableVisibility, variableValue));
if (variableValue instanceof Undefined) {
variableValue = null;
} else if (variableValue instanceof UniqueTag && ((UniqueTag) variableValue).equals(UniqueTag.NOT_FOUND)) {
variableValue = null;
} else if (variableValue instanceof NativeJavaObject) {
variableValue = ((NativeJavaObject) variableValue).unwrap();
}
if (variableValue == null) {
// looks up in parent's scope
searchScope = searchScope.getParentScope();
}
}
}
// Otherwise context parameter
if (variableValue == null) {
variableValue = (sequence.context.get(variableName) == null ? null : sequence.context.get(variableName));
if (variableValue != null)
Engine.logBeans.trace("(RequestableStep) found value from context: " + Visibility.Logs.printValue(variableVisibility, variableValue));
}
// Otherwise sequence step default value
if (variableValue == null) {
variableValue = getVariableValue(variableName);
if (variableValue != null)
Engine.logBeans.trace("(RequestableStep) found default value from step: " + Visibility.Logs.printValue(variableVisibility, variableValue));
}
// otherwise value not found
if (variableValue == null) {
Engine.logBeans.trace("(RequestableStep) Did not find any value for \"" + variableName + "\", ignore it");
} else {
if (bInternalInvoke) {
if (stepVariable.isMultiValued() && getProject().isStrictMode() && variableValue instanceof NodeList) {
String subXPath = ((StepMultiValuedVariable) stepVariable).getSubXPath();
if (subXPath.isEmpty()) {
variableValue = XMLUtils.toNodeArray((NodeList) variableValue);
} else {
TwsCachedXPathAPI xpathAPI = new TwsCachedXPathAPI(this.getProject());
NodeList nodeList = (NodeList) variableValue;
NodeList[] nodeLists = new NodeList[nodeList.getLength()];
for (int j = 0; j < nodeLists.length; j++) {
try {
nodeLists[j] = xpathAPI.selectNodeList(nodeList.item(j), subXPath);
} catch (TransformerException e) {
Engine.logBeans.debug("(RequestableStep) Failed to select subXpath", e);
}
}
variableValue = nodeLists;
}
}
request.put(variableName, variableValue);
} else {
String parameterValue;
if (variableValue instanceof NodeList) {
NodeList list = (NodeList) variableValue;
if (list != null) {
if (list.getLength() == 0) {
// Specifies here empty multivalued variable (HTTP invoque only)
postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
} else {
for (int j = 0; j < list.getLength(); j++) {
parameterValue = getNodeValue(list.item(j));
postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
}
}
}
} else if (variableValue instanceof NativeJavaArray) {
Object object = ((NativeJavaArray) variableValue).unwrap();
List<String> list = GenericUtils.toString(Arrays.asList((Object[]) object));
if (list.size() == 0) {
// Specifies here empty multivalued variable (HTTP invoque only)
postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
} else {
for (String value : list) {
postQuery = addParamToPostQuery(variableName, value, postQuery);
}
}
} else if (variableValue instanceof Collection<?>) {
List<String> list = GenericUtils.toString((Collection<?>) variableValue);
if (list.size() == 0) {
// Specifies here empty multivalued variable (HTTP invoque only)
postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
} else {
for (String value : list) {
postQuery = addParamToPostQuery(variableName, value, postQuery);
}
}
} else if (variableValue instanceof String) {
parameterValue = variableValue.toString();
postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
} else {
parameterValue = variableValue.toString();
postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
}
}
}
} catch (ClassCastException e) {
Engine.logBeans.warn("(RequestableStep) Ignoring parameter '" + variableName + "' because its value is not a string");
}
}
if (bInternalInvoke) {
return null;
} else {
if (Engine.logBeans.isTraceEnabled()) {
Engine.logBeans.trace("(RequestableStep) postQuery :" + Visibility.Logs.replaceVariables(getVariables(), postQuery));
}
return postQuery;
}
}
use of com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI in project convertigo by convertigo.
the class IfXpathExistsStatement method execute.
@Override
public boolean execute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.execute(javascriptContext, scope)) {
HtmlConnector htmlConnector = getConnector();
Document xmlDocument = htmlConnector.getCurrentXmlDocument();
TwsCachedXPathAPI xpathApi = htmlConnector.context.getXpathApi();
if ((xmlDocument == null) || (xpathApi == null)) {
Engine.logBeans.warn((xmlDocument == null) ? "(XPath) Current DOM of HtmlConnector is Null!" : "TwsCachedXPathAPI of HtmlConnector is Null!");
return false;
}
evaluate(javascriptContext, scope, getCondition(), "xpath", false);
String jsXpath = evaluated.toString();
NodeList nodeList = null;
try {
nodeList = xpathApi.selectNodeList(xmlDocument, jsXpath);
} catch (TransformerException e) {
return false;
} catch (ClassCastException e) {
return false;
}
if (nodeList == null)
return false;
if (nodeList.getLength() == 0)
return false;
return executeNextStatement(javascriptContext, scope);
}
}
return false;
}
Aggregations