com.sysunite.weaver.nifi.GetXMLNodes.java Source code

Java tutorial

Introduction

Here is the source code for com.sysunite.weaver.nifi.GetXMLNodes.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sysunite.weaver.nifi;

import org.apache.commons.io.IOUtils;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.*;
import org.apache.nifi.annotation.behavior.ReadsAttribute;
import org.apache.nifi.annotation.behavior.ReadsAttributes;
import org.apache.nifi.annotation.behavior.WritesAttribute;
import org.apache.nifi.annotation.behavior.WritesAttributes;
import org.apache.nifi.annotation.lifecycle.OnScheduled;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.SeeAlso;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.processor.exception.FlowFileHandlingException;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.io.InputStreamCallback;
import org.apache.nifi.processor.util.StandardValidators;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;

@Tags({ "weaver, getxmlnodes" })
@CapabilityDescription("Provide a description")
@SeeAlso({})
@ReadsAttributes({ @ReadsAttribute(attribute = "", description = "") })
@WritesAttributes({ @WritesAttribute(attribute = "", description = "") })
public class GetXMLNodes extends AbstractProcessor {

    public static final PropertyDescriptor PROP_XPATH = new PropertyDescriptor.Builder().name("prop_xpath")
            .description("define valid (jcabi) xpath to fetch nodes.").required(true)
            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR).build();

    public static final Relationship MY_RELATIONSHIP = new Relationship.Builder().name("my_relationship")
            .description("Example relationship").build();

    private List<PropertyDescriptor> descriptors;

    private Set<Relationship> relationships;

    @Override
    protected void init(final ProcessorInitializationContext context) {
        final List<PropertyDescriptor> descriptors = new ArrayList<PropertyDescriptor>();
        descriptors.add(PROP_XPATH);
        this.descriptors = Collections.unmodifiableList(descriptors);

        final Set<Relationship> relationships = new HashSet<Relationship>();
        relationships.add(MY_RELATIONSHIP);
        this.relationships = Collections.unmodifiableSet(relationships);
    }

    @Override
    public Set<Relationship> getRelationships() {
        return this.relationships;
    }

    @Override
    public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
        return descriptors;
    }

    @OnScheduled
    public void onScheduled(final ProcessContext context) {

    }

    @Override
    public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {

        final FlowFile flowFileIn = session.get();

        if (flowFileIn == null) {
            return;
        }

        //System.out.println("i am here!!!");

        final AtomicReference<InputStream> savedNodes = new AtomicReference<>();

        //inlezen van flowFileIn
        session.read(flowFileIn, new InputStreamCallback() {

            @Override
            public void process(InputStream isIn) throws IOException {

                try {

                    String contents = IOUtils.toString(isIn);

                    XML xml = new XMLDocument(contents); //"<error>\n<fout a=\"poo\">\n<tiger>Bla</tiger>\n</fout>\n</error>");

                    String aPath = context.getProperty(PROP_XPATH).getValue();

                    //System.out.println(aPath);

                    StringBuffer buffer = new StringBuffer();
                    buffer.append("<root>");

                    int counter = 0;

                    for (XML ibr : xml.nodes(aPath)) {

                        //              if(counter == 3){
                        //               break;
                        //              }

                        buffer.append(ibr.toString());
                        //System.out.println(ibr.toString());

                        //
                        //              FlowFile flowfileOut = session.create();
                        //
                        //        InputStream data = new ByteArrayInputStream(ibr.toString().getBytes());
                        //        flowfileOut = session.importFrom(data, flowfileOut);
                        //
                        //
                        //       session.transfer(flowfileOut, MY_RELATIONSHIP);
                        //      session.commit();

                        //counter++;

                    }

                    buffer.append("</root>");

                    InputStream is = new ByteArrayInputStream(buffer.toString().getBytes()); // geen extra parameters, want dit zorgt voor 'too much output too process'

                    savedNodes.set(is);

                } catch (Exception e) {

                    System.out.println("??????");
                    //System.out.println(e.getMessage());
                }

            }

        });

        session.remove(flowFileIn);

        try {

            String contents = IOUtils.toString(savedNodes.get());

            XML xml = new XMLDocument(contents);

            String aPath = context.getProperty(PROP_XPATH).getValue();
            String parts[] = aPath.split("/");

            int size = parts.length;
            String lastNode = parts[size - 1]; //FunctionalPhysicalObject"

            for (XML node : xml.nodes("root/" + lastNode)) {

                //System.out.println(node.toString());

                FlowFile flowfileOut = session.create();

                InputStream data = new ByteArrayInputStream(node.toString().getBytes());
                flowfileOut = session.importFrom(data, flowfileOut);

                session.transfer(flowfileOut, MY_RELATIONSHIP);
                session.commit();

            }

        } catch (IOException e) {
            System.out.println("w00t");// + e.getMessage());
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        } catch (FlowFileHandlingException e) {
            System.out.println(e.getMessage());
        }

        //---------------

        //            for (XML ibr : xml.nodes("//fout")) {
        //
        //              System.out.println(ibr.toString());
        //            }

        //            List<XML> nodes = xml.nodes(aPath);
        //
        //            if(nodes.size() == 0){
        //              System.out.println("geen nodes gevonden!!");
        //            }else{
        //
        //
        //              System.out.println("more nodes!!!");
        //
        ////              ListSaver ls = new ListSaver();
        ////
        //              StringBuffer buffer = new StringBuffer();
        //
        //              int counter = 0;
        //              for(;counter<nodes.size();counter++){
        //                XML node = nodes.get(counter);
        ////                ls.add(""+counter, node);
        //                buffer.append(node.node().toString());
        //             }
        //
        //              InputStream isOut = new ByteArrayInputStream(buffer.toString().getBytes());
        //
        //              xpath_nodes.set(isOut);
        //            }

        //      savedNodes.get().
        //      //System.out.println(ibr.toString());
        //      //remove flowFileIn from session otherwise fail to transfer new flowfile
        //      session.remove(flowFileIn);
        //
        //      FlowFile flowfileOut = session.create();
        //////
        //      InputStream data = new ByteArrayInputStream(ibr.toString().getBytes());
        //      flowfileOut = session.importFrom(data, flowfileOut);
        //      session.transfer(flowfileOut, MY_RELATIONSHIP);
        //session.transfer(flowFileIn, MY_RELATIONSHIP);

        //      System.out.println("going to make individual nodes");
        //
        //      //create flow files from every node in xpath_nodes

        //System.out.println(xpath_nodes.toString());
        //
        //      for(XML aNodeXML : nodes.xmlList){
        ////
        //       System.out.println(aNodeXML.toString());
        ////
        ////        //XMLDocument doc = new XMLDocument(aNodeXML.node());
        ////
        ////        //System.out.println(doc.node());
        ////
        ////        //FlowFile flowfileOut = session.create();
        ////
        ////        //InputStream data = new ByteArrayInputStream(doc.node().toString().getBytes());
        ////        //flowfileOut = session.importFrom(data, flowfileOut);
        ////
        ////        //session.transfer(flowfileOut, MY_RELATIONSHIP);
        ////
        //      }

        //session.transfer(flowFileIn, MY_RELATIONSHIP);

    }
}