All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.response.GraphMLResponseWriter Maven / Gradle / Ivy

There is a newer version: 9.7.0
Show newest version
/*
 * 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 org.apache.solr.response;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.client.solrj.io.Tuple;
import org.apache.solr.client.solrj.io.graph.Traversal;
import org.apache.solr.client.solrj.io.stream.TupleStream;
import org.apache.solr.handler.GraphHandler;
import org.apache.solr.request.SolrQueryRequest;

public class GraphMLResponseWriter implements QueryResponseWriter {

  @Override
  public String getContentType(SolrQueryRequest req, SolrQueryResponse res) {
    return "application/xml";
  }

  @Override
  public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse res) throws IOException {

    Exception e1 = res.getException();
    if (e1 != null) {
      e1.printStackTrace(new PrintWriter(writer));
      return;
    }

    TupleStream stream = (TupleStream) req.getContext().get("stream");

    if (stream instanceof GraphHandler.DummyErrorStream) {
      GraphHandler.DummyErrorStream d = (GraphHandler.DummyErrorStream) stream;
      Exception e = d.getException();
      e.printStackTrace(new PrintWriter(writer));
      return;
    }

    Traversal traversal = (Traversal) req.getContext().get("traversal");
    PrintWriter printWriter = new PrintWriter(writer);

    try {

      stream.open();

      Tuple tuple = null;

      int edgeCount = 0;

      printWriter.println("");
      printWriter.println("");

      printWriter.println("");

      while (true) {
        // Output the graph
        tuple = stream.read();
        if (tuple.EOF) {
          break;
        }

        String id = tuple.getString("node");

        if (traversal.isMultiCollection()) {
          id = tuple.getString("collection") + "." + id;
        }

        printWriter.write(" outfields = new ArrayList<>();
        for (String key : tuple.getFields().keySet()) {
          if (key.equals("node") || key.equals("ancestors") || key.equals("collection")) {
            continue;
          } else {
            outfields.add(key);
          }
        }

        if (outfields.size() > 0) {
          printWriter.println(">");
          for (String nodeAttribute : outfields) {
            Object o = tuple.get(nodeAttribute);
            if (o != null) {
              printWriter.println(
                  ""
                      + xmlEscape(o.toString())
                      + "");
            }
          }
          printWriter.println("");
        } else {
          printWriter.println("/>");
        }

        List ancestors = tuple.getStrings("ancestors");

        if (ancestors != null) {
          for (String ancestor : ancestors) {
            ++edgeCount;
            printWriter.write("");
          }
        }
      }

      printWriter.write("");
    } finally {
      stream.close();
    }
  }

  private String xmlEscape(String s) {
    if (s.contains(">")) {
      s = s.replace(">", ">");
    }

    if (s.contains("<")) {
      s = s.replace("<", "<");
    }

    if (s.contains("\"")) {
      s = s.replace("\"", """);
    }

    if (s.contains("'")) {
      s = s.replace("'", "'");
    }

    if (s.contains("&")) {
      s = s.replace("&", "&");
    }

    return s;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy