Example #1
0
  private void load(InputStream is) throws IOException {
    Document doc = XmlUtils.parseInputStream(is);
    XPath xp = XmlUtils.createXPath();

    columns.clear();

    try {
      String firstLineHeaderStr = xp.evaluate("/AllColumns/@firstLineHeader", doc);
      if (firstLineHeaderStr != null && firstLineHeaderStr.equals("true")) {
        firstLineHeader = true;
      }

      NodeList nodes = (NodeList) xp.evaluate("/AllColumns/Column", doc, XPathConstants.NODESET);
      for (int i = 0; i < nodes.getLength(); i++) {
        Element s = (Element) nodes.item(i);
        Column c = new Column();
        c.setName(s.getAttribute("name"));
        String type = s.getAttribute("type");
        if (type == null || type.isEmpty()) {
          type = FactorType.Unknown.toString();
        }
        c.setType(Column.FactorType.valueOf(type));
        String tmp = s.getAttribute("min");
        if (tmp.isEmpty() == false) {
          c.setMin(Double.valueOf(tmp));
        }
        tmp = s.getAttribute("max");
        if (tmp.isEmpty() == false) {
          c.setMax(Double.valueOf(tmp));
        }
        tmp = s.getAttribute("count");
        if (tmp.isEmpty() == false) {
          c.setCount(Long.valueOf(tmp));
        }
        tmp = s.getAttribute("sum");
        if (tmp.isEmpty() == false) {
          c.setSum(Double.valueOf(tmp));
        }
        tmp = s.getAttribute("quartile1");
        if (tmp.isEmpty() == false) {
          c.setQuartile1(Double.valueOf(tmp));
        }
        tmp = s.getAttribute("quartile2");
        if (tmp.isEmpty() == false) {
          c.setQuartile2(Double.valueOf(tmp));
        }
        tmp = s.getAttribute("quartile3");
        if (tmp.isEmpty() == false) {
          c.setQuartile3(Double.valueOf(tmp));
        }
        columns.add(c);
      }
    } catch (XPathExpressionException e) {
      throw new IOException("Error parsing XML", e);
    }
  }
Example #2
0
 public void fromXML(final InputStream strm) throws ColorScaleException {
   try {
     fromXML(XmlUtils.parseInputStream(strm));
   } catch (final Exception e) {
     throw new ColorScaleException(e);
   }
 }
Example #3
0
  public void store(OutputStream os) throws IOException {
    Document doc = XmlUtils.createDocument();

    Element root = doc.createElement("AllColumns");
    root.setAttribute("firstLineHeader", firstLineHeader ? "true" : "false");
    doc.appendChild(root);

    String pigLoad = "loaded = LOAD 'filename' USING PigStorage() AS (";
    String comma = "";

    for (Column c : columns) {
      pigLoad += comma + c.getName();
      comma = ", ";
      Element node = doc.createElement("Column");
      root.appendChild(node);
      node.setAttribute("name", c.getName());
      node.setAttribute("type", c.getType().toString());
      if (c.getCount() > 0) {
        node.setAttribute("count", Long.toString(c.getCount()));
      }
      if (c.getType() == FactorType.Numeric) {
        if (c.getSum() > 0.0) {
          node.setAttribute("sum", Double.toString(c.getSum()));
        }
        if (c.getMin() <= c.getMax()) {
          node.setAttribute("min", Double.toString(c.getMin()));
          node.setAttribute("max", Double.toString(c.getMax()));
        }
        if (c.isQuartile1Valid()) {
          node.setAttribute("quartile1", Double.toString(c.getQuartile1()));
        }
        if (c.isQuartile2Valid()) {
          node.setAttribute("quartile2", Double.toString(c.getQuartile2()));
        }
        if (c.isQuartile3Valid()) {
          node.setAttribute("quartile3", Double.toString(c.getQuartile3()));
        }
      }
    }

    pigLoad += ");";

    Comment c = doc.createComment(pigLoad);
    root.appendChild(c);

    XmlUtils.writeDocument(doc, new OutputStreamWriter(os));
  }
