예제 #1
0
  public static Collection<Node> getChildNodes(Node node) {
    Collection<Node> list = new LinkedList<Node>();

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
      if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) list.add(nl.item(i));

    return list;
  }
예제 #2
0
  public static Collection<Node> getNodesByName(Node node, String name) {
    Collection<Node> list = new LinkedList<Node>();

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
      if (nl.item(i).getNodeName().equals(name)) list.add(nl.item(i));

    return list;
  }
 private static Collection<Node> getNodeMatching(Node body, String regexp) {
   final Collection<Node> nodes = new ArrayList<>();
   if (body.getNodeName().matches(regexp)) nodes.add(body);
   if (body.getChildNodes().getLength() == 0) return nodes;
   NodeList returnList = body.getChildNodes();
   for (int k = 0; k < returnList.getLength(); k++) {
     final Node node = returnList.item(k);
     nodes.addAll(getNodeMatching(node, regexp));
   }
   return nodes;
 }
  // refactored this out from the three addAction methods above
  private void addAction(Object actionsCollectionOrMap, ActionDescriptor descriptor) {
    if (getAction(descriptor.getId()) != null) {
      throw new IllegalArgumentException(
          "action with id " + descriptor.getId() + " already exists for this step.");
    }

    if (actionsCollectionOrMap instanceof Map) {
      ((Map) actionsCollectionOrMap).put(new Integer(descriptor.getId()), descriptor);
    } else {
      ((Collection) actionsCollectionOrMap).add(descriptor);
    }
  }
