Пример #1
0
  Pair<Stroke, Continuation<Stroke>> parseStroke(XMLStreamReader in) throws XMLStreamException {
    in.require(START_ELEMENT, null, "Stroke");

    Stroke base = new Stroke();
    Continuation<Stroke> contn = null;

    while (!(in.isEndElement() && in.getLocalName().equals("Stroke"))) {
      in.nextTag();

      if (in.getLocalName().endsWith("Parameter")) {
        contn = parseParameter(contn, in, base);
      } else if (in.getLocalName().equals("GraphicFill")) {
        contn = parseGraphicFill(contn, in, base);
      } else if (in.getLocalName().equals("GraphicStroke")) {
        contn = parseGraphicStroke(contn, in, base);
      } else if (in.isStartElement()) {
        LOG.error("Found unknown element '{}', skipping.", in.getLocalName());
        skipElement(in);
      }
    }

    in.require(END_ELEMENT, null, "Stroke");

    return new Pair<Stroke, Continuation<Stroke>>(base, contn);
  }
Пример #2
0
 /**
  * Forwards the given {@link XMLStreamReader} to the specified element or to the end of the
  * enclosing element/document if there is no such element.
  *
  * @param reader reader to forward, must not be <code>null</code>
  * @param elementName element to forward to, must not be <code>null</code>
  * @throws XMLStreamException
  */
 public static boolean skipToElementOnSameLevel(XMLStreamReader reader, QName elementName)
     throws XMLStreamException {
   while (reader.isStartElement() && !elementName.equals(reader.getName())) {
     skipElement(reader);
     nextElement(reader);
   }
   return reader.isStartElement() && elementName.equals(reader.getName());
 }
Пример #3
0
  private Continuation<Stroke> parseGraphicStroke(
      Continuation<Stroke> contn, XMLStreamReader in, Stroke base) throws XMLStreamException {
    while (!(in.isEndElement() && in.getLocalName().equals("GraphicStroke"))) {
      in.nextTag();

      if (in.getLocalName().equals("Graphic")) {
        final Pair<Graphic, Continuation<Graphic>> pair = context.graphicParser.parseGraphic(in);

        if (pair != null) {
          base.stroke = pair.first;
          if (pair.second != null) {
            contn =
                new Continuation<Stroke>(contn) {
                  @Override
                  public void updateStep(
                      Stroke base, Feature f, XPathEvaluator<Feature> evaluator) {
                    pair.second.evaluate(base.stroke, f, evaluator);
                  }
                };
          }
        }

        in.require(END_ELEMENT, null, "Graphic");
      } else if (in.getLocalName().equals("InitialGap")) {
        contn =
            context.parser.updateOrContinue(
                    in,
                    "InitialGap",
                    base,
                    new Updater<Stroke>() {
                      @Override
                      public void update(Stroke obj, String val) {
                        obj.strokeInitialGap = Double.parseDouble(val);
                      }
                    },
                    contn)
                .second;
        in.require(END_ELEMENT, null, "InitialGap");
      } else if (in.getLocalName().equals("Gap")) {
        contn =
            context.parser.updateOrContinue(
                    in,
                    "Gap",
                    base,
                    new Updater<Stroke>() {
                      @Override
                      public void update(Stroke obj, String val) {
                        obj.strokeGap = Double.parseDouble(val);
                      }
                    },
                    contn)
                .second;
        in.require(END_ELEMENT, null, "Gap");
      } else if (in.getLocalName().equals("PositionPercentage")) {
        contn =
            context.parser.updateOrContinue(
                    in,
                    "PositionPercentage",
                    base,
                    new Updater<Stroke>() {
                      @Override
                      public void update(Stroke obj, String val) {
                        obj.positionPercentage = Double.parseDouble(val);
                      }
                    },
                    contn)
                .second;
        in.require(END_ELEMENT, null, "PositionPercentage");
      } else if (in.isStartElement()) {
        Location loc = in.getLocation();
        LOG.error(
            "Found unknown element '{}' at line {}, column {}, skipping.",
            new Object[] {in.getLocalName(), loc.getLineNumber(), loc.getColumnNumber()});
        skipElement(in);
      }
    }
    return contn;
  }
Пример #4
0
  private Continuation<Stroke> parseParameter(
      Continuation<Stroke> contn, XMLStreamReader in, Stroke base) throws XMLStreamException {
    String name = in.getAttributeValue(null, "name");

    if (name.equals("stroke")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      // keep alpha value
                      int alpha = obj.color.getAlpha();
                      obj.color = decodeWithAlpha(val);
                      obj.color =
                          new Color(
                              obj.color.getRed(), obj.color.getGreen(), obj.color.getBlue(), alpha);
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-opacity")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      // keep original color
                      float alpha = Float.parseFloat(val);
                      float[] cols = obj.color.getRGBColorComponents(null);
                      obj.color = new Color(cols[0], cols[1], cols[2], alpha);
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-width")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      obj.width = Double.parseDouble(val);
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-linejoin")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      try {
                        obj.linejoin = LineJoin.valueOf(val.toUpperCase());
                      } catch (IllegalArgumentException e) {
                        LOG.warn("Used invalid value '{}' for line join.", val);
                        obj.linejoin = ROUND;
                      }
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-linecap")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      try {
                        obj.linecap = LineCap.valueOf(val.toUpperCase());
                      } catch (IllegalArgumentException e) {
                        LOG.warn("Used invalid value '{}' for line cap.", val);
                        obj.linecap = BUTT;
                      }
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-dasharray")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      // , is not strictly allowed, but we don't lose anything by being flexible
                      if (val.contains(",")) {
                        obj.dasharray = splitAsDoubles(val, ",");
                      } else {
                        obj.dasharray = splitAsDoubles(val, "\\s");
                      }
                    }
                  },
                  contn)
              .second;
    } else if (name.equals("stroke-dashoffset")) {
      contn =
          context.parser.updateOrContinue(
                  in,
                  "Parameter",
                  base,
                  new Updater<Stroke>() {
                    @Override
                    public void update(Stroke obj, String val) {
                      obj.dashoffset = Double.parseDouble(val);
                    }
                  },
                  contn)
              .second;
    } else {
      Location loc = in.getLocation();
      LOG.error(
          "Found unknown parameter '{}' at line {}, column {}, skipping.",
          new Object[] {name, loc.getLineNumber(), loc.getColumnNumber()});
      skipElement(in);
    }

    in.require(END_ELEMENT, null, null);
    return contn;
  }