static void filterMarkerInAttribute(List<Element> eltList, Map<String, Object> parameterMap) {

    final Pattern patternMarker = Pattern.compile("\\$\\{([^}]+)\\}"); // matching pattern is ${..}

    for (Element elt : eltList) {
      for (Attribute attribute : elt.getAttributes()) {
        String attributeValue = attribute.getValue();

        if (attributeValue == null) {
          continue;
        }

        Matcher matcher = patternMarker.matcher(attributeValue);
        while (matcher.find()) {
          String marker = matcher.group(0); // full pattern matched ${..}
          String markerName = matcher.group(1); // get only text between curly braces
          String parameterValue = ServletUtil.getFirstParameter(parameterMap.get(markerName));
          if (parameterValue != null) {
            attributeValue = attributeValue.replace(marker, parameterValue);
            attribute.setValue(attributeValue);
          } else {
            logger.warn(
                "Found marker \""
                    + marker
                    + "\" with NO matching parameter in Element <"
                    + elt.getName()
                    + "> "
                    + attribute);
          }
        }
      }
    }
  }
Exemple #2
0
  static void parseXml(Element p_element, MessageDigest p_md, ArrayList<String> p_ignoreList) {
    p_md.update(p_element.getName().getBytes());

    String content = p_element.getText();

    if (content != null) {
      p_md.update(content.getBytes());
    }

    for (Attribute attribute : p_element.getAttributes()) {
      String name = attribute.getName();

      if (p_ignoreList.contains(name)) {
        continue;
      }

      String value = attribute.getValue();

      if (value != null) {
        p_md.update(value.getBytes());
      }
    }

    for (Element child : p_element.getChildren()) {
      parseXml(child, p_md, p_ignoreList);
    }
  }
Exemple #3
0
 private void checkID(final JElement je) {
   final org.jdom2.Element emt = (org.jdom2.Element) (je.shadow);
   if (emt.hasAttributes()) {
     for (final Attribute a : emt.getAttributes()) {
       if (a.getAttributeType() == AttributeType.ID) {
         if (idmap.put(a.getValue(), je) != null) {
           throw new DOMException(
               DOMException.INVALID_STATE_ERR, "Multiple elements with id " + a.getValue());
         }
       }
     }
   }
 }
  /**
   * This will invoke the <code>startElement</code> callback in the <code>ContentHandler</code>.
   *
   * @param element <code>Element</code> used in callbacks.
   * @param nsAtts <code>List</code> of namespaces to declare with the element or <code>null</code>.
   */
  private void startElement(Element element, Attributes nsAtts) throws JDOMException {
    String namespaceURI = element.getNamespaceURI();
    String localName = element.getName();
    String rawName = element.getQualifiedName();

    // Allocate attribute list.
    AttributesImpl atts = (nsAtts != null) ? new AttributesImpl(nsAtts) : new AttributesImpl();

    for (Attribute a : element.getAttributes()) {
      atts.addAttribute(
          a.getNamespaceURI(),
          a.getName(),
          a.getQualifiedName(),
          getAttributeTypeName(a.getAttributeType()),
          a.getValue());
    }

    try {
      contentHandler.startElement(namespaceURI, localName, rawName, atts);
    } catch (SAXException se) {
      throw new JDOMException("Exception in startElement", se);
    }
  }
Exemple #5
0
  void metaVersusData(Element p_element) {
    m_metaSize += p_element.getName().length();

    String content = p_element.getText();

    if (content != null) m_dataSize += content.length();

    for (Attribute attribute : p_element.getAttributes()) {
      String name = attribute.getName();

      m_metaSize += name.length();

      String value = attribute.getValue();

      if (value != null) {
        m_dataSize += value.length();
      }
    }

    for (Element child : p_element.getChildren()) {
      metaVersusData(child);
    }
  }
