Search in sources :

Example 1 with NavajoHead

use of com.dexels.navajo.document.stream.api.NavajoHead 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);
    }
}
Also used : Msg(com.dexels.navajo.document.stream.api.Msg) Prop(com.dexels.navajo.document.stream.api.Prop) Method(com.dexels.navajo.document.stream.api.Method) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead) List(java.util.List)

Example 2 with NavajoHead

use of com.dexels.navajo.document.stream.api.NavajoHead in project navajo by Dexels.

the class StreamSaxHandler method parseHeader.

private final void parseHeader(Map<String, String> headerAttributes) {
    NavajoHead newHeader = NavajoHead.create(headerAttributes, transactionAttributes, piggybackAttriutes, asyncAttributes);
    handler.navajoStart(newHeader);
}
Also used : NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead)

Example 3 with NavajoHead

use of com.dexels.navajo.document.stream.api.NavajoHead 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();
    }
}
Also used : Msg(com.dexels.navajo.document.stream.api.Msg) Logger(org.slf4j.Logger) Method(com.dexels.navajo.document.stream.api.Method) LoggerFactory(org.slf4j.LoggerFactory) Message(com.dexels.navajo.document.Message) IOException(java.io.IOException) HashMap(java.util.HashMap) Stack(java.util.Stack) Select(com.dexels.navajo.document.stream.api.Select) NavajoFactory(com.dexels.navajo.document.NavajoFactory) List(java.util.List) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Selection(com.dexels.navajo.document.Selection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Msg(com.dexels.navajo.document.stream.api.Msg) Entry(java.util.Map.Entry) Optional(java.util.Optional) Header(com.dexels.navajo.document.Header) Property(com.dexels.navajo.document.Property) NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead) Prop(com.dexels.navajo.document.stream.api.Prop) Binary(com.dexels.navajo.document.types.Binary) Navajo(com.dexels.navajo.document.Navajo) Message(com.dexels.navajo.document.Message) Prop(com.dexels.navajo.document.stream.api.Prop) Method(com.dexels.navajo.document.stream.api.Method) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Binary(com.dexels.navajo.document.types.Binary)

Example 4 with NavajoHead

use of com.dexels.navajo.document.stream.api.NavajoHead in project navajo by Dexels.

the class NavajoStreamEvent method toString.

@SuppressWarnings("unchecked")
public String toString() {
    if (type == NavajoEventTypes.NAVAJO_STARTED) {
        NavajoHead h = (NavajoHead) body;
        return "Type: " + type + " path: " + path + " attributes: {" + attributes + "} - RPCNAME: " + h.name() + " user: " + h.username();
    }
    if (type == NavajoEventTypes.MESSAGE) {
        Msg msgBody = (Msg) body;
        List<Prop> contents = msgBody.properties();
        StringBuilder sb = new StringBuilder("Message detected. Name: " + path + " with mode: " + this.attributes.get("mode") + "\n");
        for (Prop prop : contents) {
            sb.append("Prop: " + prop.name() + " = " + prop.value() + " value type: " + prop.type() + "with direction: " + prop.direction() + "\n");
            if (prop.value() != null) {
                sb.append("   -> value class: " + prop.value().getClass() + "\n");
            }
        }
        return sb.toString();
    }
    if (type == NavajoEventTypes.NAVAJO_DONE) {
        List<String> methods = (List<String>) body;
        return "Navajo Done. Methods: " + methods;
    }
    return "Type: " + type + " path: " + path + " attributes: {" + attributes + "} " + body;
}
Also used : Msg(com.dexels.navajo.document.stream.api.Msg) Prop(com.dexels.navajo.document.stream.api.Prop) NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead) List(java.util.List)

Aggregations

NavajoHead (com.dexels.navajo.document.stream.api.NavajoHead)4 Msg (com.dexels.navajo.document.stream.api.Msg)3 Prop (com.dexels.navajo.document.stream.api.Prop)3 List (java.util.List)3 Method (com.dexels.navajo.document.stream.api.Method)2 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Header (com.dexels.navajo.document.Header)1 Message (com.dexels.navajo.document.Message)1 Navajo (com.dexels.navajo.document.Navajo)1 NavajoFactory (com.dexels.navajo.document.NavajoFactory)1 Property (com.dexels.navajo.document.Property)1 Selection (com.dexels.navajo.document.Selection)1 Select (com.dexels.navajo.document.stream.api.Select)1 NavajoStreamEvent (com.dexels.navajo.document.stream.events.NavajoStreamEvent)1 Binary (com.dexels.navajo.document.types.Binary)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1