예제 #5
0
 JdbcIndexDefinition(
     DataSource ds,
     Element element,
     Set allIndexedFields,
     boolean storeText,
     boolean mergeText,
     Analyzer a,
     boolean isSub) {
   this.dataSource = ds;
   indexSql = element.getAttribute("sql");
   key = element.getAttribute("key");
   String elementId = element.getAttribute("identifier");
   identifier = "".equals(elementId) ? key : elementId;
   findSql = element.getAttribute("find");
   NodeList childNodes = element.getChildNodes();
   for (int k = 0; k < childNodes.getLength(); k++) {
     if (childNodes.item(k) instanceof Element) {
       Element childElement = (Element) childNodes.item(k);
       if ("field".equals(childElement.getLocalName())) {
         if (childElement.getAttribute("keyword").equals("true")) {
           keyWords.add(childElement.getAttribute("name"));
         }
         String m = childElement.getAttribute("multiple");
         if ("".equals(m)) m = "add";
         if (!m.equals("add")) {
           nonDefaultMultiples.put(
               childElement.getAttribute("name"), Indexer.Multiple.valueOf(m.toUpperCase()));
         }
         String b = childElement.getAttribute("boost");
         if (!b.equals("")) {
           boosts.put(childElement.getAttribute("name"), Float.valueOf(b));
         }
       } else if ("related".equals(childElement.getLocalName())) {
         subQueries.add(
             new JdbcIndexDefinition(
                 ds, childElement, allIndexedFields, storeText, mergeText, a, true));
       }
     }
   }
   this.analyzer = a;
   this.isSub = isSub;
   assert !isSub || "".equals(findSql);
 }
 /**
  * Discover WS device on the local network with specified filter
  *
  * @param regexpProtocol url protocol matching regexp like "^http$", might be empty ""
  * @param regexpPath url path matching regexp like "onvif", might be empty ""
  * @return list of unique device urls filtered
  */
 public static Collection<URL> discoverWsDevicesAsUrls(String regexpProtocol, String regexpPath) {
   final Collection<URL> urls =
       new TreeSet<>(
           new Comparator<URL>() {
             public int compare(URL o1, URL o2) {
               return o1.toString().compareTo(o2.toString());
             }
           });
   for (String key : discoverWsDevices()) {
     try {
       final URL url = new URL(key);
       boolean ok = true;
       if (regexpProtocol.length() > 0 && !url.getProtocol().matches(regexpProtocol)) ok = false;
       if (regexpPath.length() > 0 && !url.getPath().matches(regexpPath)) ok = false;
       if (ok) urls.add(url);
     } catch (MalformedURLException e) {
       e.printStackTrace();
     }
   }
   return urls;
 }
 /**
  * Discover WS device on the local network
  *
  * @return list of unique devices access strings which might be URLs in most cases
  */
 public static Collection<String> discoverWsDevices() {
   final Collection<String> addresses = new ConcurrentSkipListSet<>();
   final CountDownLatch serverStarted = new CountDownLatch(1);
   final CountDownLatch serverFinished = new CountDownLatch(1);
   final Collection<InetAddress> addressList = new ArrayList<>();
   try {
     final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
     if (interfaces != null) {
       while (interfaces.hasMoreElements()) {
         NetworkInterface anInterface = interfaces.nextElement();
         if (!anInterface.isLoopback()) {
           final List<InterfaceAddress> interfaceAddresses = anInterface.getInterfaceAddresses();
           for (InterfaceAddress address : interfaceAddresses) {
             addressList.add(address.getAddress());
           }
         }
       }
     }
   } catch (SocketException e) {
     e.printStackTrace();
   }
   ExecutorService executorService = Executors.newCachedThreadPool();
   for (final InetAddress address : addressList) {
     Runnable runnable =
         new Runnable() {
           public void run() {
             try {
               final String uuid = UUID.randomUUID().toString();
               final String probe =
                   WS_DISCOVERY_PROBE_MESSAGE.replaceAll(
                       "<wsa:MessageID>urn:uuid:.*</wsa:MessageID>",
                       "<wsa:MessageID>urn:uuid:" + uuid + "</wsa:MessageID>");
               final int port = random.nextInt(20000) + 40000;
               @SuppressWarnings("SocketOpenedButNotSafelyClosed")
               final DatagramSocket server = new DatagramSocket(port, address);
               new Thread() {
                 public void run() {
                   try {
                     final DatagramPacket packet = new DatagramPacket(new byte[4096], 4096);
                     server.setSoTimeout(WS_DISCOVERY_TIMEOUT);
                     long timerStarted = System.currentTimeMillis();
                     while (System.currentTimeMillis() - timerStarted < (WS_DISCOVERY_TIMEOUT)) {
                       serverStarted.countDown();
                       server.receive(packet);
                       final Collection<String> collection =
                           parseSoapResponseForUrls(
                               Arrays.copyOf(packet.getData(), packet.getLength()));
                       for (String key : collection) {
                         addresses.add(key);
                       }
                     }
                   } catch (SocketTimeoutException ignored) {
                   } catch (Exception e) {
                     e.printStackTrace();
                   } finally {
                     serverFinished.countDown();
                     server.close();
                   }
                 }
               }.start();
               try {
                 serverStarted.await(1000, TimeUnit.MILLISECONDS);
               } catch (InterruptedException e) {
                 e.printStackTrace();
               }
               if (address instanceof Inet4Address) {
                 server.send(
                     new DatagramPacket(
                         probe.getBytes(),
                         probe.length(),
                         InetAddress.getByName(WS_DISCOVERY_ADDRESS_IPv4),
                         WS_DISCOVERY_PORT));
               } else {
                 server.send(
                     new DatagramPacket(
                         probe.getBytes(),
                         probe.length(),
                         InetAddress.getByName(WS_DISCOVERY_ADDRESS_IPv6),
                         WS_DISCOVERY_PORT));
               }
             } catch (Exception e) {
               e.printStackTrace();
             }
             try {
               serverFinished.await((WS_DISCOVERY_TIMEOUT), TimeUnit.MILLISECONDS);
             } catch (InterruptedException e) {
               e.printStackTrace();
             }
           }
         };
     executorService.submit(runnable);
   }
   try {
     executorService.shutdown();
     executorService.awaitTermination(WS_DISCOVERY_TIMEOUT + 2000, TimeUnit.MILLISECONDS);
   } catch (InterruptedException ignored) {
   }
   return addresses;
 }
