예제 #1
0
  public void setValue(String value) {
    synchronized (DOMUtils.getDOMLock(elt)) {
      {
        String oldValue = getValue();
        if (oldValue == null ? value == null : oldValue.equals(value)) return;
        synchronized (DOMUtils.getDOMLock(elt)) {
          // Get rid of any text nodes that are children of the element.
          elt.normalize();
        }
        NodeList nl = elt.getChildNodes();
        for (int i = (nl.getLength() - 1); i >= 0; i--) {
          Node n = nl.item(i);
          if (n.getNodeType() == Node.TEXT_NODE) {
            synchronized (DOMUtils.getDOMLock(elt)) {
              elt.removeChild(n);
            }

            IXArch context = getXArch();
            if (context != null) {
              context.fireXArchEvent(
                  new XArchEvent(
                      this,
                      XArchEvent.CLEAR_EVENT,
                      XArchEvent.SIMPLE_TYPE_VALUE_CHANGED,
                      "$SIMPLETYPEVALUE$",
                      null,
                      XArchUtils.getDefaultXArchImplementation().isContainedIn(xArch, this),
                      true));
            }
          }
        }
      }
      elt.normalize();
      Document doc = elt.getOwnerDocument();
      Text txt = doc.createTextNode(value);
      elt.appendChild(txt);
    }

    IXArch context = getXArch();
    if (context != null) {
      context.fireXArchEvent(
          new XArchEvent(
              this,
              XArchEvent.SET_EVENT,
              XArchEvent.SIMPLE_TYPE_VALUE_CHANGED,
              "$SIMPLETYPEVALUE$",
              value,
              XArchUtils.getDefaultXArchImplementation().isContainedIn(xArch, this)));
    }
  }
예제 #2
0
  public static void write() {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc;

    System.out.println("Writing patient data file\n");

    try {
      DocumentBuilder builder = factory.newDocumentBuilder();
      doc = builder.newDocument();

      Element root = doc.createElement("patientdata");
      root.appendChild(XMLInterface.nl(doc));

      // get first PatientData from DataStore
      Iterator<PatientDataStore> pdsIt = GlobalVars.pds.iterator();
      while (pdsIt.hasNext()) {
        PatientDataStore pds = pdsIt.next();
        Node n = pds.createXMLNode(doc);

        XMLInterface.addNode(doc, root, n, 0);
      }

      root.normalize();
      // create junk to write it out (see java xml tutorial)
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();
      DOMSource src = new DOMSource(root);
      StreamResult result = new StreamResult(new File(GlobalVars.XMLDataFile));
      transformer.transform(src, result);
    } catch (Exception e) {
      System.err.println("Error WRITING xml patient data file");
      e.printStackTrace();
    }
  }
예제 #3
0
  public void clearValue() {
    synchronized (DOMUtils.getDOMLock(elt)) {
      // Get rid of any text nodes that are children of the element.
      elt.normalize();
    }
    NodeList nl = elt.getChildNodes();
    for (int i = (nl.getLength() - 1); i >= 0; i--) {
      Node n = nl.item(i);
      if (n.getNodeType() == Node.TEXT_NODE) {
        synchronized (DOMUtils.getDOMLock(elt)) {
          elt.removeChild(n);
        }

        IXArch context = getXArch();
        if (context != null) {
          context.fireXArchEvent(
              new XArchEvent(
                  this,
                  XArchEvent.CLEAR_EVENT,
                  XArchEvent.SIMPLE_TYPE_VALUE_CHANGED,
                  "$SIMPLETYPEVALUE$",
                  null,
                  XArchUtils.getDefaultXArchImplementation().isContainedIn(xArch, this)));
        }
      }
    }
  }
예제 #4
0
 public static String textInElement(Element elem) {
   StringBuilder buf = new StringBuilder(100);
   elem.normalize();
   NodeList nlst = elem.getChildNodes();
   for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) {
     Node n = nlst.item(i);
     if (n instanceof Text) {
       final String data = ((Text) n).getData();
       buf.append(data);
     }
   }
   return buf.toString();
 }
예제 #5
0
 public String getValue() {
   synchronized (DOMUtils.getDOMLock(elt)) {
     elt.normalize();
     NodeList nl = elt.getChildNodes();
     for (int i = (nl.getLength() - 1); i >= 0; i--) {
       Node n = nl.item(i);
       if (n.getNodeType() == Node.TEXT_NODE) {
         return n.getNodeValue();
       }
     }
     return null;
   }
 }
