use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.
the class TslCompiler method operationsNode.
public String operationsNode(int ident, Element n) throws MappingException {
StringBuilder result = new StringBuilder();
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeName().equals("operation")) {
Element e = (Element) children.item(i);
String entity = e.getAttribute("entity");
String service = e.getAttribute("service");
String tenant = e.getAttribute("tenant");
String validationService = e.getAttribute("validationService");
String method = e.getAttribute("method");
String debug = e.getAttribute("debug");
String scopes = e.getAttribute("scopes");
String description = e.getAttribute("description");
result.append(printIdent(ident) + "if (true) {\n");
String operationString = "com.dexels.navajo.document.Operation o = " + "NavajoFactory.getInstance().createOperation(access.getOutputDoc(), " + "\"" + method + "\", \"" + service;
if (!validationService.equals("")) {
operationString += "\", \"" + validationService;
}
operationString += "\", \"" + entity + "\", null);\n";
result.append(printIdent(ident + 2) + operationString);
// Find extra message definition.
NodeList extraMessages = e.getChildNodes();
String extraMessageName = null;
Element extraMessageElement = null;
for (int j = 0; j < extraMessages.getLength(); j++) {
if (extraMessages.item(j).getNodeName().equals("message")) {
extraMessageElement = (Element) extraMessages.item(j);
extraMessageName = extraMessageElement.getAttribute("name");
break;
}
}
try {
if (extraMessageName != null) {
DOMSource domSource = new DOMSource(extraMessageElement);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(domSource, sr);
String extraNavajo = removeNewLines("<tml>" + sw.toString().replace('\"', '\'') + "</tml>");
String extraNavajoOperation = "Navajo extra = NavajoFactory.getInstance().createNavajo(new java.io.StringReader(\"" + extraNavajo + "\"));\n" + "o.setExtraMessage(extra.getMessage(\"" + extraMessageName + "\"));\n";
result.append(printIdent(ident + 2) + extraNavajoOperation);
}
} catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e1) {
throw new MappingException("Error parsing operations", e1);
}
if (debug != null && !debug.equals("")) {
result.append(printIdent(ident + 2) + "o.setDebug(\"" + debug + "\");\n");
}
if (tenant != null && !tenant.equals("")) {
result.append(printIdent(ident + 2) + "o.setTenant(\"" + tenant + "\");\n");
}
if (scopes != null && !scopes.equals("")) {
result.append(printIdent(ident + 2) + "o.setScopes(\"" + scopes + "\");\n");
}
if (description != null && !description.equals("")) {
result.append(printIdent(ident + 2) + "o.setDescription(\"" + description + "\");\n");
}
result.append(printIdent(ident + 2) + "access.getOutputDoc().addOperation(o);\n");
result.append(printIdent(ident) + "}\n");
}
}
return result.toString();
}
use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.
the class DomainObjectMapper method getMethodReference.
/**
* Returns a method associated with a propertyName getter
*
* @param myClass the class to introspect
* @param propertyName the attribute to find the getter
* @param arguments object array to be passed as method parameters
* @return
* @throws MappingException
*/
private final Method getMethodReference(Class myClass, String propertyName, Object[] arguments) throws MappingException {
StringBuffer key = new StringBuffer();
key.append(propertyName);
// Determine method unique method key:
Class[] classArray = null;
if (arguments != null) {
// Get method with arguments.
classArray = new Class[arguments.length];
for (int i = 0; i < arguments.length; i++) {
classArray[i] = arguments[i].getClass();
key.append(arguments[i].getClass().getName());
}
}
java.lang.reflect.Method m = methods.get(key.toString());
if (m == null) {
java.lang.reflect.Method[] all = myClass.getMethods();
for (int i = 0; i < all.length; i++) {
if (all[i].getName().equalsIgnoreCase("get" + propertyName)) {
m = all[i];
if (equalsParameterTypes(m.getParameterTypes(), classArray)) {
methods.put(key.toString(), m);
break;
}
}
}
if (m == null) {
throw new MappingException("Could not find getter in class " + myClass.getCanonicalName() + " for attribute: " + propertyName);
}
}
return m;
}
use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.
the class MappingUtils method getAttributeValue.
/**
* The next two methods: getAttribute()/setAttribute() are used for the set/get
* functionality of the new Mappable interface (see also Java Beans standard).
* The use of set/get methods enables the use of triggers that can be
* implemented within the set/get methods. So if there is a field: private
* double noot; within a Mappable object, the following methods need to be
* implemented: public double getNoot(); and public void setNoot(double d);
*/
public static final Object getAttributeValue(MappableTreeNode o, String name, Object[] arguments) throws UserException, MappingException {
while ((name.indexOf("../")) != -1) {
o = o.parent;
if (o == null) {
throw new MappingException("Null parent object encountered: " + name);
}
name = name.substring(3, name.length());
}
Object result = getAttributeObject(o, name, arguments);
if (result != null && result.getClass().isArray()) {
// Encountered array cast to ArrayList.
Object[] array = (Object[]) result;
ArrayList list = new ArrayList();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
return list;
} else {
return result;
}
}
use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.
the class MappingUtils method addMessage.
/**
* @param template
*/
public static final Message[] addMessage(Navajo doc, Message parent, String message, String template, int count, String type, String mode) throws NavajoException, MappingException {
/**
* Added 22/5/2007: support for relative message creation.
*/
if ((message.length() == 0 || message.charAt(0) != '/') && message.indexOf(Navajo.MESSAGE_SEPARATOR) != -1 && parent == null) {
throw new MappingException("No submessage constructs allowed in non-nested <message> tags: " + message);
}
Message[] messages = new Message[count];
Message msg = null;
int index = 0;
// Check for existing message.
Message existing = null;
/**
* Get the real parent message given the fact that message could contain a
* relative name.
*/
parent = (message.length() > 0 && message.charAt(0) == '/' ? null : getParentMessage(parent, message));
if (parent != null) {
existing = parent.getMessage(getBaseMessageName(message));
} else {
existing = doc.getMessage(getBaseMessageName(message));
}
// this message my parent instead of reusing existing message.
if (existing != null && existing.isArrayMessage() && !Message.MSG_TYPE_ARRAY.equals(type)) {
parent = existing;
existing = null;
}
if (Message.MSG_MODE_OVERWRITE.equals(mode) && existing != null) {
if (parent != null) {
parent.removeMessage(existing);
} else {
doc.removeMessage(existing);
}
existing = null;
}
// is put under the existing array message parent.
if ((existing != null)) {
if (parent != null && !parent.isArrayMessage()) {
messages[0] = existing;
return messages;
} else if (parent == null) {
messages[0] = existing;
return messages;
}
}
if (getBaseMessageName(message).contains(Navajo.MESSAGE_SEPARATOR)) {
throw new MappingException("No submessage constructs allowed in messagename: " + message);
}
/**
* Added getBaseMessageName to support relative message creation.
*/
msg = doc.getNavajoFactory().createMessage(doc, getBaseMessageName(message));
if (mode != null && !mode.equals("")) {
msg.setMode(mode);
}
if (count > 1) {
msg.setName(getBaseMessageName(message) + "0");
msg.setIndex(0);
// if (!mode.equals(Message.MSG_MODE_IGNORE)) {
if (parent == null) {
msg = doc.addMessage(msg, false);
} else {
msg = parent.addMessage(msg, false);
}
// }
messages[index++] = msg;
} else if (count == 1) {
// if (!mode.equals(Message.MSG_MODE_IGNORE)) {
if (parent == null) {
msg = doc.addMessage(msg, false);
} else {
msg = parent.addMessage(msg, false);
}
// }
messages[index++] = msg;
if (type != null && !type.equals("")) {
msg.setType(type);
}
}
if (Message.MSG_MODE_IGNORE.equals(mode)) {
msg.setMode(mode);
}
// Add additional messages based on the first messages that was added.
for (int i = 1; i < count; i++) {
Message extra = doc.copyMessage(msg, doc);
extra.setName(getBaseMessageName(message) + i);
extra.setIndex(i);
if (parent == null) {
extra = doc.addMessage(extra, false);
} else {
extra = parent.addMessage(extra, false);
}
messages[index++] = extra;
}
return messages;
}
use of com.dexels.navajo.script.api.MappingException in project navajo by Dexels.
the class MappingUtils method getTypeForField.
private static final Type getTypeForField(String name, Class c, boolean fetchGenericType) throws MappingException {
if (name.indexOf('(') != -1) {
name = name.substring(0, name.indexOf('('));
}
try {
if (c.getField(name).getType().equals(Iterator.class) && fetchGenericType) {
ParameterizedType pt = (ParameterizedType) c.getField(name).getGenericType();
return (pt.getActualTypeArguments()[0]);
} else {
return c.getField(name).getType();
}
} catch (Exception e) {
try {
if (c.getDeclaredField(name).getType().equals(Iterator.class) && fetchGenericType) {
ParameterizedType pt = (ParameterizedType) c.getDeclaredField(name).getGenericType();
return (pt.getActualTypeArguments()[0]);
} else {
return c.getDeclaredField(name).getType();
}
} catch (Exception e2) {
Method[] methods = c.getMethods();
String getMethod = constructGetMethod(name);
String setMethod = constructSetMethod(name);
for (int j = 0; j < methods.length; j++) {
if (methods[j].getName().equals(getMethod)) {
Method m = methods[j];
if (m.getReturnType().equals(Iterator.class) && fetchGenericType) {
ParameterizedType pt = (ParameterizedType) m.getGenericReturnType();
return (pt.getActualTypeArguments()[0]);
} else {
return m.getReturnType();
}
} else if (methods[j].getName().equals(setMethod)) {
Class[] types = methods[j].getParameterTypes();
return types[0];
}
}
throw new MappingException("Could not find method " + constructGetMethod(name) + " in Mappable object: " + c.getSimpleName());
}
}
}
Aggregations