예제 #8
0
  private void processResource(Session session, Element element, HashMap additionalInformation) {

    String nodeName = element.getNodeName();
    boolean oldSession = nodeName.equals(SessionElement.DATA_FILE.getText());

    String label = getAttribute(element, SessionAttribute.LABEL.getText());
    String name = getAttribute(element, SessionAttribute.NAME.getText());
    String sampleId = getAttribute(element, SessionAttribute.SAMPLE_ID.getText());
    String description = getAttribute(element, SessionAttribute.DESCRIPTION.getText());
    String type = getAttribute(element, SessionAttribute.TYPE.getText());
    String coverage = getAttribute(element, SessionAttribute.COVERAGE.getText());
    String trackLine = getAttribute(element, SessionAttribute.TRACK_LINE.getText());
    String colorString = getAttribute(element, SessionAttribute.COLOR.getText());

    String relPathValue = getAttribute(element, SessionAttribute.RELATIVE_PATH.getText());
    boolean isRelativePath = ((relPathValue != null) && relPathValue.equalsIgnoreCase("true"));
    String serverURL = getAttribute(element, SessionAttribute.SERVER_URL.getText());

    // Older sessions used the "name" attribute for the path.
    String path = getAttribute(element, SessionAttribute.PATH.getText());

    if (oldSession && name != null) {
      path = name;
      int idx = name.lastIndexOf("/");
      if (idx > 0 && idx + 1 < name.length()) {
        name = name.substring(idx + 1);
      }
    }

    ResourceLocator resourceLocator;
    if (isRelativePath) {
      final String sessionPath = session.getPath();

      String absolutePath;
      if (sessionPath == null) {
        log.error("Null session path -- this is not expected");
        MessageUtils.showMessage("Unexpected error loading session: null session path");
        return;
      }
      absolutePath = FileUtils.getAbsolutePath(path, sessionPath);
      fullToRelPathMap.put(absolutePath, path);
      resourceLocator = new ResourceLocator(serverURL, absolutePath);

      // If the resourceLocator is relative, we assume coverage is as well
      if (coverage != null) {
        String absoluteCoveragePath = FileUtils.getAbsolutePath(coverage, sessionPath);
        resourceLocator.setCoverage(absoluteCoveragePath);
      }
    } else {
      resourceLocator = new ResourceLocator(serverURL, path);
      resourceLocator.setCoverage(coverage);
    }

    String url = getAttribute(element, SessionAttribute.URL.getText());
    if (url == null) {
      url = getAttribute(element, SessionAttribute.FEATURE_URL.getText());
    }
    resourceLocator.setUrl(url);

    String infolink = getAttribute(element, SessionAttribute.HYPERLINK.getText());
    if (infolink == null) {
      infolink = getAttribute(element, SessionAttribute.INFOLINK.getText());
    }
    resourceLocator.setInfolink(infolink);

    // Label is deprecated in favor of name.
    if (name != null) {
      resourceLocator.setName(name);
    } else {
      resourceLocator.setName(label);
    }

    resourceLocator.setSampleId(sampleId);

    resourceLocator.setDescription(description);
    // This test added to get around earlier bug in the writer
    if (type != null && !type.equals("local")) {
      resourceLocator.setType(type);
    }
    resourceLocator.setCoverage(coverage);
    resourceLocator.setTrackLine(trackLine);

    if (colorString != null) {
      try {
        Color c = ColorUtilities.stringToColor(colorString);
        resourceLocator.setColor(c);
      } catch (Exception e) {
        log.error("Error setting color: ", e);
      }
    }

    dataFiles.add(resourceLocator);

    NodeList elements = element.getChildNodes();
    process(session, elements, additionalInformation);
  }