use of com.dexels.navajo.document.stream.api.Msg in project navajo by Dexels.
the class NavajoStreamToMutableMessageStream method processNavajoEvent.
public Flowable<Message> processNavajoEvent(NavajoStreamEvent n) {
switch(n.type()) {
case NAVAJO_STARTED:
return Flowable.empty();
case MESSAGE_STARTED:
Message prMessage = null;
if (!messageStack.isEmpty()) {
prMessage = messageStack.peek();
}
String mode = (String) n.attribute("mode");
Message msg = NavajoFactory.getInstance().createMessage(null, n.path(), Message.MSG_TYPE_SIMPLE);
msg.setMode(mode);
if (prMessage == null) {
// assemble.addMessage(msg);
// currentMessage.set(msg);
} else {
prMessage.addMessage(msg);
}
messageStack.push(msg);
tagStack.push(n.path());
return Flowable.empty();
case MESSAGE:
// if(messageStack.isEmpty())
Message msgParent = null;
if (messageStack.isEmpty()) {
msgParent = NavajoFactory.getInstance().createMessage(null, n.path(), Message.MSG_TYPE_SIMPLE);
} else {
msgParent = messageStack.pop();
}
Msg mm = (Msg) n.body();
List<Prop> msgProps = mm.properties();
for (Prop e : msgProps) {
msgParent.addProperty(createTmlProperty(e));
}
if (emitStack()) {
tagStack.pop();
return Flowable.just(msgParent);
}
tagStack.pop();
return Flowable.empty();
case ARRAY_STARTED:
tagStack.push(n.path());
Message parentMessage = null;
if (!messageStack.isEmpty()) {
parentMessage = messageStack.peek();
}
Message arr = NavajoFactory.getInstance().createMessage(null, n.path(), Message.MSG_TYPE_ARRAY);
if (parentMessage == null) {
// assemble.addMessage(arr);
} else {
parentMessage.addMessage(arr);
}
messageStack.push(arr);
return Flowable.empty();
case ARRAY_DONE:
// String apath = currentPath();
// arrayCounts.remove(apath);
this.messageStack.pop();
return Flowable.empty();
case ARRAY_ELEMENT_STARTED:
String arrayElementName = tagStack.peek();
Message newElt = NavajoFactory.getInstance().createMessage(null, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT);
Message arrParent = messageStack.peek();
arrParent.addElement(newElt);
messageStack.push(newElt);
return Flowable.empty();
case ARRAY_ELEMENT:
Message elementParent = messageStack.pop();
Msg msgElement = (Msg) n.body();
List<Prop> elementProps = msgElement.properties();
for (Prop e : elementProps) {
elementParent.addProperty(createTmlProperty(e));
}
if (emitStack()) {
// tagStack.pop();
return Flowable.just(elementParent);
}
// tagStack.pop();
return Flowable.empty();
case MESSAGE_DEFINITION_STARTED:
return Flowable.empty();
case MESSAGE_DEFINITION:
// deferredMessages.get(stripIndex(n.path())).setDefinitionMessage((Message) n.body());
return Flowable.empty();
case NAVAJO_DONE:
return Flowable.empty();
default:
return Flowable.empty();
}
}
use of com.dexels.navajo.document.stream.api.Msg in project navajo by Dexels.
the class TestMutableStream method testOrderingStreamBug.
@Test
public void testOrderingStreamBug() {
Msg m = Bytes.from(TestRx.class.getClassLoader().getResourceAsStream("misorderedtml.xml"), 128).lift(XML.parseFlowable(10)).concatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).filter(e -> "Member".equals(e.path()) && e.type() == NavajoEventTypes.MESSAGE).firstOrError().map(e -> e.body()).cast(Msg.class).blockingGet();
Assert.assertEquals("Dmitri", m.toImmutableMessage().value("Name").get());
}
use of com.dexels.navajo.document.stream.api.Msg in project navajo by Dexels.
the class NavajoStreamSerializer method processNavajoEvent.
@SuppressWarnings("unchecked")
private void processNavajoEvent(NavajoStreamEvent event, Writer w) {
try {
String name = event.path();
switch(event.type()) {
case MESSAGE_STARTED:
messageIgnoreStack.push("ignore".equals(event.attribute("mode")));
if (!messageIgnoreStack.contains(true)) {
printStartTag(w, INDENT * (tagDepth + 1), true, "message", createMessageAttributes("name=\"" + name + "\"", event.attributes()));
} else {
logger.info("Message IGNORE!");
}
tagDepth++;
messageNameStack.push(name);
break;
case MESSAGE_DEFINITION_STARTED:
messageIgnoreStack.push("ignore".equals(event.attribute("mode")));
if (!messageIgnoreStack.contains(true)) {
printStartTag(w, INDENT * (tagDepth + 1), true, "message", createMessageAttributes("name=\"" + name + "\" type=\"definition\"", event.attributes()));
}
tagDepth++;
messageNameStack.push(name + "@definition");
break;
case ARRAY_ELEMENT_STARTED:
messageIgnoreStack.push("ignore".equals(event.attribute("mode")));
String arrayName = messageNameStack.peek();
String pth = currentPath();
AtomicInteger index = counterStack.peek();
if (index == null) {
logger.error("no current index for path: " + pth);
}
if (!"ignore".equals(event.attribute("mode"))) {
printStartTag(w, INDENT * (tagDepth + 1), true, "message", createMessageAttributes("name=\"" + arrayName + "\" index=\"" + index + "\" type=\"array_element\"", event.attributes()));
tagDepth++;
}
int ind = index.getAndIncrement();
messageNameStack.push("@" + ind);
break;
case MESSAGE:
case MESSAGE_DEFINITION:
case ARRAY_ELEMENT:
messageNameStack.pop();
Msg msgBody = event.message();
List<Prop> properties = msgBody.properties();
if (!messageIgnoreStack.contains(true)) {
for (Prop prop : properties) {
prop.write(w, INDENT * (tagDepth + 1));
}
printEndTag(w, INDENT * tagDepth, "message");
}
messageIgnoreStack.pop();
tagDepth--;
break;
case ARRAY_STARTED:
messageIgnoreStack.push("ignore".equals(event.attribute("mode")));
if (!messageIgnoreStack.contains(true)) {
printStartTag(w, INDENT * (tagDepth + 1), true, "message", "name=\"" + name + "\" type=\"array\"");
}
messageNameStack.push(name);
tagDepth++;
counterStack.push(new AtomicInteger());
// arrayCounter.put(currentPath(), new AtomicInteger(0));
break;
case ARRAY_DONE:
if (!messageIgnoreStack.contains(true)) {
printEndTag(w, INDENT * tagDepth, "message");
}
// printCloseTag(w, INDENT*tagStack.size());
// arrayCounter.remove(currentPath());
tagDepth--;
counterStack.pop();
messageIgnoreStack.pop();
messageNameStack.pop();
break;
case NAVAJO_STARTED:
w.write("<tml>\n");
NavajoHead head = (NavajoHead) event.body();
// head.printElement(w,INDENT);
head.print(w, INDENT);
// h.printElement(w, INDENT);
break;
case NAVAJO_DONE:
List<Method> methods = (List<Method>) event.body();
if (methods.size() > 0) {
printStartTag(w, INDENT * (tagDepth + 1), true, "methods", "");
methods.forEach(e -> {
try {
e.write(w, INDENT * (tagDepth + 2));
} catch (IOException e1) {
logger.error("Error: ", e1);
}
});
printEndTag(w, INDENT * (tagDepth + 1), "methods");
}
w.write("</tml>\n");
break;
case BINARY_STARTED:
int length = (Integer) event.attribute("length");
String description = (String) event.attribute("description");
String direction = (String) event.attribute("direction");
String subtype = (String) event.attribute("subtype");
StringBuilder sb = new StringBuilder("name=\"" + event.path() + "\" type=\"binary\" length=\"" + length + "\"");
if (description != null) {
sb.append(" description=\"" + description + "\"");
}
if (direction != null) {
sb.append(" direction=\"" + direction + "\"");
}
if (subtype != null) {
sb.append(" subtype=\"" + subtype + "\"");
}
printStartTag(w, INDENT * (tagDepth + 1), true, "property", sb.toString());
tagDepth++;
break;
case BINARY_CONTENT:
String body = (String) event.body();
w.write(body);
tagDepth++;
break;
case BINARY_DONE:
tagDepth--;
printEndTag(w, INDENT * tagDepth, "property");
break;
default:
break;
}
} catch (IOException e) {
logger.error("Error: ", e);
}
}
use of com.dexels.navajo.document.stream.api.Msg in project navajo by Dexels.
the class NavajoStreamCollector method processNavajoEvent.
public Optional<Navajo> processNavajoEvent(NavajoStreamEvent n) throws IOException {
switch(n.type()) {
case NAVAJO_STARTED:
createHeader((NavajoHead) n.body());
return Optional.empty();
case MESSAGE_STARTED:
Message prMessage = null;
if (!messageStack.isEmpty()) {
prMessage = messageStack.peek();
}
String mode = (String) n.attribute("mode");
Message msg = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_SIMPLE);
msg.setMode(mode);
if (prMessage == null) {
assemble.addMessage(msg);
} else {
prMessage.addMessage(msg);
}
messageStack.push(msg);
tagStack.push(n.path());
return Optional.empty();
case MESSAGE:
Message msgParent = messageStack.pop();
tagStack.pop();
Msg mm = (Msg) n.body();
List<Prop> msgProps = mm.properties();
for (Prop e : msgProps) {
msgParent.addProperty(createTmlProperty(e));
}
// pushBinaries
for (Entry<String, Binary> e : pushBinaries.entrySet()) {
msgParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
}
pushBinaries.clear();
binaryProperties.clear();
return Optional.empty();
case ARRAY_STARTED:
tagStack.push(n.path());
String path = currentPath();
AtomicInteger cnt = arrayCounts.get(path);
if (cnt == null) {
cnt = new AtomicInteger();
arrayCounts.put(path, cnt);
}
// cnt.incrementAndGet();
Message parentMessage = null;
if (!messageStack.isEmpty()) {
parentMessage = messageStack.peek();
}
Message arr = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_ARRAY);
if (parentMessage == null) {
assemble.addMessage(arr);
} else {
parentMessage.addMessage(arr);
}
messageStack.push(arr);
return Optional.empty();
case ARRAY_DONE:
String apath = currentPath();
arrayCounts.remove(apath);
this.messageStack.pop();
return Optional.empty();
case ARRAY_ELEMENT_STARTED:
String arrayElementName = tagStack.peek();
String arrayPath = currentPath();
AtomicInteger currentCount = arrayCounts.get(arrayPath);
if (currentCount != null) {
String ind = "@" + currentCount.getAndIncrement();
tagStack.push(ind);
}
arrayPath = currentPath();
Message newElt = NavajoFactory.getInstance().createMessage(assemble, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT);
Message arrParent = messageStack.peek();
arrParent.addElement(newElt);
messageStack.push(newElt);
return Optional.empty();
case ARRAY_ELEMENT:
tagStack.pop();
Message elementParent = messageStack.pop();
Msg msgElement = (Msg) n.body();
List<Prop> elementProps = msgElement.properties();
for (Prop e : elementProps) {
elementParent.addProperty(createTmlProperty(e));
}
for (Entry<String, Binary> e : pushBinaries.entrySet()) {
elementParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
}
pushBinaries.clear();
return Optional.empty();
case MESSAGE_DEFINITION_STARTED:
return Optional.empty();
case MESSAGE_DEFINITION:
return Optional.empty();
case NAVAJO_DONE:
@SuppressWarnings("unchecked") List<Method> methodList = (List<Method>) n.body();
methodList.stream().map(m -> NavajoFactory.getInstance().createMethod(assemble, m.name, "")).forEach(e -> assemble.addMethod(e));
return Optional.of(assemble);
case BINARY_STARTED:
String name = n.path();
n.attribute("direction");
this.currentBinary = new Binary();
this.currentBinary.startPushRead();
this.pushBinaries.put(name, currentBinary);
int length = (Integer) n.attribute("length", () -> -1);
String description = (String) n.attribute("description", () -> "");
String direction = (String) n.attribute("direction", () -> "");
String subtype = (String) n.attribute("subtype", () -> "");
this.currentProperty = NavajoFactory.getInstance().createProperty(assemble, name, Property.BINARY_PROPERTY, "", length, description, direction);
this.currentProperty.setSubType(subtype);
return Optional.empty();
case BINARY_CONTENT:
if (this.currentBinary == null) {
// whoops;
}
this.currentBinary.pushContent((String) n.body());
return Optional.empty();
case BINARY_DONE:
this.currentBinary.finishPushContent();
return Optional.empty();
default:
return Optional.empty();
}
}
use of com.dexels.navajo.document.stream.api.Msg in project navajo by Dexels.
the class StreamDocument method messageWithPath.
public static FlowableOperator<NavajoStreamEvent, NavajoStreamEvent> messageWithPath(final String messagePath, final Function<Msg, Msg> operation, boolean filterOthers) {
return new BaseFlowableOperator<NavajoStreamEvent, NavajoStreamEvent>(1) {
@Override
public Subscriber<? super NavajoStreamEvent> apply(Subscriber<? super NavajoStreamEvent> child) throws Exception {
return new Subscriber<NavajoStreamEvent>() {
private final Stack<String> pathStack = new Stack<>();
@Override
public void onComplete() {
operatorComplete(child);
}
@Override
public void onError(Throwable e) {
operatorError(e, child);
}
@Override
public void onNext(NavajoStreamEvent event) {
switch(event.type()) {
case MESSAGE_STARTED:
pathStack.push(event.path());
// operatorRequest(1);
break;
case ARRAY_ELEMENT_STARTED:
// operatorRequest(1);
break;
case MESSAGE:
if (matches(messagePath, pathStack)) {
Msg transformed;
try {
transformed = operation.apply((Msg) event.body());
operatorNext(event, e -> {
return Events.message(transformed, event.path(), event.attributes());
}, child);
} catch (Exception e1) {
logger.error("Unexpected error: ", e1);
}
return;
}
pathStack.pop();
break;
case ARRAY_ELEMENT:
if (matches(messagePath, pathStack)) {
Msg transformed;
try {
transformed = operation.apply((Msg) event.body());
operatorNext(event, e -> {
return Events.arrayElement(transformed, event.attributes());
}, child);
} catch (Exception e1) {
logger.error("Very unexpected exception: ", e1);
e1.printStackTrace();
}
return;
}
break;
// TODO Support these?
case ARRAY_STARTED:
pathStack.push(event.path());
break;
case ARRAY_DONE:
pathStack.pop();
break;
default:
break;
}
if (!filterOthers) {
operatorNext(event, e -> e, child);
} else {
operatorRequest(1);
}
}
private boolean matches(String path, Stack<String> pathStack) {
String joined = String.join("/", pathStack);
return path.equals(joined);
}
@Override
public void onSubscribe(Subscription s) {
operatorSubscribe(s, child);
}
};
}
};
}
Aggregations