예제 #6
0
  /**
   * Save the XML description of the circuit
   *
   * @param output an output stream to write in
   * @return true if the dump was successful, false either
   */
  public boolean dumpToXml(OutputStream output) {
    Document doc;
    Element root;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

    try {
      builder = factory.newDocumentBuilder();
      doc = builder.newDocument();
    } catch (ParserConfigurationException pce) {
      System.err.println("dumpToXmlFile: unable to write XML save file.");
      return false;
    }

    root = doc.createElement("Circuit");
    root.setAttribute("name", this.getName());

    for (iterNodes = this.nodes.iterator(); iterNodes.hasNext(); )
      iterNodes.next().dumpToXml(doc, root);

    root.normalize();
    doc.appendChild(root);

    try {
      TransformerFactory tffactory = TransformerFactory.newInstance();
      Transformer transformer = tffactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(output);
      transformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
      System.err.println("dumpToXmlFile:  Configuration Transformer exception.");
      return false;
    } catch (TransformerException te) {
      System.err.println("dumpToXmlFile: Transformer exception.");
      return false;
    }

    return true;
  }
 /**
  * Unmarshall a Chromosome instance from a given XML Element representation.
  *
  * @param a_activeConfiguration current Configuration object
  * @param a_xmlElement the XML Element representation of the Chromosome
  * @return a new Chromosome instance setup with the data from the XML Element representation
  * @throws ImproperXMLException if the given Element is improperly structured or missing data
  * @throws UnsupportedRepresentationException if the actively configured Gene implementation does
  *     not support the string representation of the alleles used in the given XML document
  * @throws GeneCreationException if there is a problem creating or populating a Gene instance
  * @author Neil Rotstan
  * @since 1.0
  */
 public static Gene[] getGenesFromElement(
     Configuration a_activeConfiguration, Element a_xmlElement)
     throws ImproperXMLException, UnsupportedRepresentationException, GeneCreationException {
   // Do some sanity checking. Make sure the XML Element isn't null and
   // that it in fact represents a set of genes.
   // -----------------------------------------------------------------
   if (a_xmlElement == null || !(a_xmlElement.getTagName().equals(GENES_TAG))) {
     throw new ImproperXMLException(
         "Unable to build Chromosome instance from XML Element: "
             + "given Element is not a 'genes' element.");
   }
   List genes = Collections.synchronizedList(new ArrayList());
   // Extract the nested gene elements.
   // ---------------------------------
   NodeList geneElements = a_xmlElement.getElementsByTagName(GENE_TAG);
   if (geneElements == null) {
     throw new ImproperXMLException(
         "Unable to build Gene instances from XML Element: "
             + "'"
             + GENE_TAG
             + "'"
             + " sub-elements not found.");
   }
   // For each gene, get the class attribute so we know what class
   // to instantiate to represent the gene instance, and then find
   // the child text node, which is where the string representation
   // of the allele is located, and extract the representation.
   // -------------------------------------------------------------
   int numberOfGeneNodes = geneElements.getLength();
   for (int i = 0; i < numberOfGeneNodes; i++) {
     Element thisGeneElement = (Element) geneElements.item(i);
     thisGeneElement.normalize();
     // Fetch the class attribute and create an instance of that
     // class to represent the current gene.
     // --------------------------------------------------------
     String geneClassName = thisGeneElement.getAttribute(CLASS_ATTRIBUTE);
     Gene thisGeneObject;
     Class geneClass = null;
     try {
       geneClass = Class.forName(geneClassName);
       try {
         Constructor constr = geneClass.getConstructor(new Class[] {Configuration.class});
         thisGeneObject = (Gene) constr.newInstance(new Object[] {a_activeConfiguration});
       } catch (NoSuchMethodException nsme) {
         // Try it by calling method newGeneInternal.
         // -----------------------------------------
         Constructor constr = geneClass.getConstructor(new Class[] {});
         thisGeneObject = (Gene) constr.newInstance(new Object[] {});
         thisGeneObject =
             (Gene)
                 PrivateAccessor.invoke(
                     thisGeneObject, "newGeneInternal", new Class[] {}, new Object[] {});
       }
     } catch (Throwable e) {
       throw new GeneCreationException(geneClass, e);
     }
     // Find the text node and fetch the string representation of
     // the allele.
     // ---------------------------------------------------------
     NodeList children = thisGeneElement.getChildNodes();
     int childrenSize = children.getLength();
     String alleleRepresentation = null;
     for (int j = 0; j < childrenSize; j++) {
       Element alleleElem = (Element) children.item(j);
       if (alleleElem.getTagName().equals(ALLELE_TAG)) {
         alleleRepresentation = alleleElem.getAttribute("value");
       }
       if (children.item(j).getNodeType() == Node.TEXT_NODE) {
         // We found the text node. Extract the representation.
         // ---------------------------------------------------
         alleleRepresentation = children.item(j).getNodeValue();
         break;
       }
     }
     // Sanity check: Make sure the representation isn't null.
     // ------------------------------------------------------
     if (alleleRepresentation == null) {
       throw new ImproperXMLException(
           "Unable to build Gene instance from XML Element: "
               + "value (allele) is missing representation.");
     }
     // Now set the value of the gene to that reflect the
     // string representation.
     // -------------------------------------------------
     try {
       thisGeneObject.setValueFromPersistentRepresentation(alleleRepresentation);
     } catch (UnsupportedOperationException e) {
       throw new GeneCreationException(
           "Unable to build Gene because it does not support the "
               + "setValueFromPersistentRepresentation() method.");
     }
     // Finally, add the current gene object to the list of genes.
     // ----------------------------------------------------------
     genes.add(thisGeneObject);
   }
   return (Gene[]) genes.toArray(new Gene[genes.size()]);
 }