Exemple #6
0
  Portal loadPortal(Element elem) {
    String sysName = null;
    String userName = elem.getAttribute("portalName").getValue();
    if (elem.getAttribute("systemName") == null) {
      if (log.isDebugEnabled()) {
        log.debug("Portal systemName is null");
      }
    } else {
      sysName = elem.getAttribute("systemName").getValue();
    }
    String fromBlockName = null;
    String toBlockName = null;
    // Portals must have user names.
    Portal portal = _portalMgr.getByUserName(userName);
    if (portal != null) {
      fromBlockName = portal.getFromBlock().getSystemName();
      toBlockName = portal.getToBlock().getSystemName();
    } else {
      portal = _portalMgr.providePortal(userName);
    }
    if (portal == null) {
      log.error(
          "unable to create Portal ("
              + sysName
              + ", "
              + userName
              + ") "
              + elem
              + " "
              + elem.getAttributes());
      return null;
    }
    if (log.isDebugEnabled()) {
      log.debug("create Portal: (" + sysName + ", " + userName + ")");
    }

    OBlock fromBlock = null;
    Element eFromBlk = elem.getChild("fromBlock");
    if (eFromBlk != null && eFromBlk.getAttribute("blockName") != null) {
      String name = eFromBlk.getAttribute("blockName").getValue();
      if (fromBlockName != null && !fromBlockName.equals(name)) {
        log.error(
            "Portal has user name \"" + userName + "\" conflicting with " + portal.toString());
      } else {
        fromBlock = getBlock(name);
        if (fromBlock != null) {
          portal.setFromBlock(fromBlock, false);
          fromBlock.addPortal(portal);

          List<Element> ePathsFromBlock = eFromBlk.getChildren("path");
          for (int i = 0; i < ePathsFromBlock.size(); i++) {
            Element e = ePathsFromBlock.get(i);
            String pathName = e.getAttribute("pathName").getValue();
            String blockName = e.getAttribute("blockName").getValue();
            if (log.isDebugEnabled()) {
              log.debug(
                  "Load portal= "
                      + userName
                      + " fromBlock= "
                      + fromBlock.getSystemName()
                      + " pathName= "
                      + pathName
                      + " blockName= "
                      + blockName);
            }
            /*(if (fromBlock.getSystemName().equals(blockName))*/ {
              // path is in the fromBlock
              OPath path = getPath(fromBlock, pathName);
              portal.addPath(path);
            }
          }
        }
      }
    } else {
      log.error("Portal \"" + userName + "\" has no fromBlock!");
    }

    OBlock toBlock = null;
    Element eToBlk = elem.getChild("toBlock");
    if (eToBlk != null && eToBlk.getAttribute("blockName") != null) {
      String name = eToBlk.getAttribute("blockName").getValue();
      if (toBlockName != null && !toBlockName.equals(name)) {
        log.error(
            "Portal has user name \"" + userName + "\" conflicting with " + portal.toString());
      } else {
        toBlock = getBlock(name);
        if (toBlock != null) {
          portal.setToBlock(toBlock, false);
          toBlock.addPortal(portal);

          List<Element> ePathsToBlock = eToBlk.getChildren("path");
          for (int i = 0; i < ePathsToBlock.size(); i++) {
            Element e = ePathsToBlock.get(i);
            String pathName = e.getAttribute("pathName").getValue();
            String blockName = e.getAttribute("blockName").getValue();
            if (log.isDebugEnabled()) {
              log.debug(
                  "Load portal= "
                      + userName
                      + " toBlock= "
                      + toBlock.getSystemName()
                      + " pathName= "
                      + pathName
                      + " blockName= "
                      + blockName);
            }
            /*if (toBlock.getSystemName().equals(blockName))*/ {
              // path is in the toBlock
              OPath path = getPath(toBlock, pathName);
              portal.addPath(path);
            }
          }
        }
      }
    } else {
      log.error("Portal \"" + userName + "\" has no toBlock!");
    }
    Element eSignal = elem.getChild("fromSignal");
    if (eSignal != null) {
      String name = eSignal.getAttribute("signalName").getValue();
      float length = 0.0f;
      try {
        Attribute attr = eSignal.getAttribute("signalDelay");
        if (attr != null) {
          length = attr.getFloatValue();
        }
      } catch (org.jdom2.DataConversionException e) {
        log.error(
            "Could not parse signalDelay for signal (" + name + ") in portal (" + userName + ")");
      }
      portal.setProtectSignal(Portal.getSignal(name), length, toBlock);
    }
    eSignal = elem.getChild("toSignal");
    if (eSignal != null) {
      String name = eSignal.getAttribute("signalName").getValue();
      float length = 0.0f;
      try {
        Attribute attr = eSignal.getAttribute("signalDelay");
        if (attr != null) {
          length = attr.getFloatValue();
        }
      } catch (org.jdom2.DataConversionException e) {
        log.error(
            "Could not parse signalDelay for signal (" + name + ") in portal (" + userName + ")");
      }
      portal.setProtectSignal(Portal.getSignal(name), length, fromBlock);
    }

    if (log.isDebugEnabled()) {
      log.debug("End Load portal " + userName);
    }
    return portal;
  }
