Search in sources :

Example 1 with Method

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

the class NavajoDomStreamer method processNavajo.

public static List<NavajoStreamEvent> processNavajo(Navajo navajo) {
    List<NavajoStreamEvent> result = new ArrayList<>();
    Navajo output = NavajoFactory.getInstance().createNavajo();
    List<Message> all = navajo.getAllMessages();
    Header h = navajo.getHeader();
    if (h != null) {
        result.add(header(h));
    } else {
        logger.warn("Unexpected case: Deal with tml without header?");
    }
    for (Message message : all) {
        emitMessage(message, result, output);
    }
    NavajoStreamEvent done = done(navajo.getAllMethods().stream().map(e -> new Method(e.getName())).collect(Collectors.toList()));
    result.add(done);
    return result;
}
Also used : Message(com.dexels.navajo.document.Message) Header(com.dexels.navajo.document.Header) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) Method(com.dexels.navajo.document.stream.api.Method) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent)

Example 2 with Method

use of com.dexels.navajo.document.stream.api.Method 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 3 with Method

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

the class StreamSaxHandler method parseMethod.

private final void parseMethod(Map<String, String> h) throws NavajoException {
    String name = h.get("name");
    methods.add(new Method(name));
// currentMethod = NavajoFactory.getInstance().createMethod(currentDocument, name, null);
// currentDocument.addMethod(currentMethod);
}
Also used : Method(com.dexels.navajo.document.stream.api.Method)

Example 4 with Method

use of com.dexels.navajo.document.stream.api.Method 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)

Aggregations

Method (com.dexels.navajo.document.stream.api.Method)4 Header (com.dexels.navajo.document.Header)2 Message (com.dexels.navajo.document.Message)2 Navajo (com.dexels.navajo.document.Navajo)2 Msg (com.dexels.navajo.document.stream.api.Msg)2 NavajoHead (com.dexels.navajo.document.stream.api.NavajoHead)2 Prop (com.dexels.navajo.document.stream.api.Prop)2 NavajoStreamEvent (com.dexels.navajo.document.stream.events.NavajoStreamEvent)2 IOException (java.io.IOException)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 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 Binary (com.dexels.navajo.document.types.Binary)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1