use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method isEqual.
@Override
public final boolean isEqual(Message o, String skipProperties) {
BaseMessageImpl other = (BaseMessageImpl) o;
if (!other.getName().equals(this.getName())) {
return false;
}
// Check sub message structure.
List<Message> allOther = other.messageList;
if (allOther != null && messageList == null) {
return false;
}
if (messageList != null && allOther == null) {
return false;
}
if (allOther != null && allOther.size() != messageList.size()) {
return false;
}
if (allOther != null) {
for (int i = 0; i < allOther.size(); i++) {
Message otherMsg = allOther.get(i);
boolean match = false;
for (int j = 0; j < messageList.size(); j++) {
Message myMsg = messageList.get(j);
if (myMsg.isEqual(otherMsg, skipProperties)) {
match = true;
j = messageList.size() + 1;
}
}
if (!match) {
return false;
}
}
}
// Check property structure.
ArrayList<Property> allOtherProps = other.getAllProperties();
ArrayList<Property> allMyProps = this.getAllProperties();
if (allOtherProps.size() != allMyProps.size()) {
return false;
}
for (int i = 0; i < allOtherProps.size(); i++) {
Property otherProp = allOtherProps.get(i);
boolean match = false;
// Check whether property name exists in skipProperties list.
if (skipProperties.indexOf(otherProp.getName()) != -1) {
match = true;
} else {
for (int j = 0; j < allMyProps.size(); j++) {
Property myProp = allMyProps.get(j);
if (myProp.isEqual(otherProp)) {
match = true;
j = allMyProps.size() + 1;
}
}
}
if (!match) {
return false;
}
}
return true;
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method getMessages.
/**
* Return all messages that match a given regular expression. Regular expression
* may include sub-messages and even absolute message references starting at the
* root level.
*/
@Override
public List<Message> getMessages(String regularExpression) {
List<Message> sub = null;
List<Message> sub2 = null;
if (regularExpression.startsWith(Navajo.PARENT_MESSAGE + Navajo.MESSAGE_SEPARATOR)) {
regularExpression = regularExpression.substring((Navajo.PARENT_MESSAGE + Navajo.MESSAGE_SEPARATOR).length());
return getParentMessage().getMessages(regularExpression);
} else if (regularExpression.startsWith(Navajo.MESSAGE_SEPARATOR)) {
return myDocRoot.getMessages(regularExpression);
} else // Contains submessages.
if (regularExpression.indexOf(Navajo.MESSAGE_SEPARATOR) != -1) {
// contains
// a path, descent it first
StringTokenizer tok = new StringTokenizer(regularExpression, Navajo.MESSAGE_SEPARATOR);
Message m = null;
while (tok.hasMoreElements()) {
String msgName = tok.nextToken();
if (sub == null) {
// First message in path.
sub = getMessages(msgName);
} else {
// Subsequent submessages in path.
ArrayList<Message> messages = new ArrayList<Message>();
for (int i = 0; i < sub.size(); i++) {
m = sub.get(i);
sub2 = m.getMessages(msgName);
messages.addAll(sub2);
}
sub = messages;
}
}
return sub;
} else {
ArrayList<Message> result = new ArrayList<>();
try {
String index = null;
if (regularExpression.indexOf("@") != -1) {
StringTokenizer arEl = new StringTokenizer(regularExpression, "@");
regularExpression = arEl.nextToken();
index = arEl.nextToken();
}
Pattern pattern = Pattern.compile(regularExpression);
if (messageList != null) {
for (int i = 0; i < messageList.size(); i++) {
BaseMessageImpl m = (BaseMessageImpl) messageList.get(i);
String name = m.getName();
String type = m.getType();
if (type.equals(Message.MSG_TYPE_ARRAY) && pattern.matcher(name).matches()) {
// message is array type add all children.
if (index == null) {
if (m.messageList != null) {
result.addAll(m.messageList);
}
} else {
if (index.indexOf("=") >= 0) {
String propertyName = index.split("=")[0];
String propertyValue = index.split("=")[1];
// Find array element.
for (int x = 0; x < m.getArraySize(); x++) {
Message am = m.getMessage(x);
if (am.getProperty(propertyName) != null) {
if (am.getProperty(propertyName).getValue().equals(propertyValue)) {
result.add(am);
}
}
}
} else {
try {
if (m.getMessage(Integer.parseInt(index)) != null) {
result.add(m.getMessage(Integer.parseInt(index)));
}
} catch (Exception pe) {
throw new NavajoExceptionImpl("Could not parse array index: " + index);
}
}
}
} else {
if (pattern.matcher(name).matches()) {
result.add(messageList.get(i));
}
}
}
}
} catch (Exception re) {
throw new NavajoExceptionImpl(re);
}
return result;
}
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class BaseMessageImpl method copy.
@Override
public final Message copy() {
Navajo empty = NavajoFactory.getInstance().createNavajo();
Message result = copy(empty);
empty.addMessage(result);
return result;
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class JSONTMLImpl method formatMessages.
private void formatMessages(JsonGenerator jg, List<Message> messages) throws Exception {
for (Message m : messages) {
String origName = TOP_LEVEL_MSG;
try {
if (skipTopLevelMessage && !m.isArrayMessage()) {
origName = m.getName();
m.setName(TOP_LEVEL_MSG);
}
format(jg, m, false);
} finally {
if (skipTopLevelMessage) {
m.setName(origName);
}
}
}
}
use of com.dexels.navajo.document.Message in project navajo by Dexels.
the class JSONTMLImpl method getEntityTemplateProperty.
private Property getEntityTemplateProperty(Property prop) {
// message in the entity template
if (entityTemplate == null) {
return null;
}
String fullPath = prop.getFullPropertyName();
if (!fullPath.contains("@")) {
return entityTemplate.getProperty(prop.getFullPropertyName());
}
// We have an array message somewhere. Lets try to get the correct property from the entity template
StringTokenizer st = new StringTokenizer(prop.getParentMessage().getPath(), "/");
Message next = entityTemplate.getMessage(st.nextToken());
while (next != null && st.hasMoreTokens()) {
String subpath = st.nextToken();
if (subpath.contains("@")) {
// Strip the @ part
subpath = subpath.substring(0, subpath.indexOf('@'));
next = next.getMessage(subpath).getDefinitionMessage();
} else {
next = next.getMessage(subpath);
}
}
if (next != null) {
return next.getProperty(prop.getName());
}
return null;
}
Aggregations