/** Update the lookup table and the database. */
 public synchronized void update(Document doc) {
   LookupTable lut = LookupTable.getInstance(lutFile);
   Properties props = lut.getProperties();
   Element root = doc.getDocumentElement();
   boolean changed = false;
   Node child = root.getFirstChild();
   while (child != null) {
     if (child instanceof Element) {
       Element term = (Element) child;
       String key = term.getAttribute("key");
       String value = term.getAttribute("value");
       props.setProperty(key, value);
       try {
         index.remove(key);
       } catch (Exception ignore) {
       }
       changed = true;
     }
     child = child.getNextSibling();
   }
   if (changed) {
     try {
       recman.commit();
     } catch (Exception ignore) {
     }
     lut.save();
   }
 }
 /** Get an XML object containing all the terms in the database. */
 public Document getIndexDocument() {
   try {
     Document doc = XmlUtil.getDocument();
     Element root = doc.createElement("Terms");
     doc.appendChild(root);
     TupleBrowser browser = index.browse();
     Tuple tuple = new Tuple();
     while (browser.getNext(tuple)) {
       Element term = doc.createElement("Term");
       root.appendChild(term);
       String key = (String) tuple.getKey();
       String value = (String) tuple.getValue();
       term.setAttribute("key", key.substring(value.length()));
       term.setAttribute("keyType", value);
     }
     return doc;
   } catch (Exception unable) {
     return null;
   }
 }
 /**
  * Get HTML text displaying the active status of the stage.
  *
  * @param childUniqueStatus the status of the stage of which this class is the parent.
  * @return HTML text displaying the active status of the stage.
  */
 public synchronized String getStatusHTML(String childUniqueStatus) {
   String stageUniqueStatus =
       "<tr><td width=\"20%\">Database size:</td><td>" + index.size() + "</td></tr>";
   return super.getStatusHTML(childUniqueStatus + stageUniqueStatus);
 }
  private boolean checkDataset(Dataset ds) {
    boolean ok = true;
    for (Iterator it = ds.iterator(); it.hasNext(); ) {
      DcmElement el = (DcmElement) it.next();
      int tag = 0;
      try {
        tag = el.tag();
      } catch (Exception useZero) {
      }
      String command = scriptTable.get(new Integer(tag));
      if (command != null) {
        if (el.vr() == VRs.SQ) {
          Matcher processMatcher = processPattern.matcher(command);
          if (processMatcher.find()) {
            int i = 0;
            Dataset child;
            while ((child = el.getItem(i++)) != null) {
              ok &= checkDataset(child);
            }
          }
        } else {
          Matcher lookupMatcher = lookupPattern.matcher(command);
          // logger.info("Parsing: "+command);
          while (lookupMatcher.find()) {

            int nGroups = lookupMatcher.groupCount();
            String element = lookupMatcher.group(1).trim();
            String keyType = lookupMatcher.group(2).trim() + "/";
            String action = (nGroups > 2) ? lookupMatcher.group(3).trim() : "";
            String regex = (nGroups > 3) ? lookupMatcher.group(4).trim() : "";

            // logger.info("...nGroups  = "+nGroups);
            // logger.info("...element: |"+element+"|");
            // logger.info("...keyType: |"+keyType+"|");
            // logger.info("...action : |"+action+"|");
            // logger.info("...regex:   |"+regex+"|");

            int targetTag = (element.equals("this") ? tag : DicomObject.getElementTag(element));
            String targetValue = handleNull(ds.getString(targetTag));

            if (!targetValue.equals("")) {
              String key = keyType + targetValue;
              if (lutProps.getProperty(key) == null) {
                boolean there = false;
                if (action.equals("keep")
                    || action.equals("skip")
                    || action.equals("remove")
                    || action.equals("empty")
                    || action.equals("default")) there = true;
                else if (action.equals("ignore")) {
                  regex = removeQuotes(regex);
                  there = targetValue.matches(regex);
                }
                try {
                  if (!there) {
                    index.insert(key, keyType, true);
                    ok = false;
                  }
                } catch (Exception ignore) {
                }
              }
            }
          }
        }
      }
    }
    return ok;
  }