Example #4
0
 /**
  * Sets a nodes child to have a given value. If the child doesn't exist it is created.
  *
  * @param parent
  * @param childName
  * @param childValue
  * @throws XPathExpressionException
  */
 public static void setChild(Node parent, String childName, String childValue)
     throws XPathExpressionException {
   Document doc = parent.getOwnerDocument();
   XPath xpath = XmlUtils.createXPath();
   Node childNode = (Node) xpath.evaluate(childName, parent, XPathConstants.NODE);
   if (childNode == null) {
     childNode = doc.createElement(childName);
     parent.appendChild(childNode);
   }
   childNode.setTextContent(childValue);
 }
Example #5
0
 /**
  * Sets the altitude of all Polygons
  *
  * @throws XPathExpressionException
  */
 public static void setAltitude(Document doc, double altitude, String altitudeMode)
     throws XPathExpressionException {
   XPath xpath = XmlUtils.createXPath();
   // only polygons are supported at this time
   XPathExpression expr = xpath.compile("/kml/*/Polygon");
   NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
   for (int i = 0; i < nodes.getLength(); i++) {
     Node polygonNode = nodes.item(i);
     setChild(polygonNode, "altitude", new Double(altitude).toString());
     if (altitudeMode != null) {
       setChild(polygonNode, "altitudeMode", altitudeMode);
     }
   }
   throw new XPathExpressionException("I haven't been tested!");
 }
Example #6
0
  public void fromXML(final Document doc) throws ColorScaleException {
    try {
      clear();

      final XPath xpath = XmlUtils.createXPath();

      name = xpath.evaluate("/ColorMap/@name", doc);

      final Node nodeTitle = (Node) xpath.evaluate("/ColorMap/Title", doc, XPathConstants.NODE);
      if (nodeTitle != null) {
        title = xpath.evaluate("text()", nodeTitle);
      }

      final Node nodeDesc =
          (Node) xpath.evaluate("/ColorMap/Description", doc, XPathConstants.NODE);
      if (nodeDesc != null) {
        description = xpath.evaluate("text()", nodeDesc);
      }

      scaling = Scaling.valueOf(xpath.evaluate("/ColorMap/Scaling/text()", doc));
      final String reliefShadingStr =
          xpath.evaluate("/ColorMap/ReliefShading/text()", doc).toLowerCase();
      reliefShading = reliefShadingStr.equals("1") || reliefShadingStr.equals("true");

      final String interpolateStr =
          xpath.evaluate("/ColorMap/Interpolate/text()", doc).toLowerCase();
      interpolate =
          interpolateStr.isEmpty() || (interpolateStr.equals("1") || interpolateStr.equals("true"));

      final String forceStr =
          xpath.evaluate("/ColorMap/ForceValuesIntoRange/text()", doc).toLowerCase();
      forceValuesIntoRange = (forceStr.equals("1") || forceStr.equals("true"));

      final Node nullColorNode =
          (Node) xpath.evaluate("/ColorMap/NullColor", doc, XPathConstants.NODE);
      if (nullColorNode != null) {
        final String colorStr = xpath.evaluate("@color", nullColorNode);
        final String opacityStr = xpath.evaluate("@opacity", nullColorNode);
        parseColor(colorStr, opacityStr, nullColor);
      }

      final int[] color = new int[4];
      final XPathExpression expr = xpath.compile("/ColorMap/Color");
      final NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
      for (int i = 0; i < nodes.getLength(); i++) {
        final Node node = nodes.item(i);

        final String valueStr = xpath.evaluate("@value", node);
        final String colorStr = xpath.evaluate("@color", node);
        final String opacityStr = xpath.evaluate("@opacity", node);

        if (valueStr.isEmpty()) {
          throw new IOException(
              "Error parsing XML: A value must be specified for a color element.");
        }

        final double value = Double.valueOf(valueStr);
        parseColor(colorStr, opacityStr, color);
        put(value, color);
      }
      cache = null;
    } catch (final Exception e) {
      throw new BadXMLException(e);
    }
  }