Exemple #7
0
  void loadBlock(Element elem) {
    if (elem.getAttribute("systemName") == null) {
      log.error("unexpected null in systemName " + elem + " " + elem.getAttributes());
      return;
    }
    String sysName = elem.getAttribute("systemName").getValue();
    String userName = null;
    if (elem.getAttribute("userName") != null) {
      userName = elem.getAttribute("userName").getValue();
    }
    if (log.isDebugEnabled()) {
      log.debug("Load block sysName= " + sysName + " userName= "******"Null block!! sysName= " + sysName + ", userName= "******"comment");
    if (c != null) {
      block.setComment(c);
    }
    if (elem.getAttribute("units") != null) {
      block.setMetricUnits(elem.getAttribute("units").getValue().equals("true"));
    } else {
      block.setMetricUnits(false);
    }
    if (elem.getAttribute("length") != null) {
      block.setLength(Float.valueOf(elem.getAttribute("length").getValue()).floatValue());
    }
    if (elem.getAttribute("curve") != null) {
      block.setCurvature(Integer.parseInt((elem.getAttribute("curve")).getValue()));
    }
    List<Element> sensors = elem.getChildren("sensor");
    if (sensors.size() > 1) {
      log.error("More than one sensor present: " + sensors.size());
    }
    if (sensors.size() > 0) {
      // sensor
      String name = sensors.get(0).getAttribute("systemName").getValue();
      block.setSensor(name);
    }
    Element errSensor = elem.getChild("errorSensor");
    if (errSensor != null) {
      // sensor
      String name = errSensor.getAttribute("systemName").getValue();
      // Sensor sensor = InstanceManager.sensorManagerInstance().provideSensor(name);
      block.setErrorSensor(name);
    }
    Element reporter = elem.getChild("reporter");
    if (reporter != null) {
      // sensor
      String name = reporter.getAttribute("systemName").getValue();
      // Sensor sensor = InstanceManager.sensorManagerInstance().provideSensor(name);
      try {
        Reporter rep = InstanceManager.reporterManagerInstance().getReporter(name);
        if (rep != null) {
          block.setReporter(rep);
        }
      } catch (Exception ex) {
        log.error("No Reporter named \"" + name + "\" found. threw exception: " + ex);
      }
      if (reporter.getAttribute("reportCurrent") != null) {
        block.setReportingCurrent(reporter.getAttribute("reportCurrent").getValue().equals("true"));
      } else {
        block.setReportingCurrent(false);
      }
    }
    if (elem.getAttribute("permissive") != null) {
      block.setPermissiveWorking(elem.getAttribute("permissive").getValue().equals("true"));
    } else {
      block.setPermissiveWorking(false);
    }
    if (elem.getAttribute("speedNotch") != null) {
      try {
        block.setBlockSpeed(elem.getAttribute("speedNotch").getValue());
      } catch (jmri.JmriException ex) {
        JOptionPane.showMessageDialog(
            null, ex.getMessage() + "\n" + elem.getAttribute("speedNotch").getValue());
      }
    }

    List<Element> portals = elem.getChildren("portal");
    for (int k = 0; k < portals.size(); k++) {
      block.addPortal(loadPortal(portals.get(k)));
    }

    List<Element> paths = elem.getChildren("path");
    for (int j = 0; j < paths.size(); j++) {
      if (!block.addPath(loadPath(paths.get(j), block))) {
        log.error(
            "load: block \""
                + sysName
                + "\" failed to add path \""
                + paths.get(j).getName()
                + "\" in block \""
                + block.getSystemName()
                + "\"");
      }
    }
  }
  @Override
  public boolean load(Element shared, Element perNode) {

    WarrantManager manager = InstanceManager.getDefault(WarrantManager.class);

    // don't continue on to build NXFrame if no content
    if (shared.getChildren().size() == 0) return true;

    if (!GraphicsEnvironment.isHeadless()) {
      NXFrame nxFrame = NXFrame.getInstance();
      loadNXParams(nxFrame, shared.getChild("nxparams"));
      //            nxFrame.init();   don't make visible
    }
    List<Element> warrantList = shared.getChildren("warrant");
    if (log.isDebugEnabled()) log.debug("Found " + warrantList.size() + " Warrant objects");
    for (int i = 0; i < warrantList.size(); i++) {
      Element elem = warrantList.get(i);
      if (elem.getAttribute("systemName") == null) {
        log.warn("unexpected null in systemName " + elem + " " + elem.getAttributes());
        break;
      }
      String sysName = null;
      if (elem.getAttribute("systemName") != null)
        sysName = elem.getAttribute("systemName").getValue();

      String userName = null;
      if (elem.getAttribute("userName") != null)
        userName = elem.getAttribute("userName").getValue();

      boolean SCWa = true;
      log.debug("loading warrant " + sysName);
      Attribute wType = elem.getAttribute("wtype");
      if (wType == null) {
        log.debug("wtype is null for " + sysName);
        SCWa = false;
      } else if (!wType.getValue().equals("SC")) {
        log.debug("wtype is " + wType.getValue() + " for " + sysName);
        SCWa = false;
      }

      long timeToPlatform = 500;
      Attribute TTP = elem.getAttribute("timeToPlatform");
      if (TTP != null) {
        try {
          timeToPlatform = TTP.getLongValue();
        } catch (DataConversionException e) {
          log.debug(
              "ignoring DataConversionException (and reverting to default value): " + e.toString());
        }
      }

      Warrant warrant = manager.createNewWarrant(sysName, userName, SCWa, timeToPlatform);
      if (warrant == null) {
        log.info(
            "Warrant \""
                + sysName
                + "("
                + userName
                + ")\" previously loaded. This version not loaded.");
        continue;
      }
      List<Element> orders = elem.getChildren("blockOrder");
      for (int k = 0; k < orders.size(); k++) {
        BlockOrder bo = loadBlockOrder(orders.get(k));
        if (bo == null) {
          break;
        }
        warrant.addBlockOrder(bo);
      }
      String c = elem.getChildText("comment");
      if (c != null) {
        warrant.setComment(c);
      }

      Element order = elem.getChild("viaOrder");
      if (order != null) {
        warrant.setViaOrder(loadBlockOrder(order));
      }
      order = elem.getChild("avoidOrder");
      if (order != null) {
        warrant.setAvoidOrder(loadBlockOrder(order));
      }

      List<Element> throttleCmds = elem.getChildren("throttleCommand");
      for (int k = 0; k < throttleCmds.size(); k++) {
        warrant.addThrottleCommand(loadThrottleCommand(throttleCmds.get(k)));
      }
      Element train = elem.getChild("train");
      if (train != null) {
        loadTrain(train, warrant);
      }
    }
    return true;
  }