コード例 #1
0
  private JSONObject convertXMLToJSON(String xmlData) throws JSONException, JAXBException {
    // Unmarshall the xml into a GraphML
    ByteArrayInputStream baiStream = new ByteArrayInputStream(xmlData.getBytes());
    JAXBContext jc = JAXBContext.newInstance(GraphMLUtil.GRAPHML_CLASSES);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    GraphML graphML = (GraphML) unmarshaller.unmarshal(baiStream);

    // eventually check version for compatibility
    s_logger.info("Importing chart version: {}", graphML.getversion());

    Graph graph = graphML.getGraph();

    JSONObject toReturn = new JSONObject(),
        miscData = null,
        columnJSON,
        nodeJSON = null,
        clusterJSON;
    String cachedClusterJSON = null;
    int columnIdx = -1, rowIdx = -1;
    List<List<JSONObject>> allColumns = new ArrayList<List<JSONObject>>();
    List<JSONObject> currColumn, outColumns = new ArrayList<JSONObject>();

    // first, set up the column and misc JSON data contained in the graph
    for (GraphDataXML data : graph.getdata()) {
      if (data.getkey().startsWith("column")) {
        columnJSON = XML.toJSONObject(data.getvalue());
        columnIdx = Integer.parseInt(data.getkey().substring(6));
        while (columnIdx >= outColumns.size()) {
          outColumns.add(new JSONObject());
        }

        outColumns.set(columnIdx, columnJSON);
        allColumns.add(new ArrayList<JSONObject>());
      } else if (data.getkey().equals("miscData")) {
        miscData = XML.toJSONObject(data.getvalue());
      }
    }

    // next, iterate through the graph nodes and place the JSONObject for each into it's proper
    // row/column
    for (GraphNode node : graph.getnode()) {
      for (GraphData data : node.getdata()) { // parse the goodies from each file node
        if (data.getkey().equals("column")) {
          columnIdx = Integer.parseInt(data.getvalue());
        } else if (data.getkey().equals("row")) {
          rowIdx = Integer.parseInt(data.getvalue());
        }
      }

      for (GraphDataXML data : node.getnodedata()) { // parse the goodies from each data node
        if (data.getkey().equals("fileJSON")) {
          nodeJSON = XML.toJSONObject(data.getvalue());
        } else if (data.getkey().equals("clusterJSON")) {
          cachedClusterJSON = data.getvalue();
        }
      }

      if (nodeJSON != null
          && nodeJSON.has("clusterUIObject")
          && !nodeJSON.get("clusterUIObject").toString().equals("null")) {
        clusterJSON = nodeJSON.getJSONObject("clusterUIObject"); // do annoying cleanup
        insertJSONArray(clusterJSON, "children");
        insertJSONArray(clusterJSON.getJSONObject("spec"), "members");
      }

      if (cachedClusterJSON != null) {
        PermitSet permits = new PermitSet();

        try {
          List<String> entityIds = new LinkedList<String>();
          List<FL_Cluster> allClusters = ClusterHelper.listFromJson(cachedClusterJSON);
          ;
          for (FL_Cluster cluster : allClusters) {
            entityIds.addAll(cluster.getMembers());
            for (FL_Property property : cluster.getProperties()) {
              if (!(property.getRange() instanceof FL_SingletonRange)) continue;
              FL_SingletonRange range = (FL_SingletonRange) property.getRange();
              if (!range.getType().equals(FL_PropertyType.STRING)) continue;
              property.setRange(
                  new SingletonRangeHelper(
                      StringEscapeUtils.unescapeXml((String) range.getValue())));
            }
          }

          final ContextReadWrite contextRW =
              contextCache.getReadWrite(nodeJSON.getString("xfId"), permits);
          contextRW.getContext().addClusters(allClusters);
          contextRW
              .getContext()
              .addEntities(entityAccess.getEntities(entityIds, FL_LevelOfDetail.SUMMARY));
          contextRW.setSimplifiedContext(allClusters);
        } catch (IOException e) {
          throw new ResourceException(
              Status.CLIENT_ERROR_BAD_REQUEST, "Exception during cluster cache processing.", e);
        } finally {
          permits.revoke();
        }
      }

      currColumn = allColumns.get(columnIdx);
      while (rowIdx >= currColumn.size()) {
        currColumn.add(new JSONObject());
      }
      currColumn.set(rowIdx, nodeJSON);
    }

    // place the files as children in the outgoing columns array
    for (int i = 0; i < allColumns.size(); i++) {
      columnJSON = outColumns.get(i);
      columnJSON.put("children", new JSONArray(allColumns.get(i)));
    }

    // finally, place the child columns and misc data in the resulting JSON object
    toReturn.put("children", new JSONArray(outColumns));
    for (String dataKey : JSONObject.getNames(miscData)) {
      toReturn.put(dataKey, miscData.get(dataKey));
    }

    return toReturn;
  }