Beispiel #1
0
  // load color scale from json
  public void fromJSON(final String json) throws ColorScaleException {
    try {
      final ObjectMapper mapper = new ObjectMapper();
      final Map<String, Object> colorScaleMap = mapper.readValue(json, Map.class);
      final String scalingStr = (String) colorScaleMap.get("Scaling");
      if (scalingStr != null) {
        scaling = Scaling.valueOf(scalingStr);
      }
      final String reliefShadingStr = (String) colorScaleMap.get("ReliefShading");
      reliefShading = ("1").equals(reliefShadingStr) || ("true").equalsIgnoreCase(reliefShadingStr);

      final String interpolateStr = (String) colorScaleMap.get("Interpolate");
      interpolate =
          (interpolateStr == null)
              || interpolateStr.isEmpty()
              || ("1").equals(interpolateStr)
              || ("true").equalsIgnoreCase(interpolateStr);

      final String forceStr = (String) colorScaleMap.get("ForceValuesIntoRange");
      forceValuesIntoRange = ("1").equals(forceStr) || ("true").equalsIgnoreCase(forceStr);

      final Map<String, String> nullColorMap = (Map<String, String>) colorScaleMap.get("NullColor");
      final String nullColorStr = nullColorMap.get("color");
      if (nullColorStr != null) {
        parseColor(nullColorStr, nullColorMap.get("opacity"), nullColor);
      }

      final ArrayList<Map<String, String>> colorsList =
          (ArrayList<Map<String, String>>) colorScaleMap.get("Colors");
      if (colorsList != null) {
        for (final Map<String, String> color : colorsList) {
          final int[] colorArr = new int[4];
          final String colorStr = color.get("color");
          final String valueStr = color.get("value");
          final Double value = Double.valueOf(valueStr);
          if (colorStr != null) {
            parseColor(colorStr, color.get("opacity"), colorArr);
            put(value, colorArr);
          }
        }
      }
    } catch (final Exception e) {
      throw new BadJSONException(e);
    }
  }
Beispiel #2
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);
    }
  }