Esempio n. 1
0
  /**
   * Create a document in using the XConfiguration-*.xml found in SCI/modules/MODULE_NAME/etc/
   *
   * @return the built document
   */
  public static Document createDocument() {
    DocumentBuilder docBuilder;
    DocumentBuilderFactory factory;
    Document mainDoc = getDefaultDocument();
    if (mainDoc == null) {
      return null;
    }

    Element root = mainDoc.getDocumentElement();

    factory = ScilabDocumentBuilderFactory.newInstance();

    try {
      docBuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException pce) {
      System.err.println("Cannot create a XML DocumentBuilder:\n" + pce);
      return null;
    }

    List<File> etcs = getEtcDir();
    for (File etc : etcs) {
      File[] xmls =
          etc.listFiles(
              new FilenameFilter() {
                public boolean accept(File dir, String name) {
                  return name.endsWith(".xml") && name.startsWith("XConfiguration-");
                }
              });
      for (File xml : xmls) {
        try {
          Document doc = docBuilder.parse(xml);
          if (xml.getName().equals("XConfiguration-general.xml")) {
            try {
              XPath xp = xpathFactory.newXPath();
              NodeList nodes =
                  (NodeList)
                      xp.compile(
                              "//shortcuts/body/actions/action-folder/action[contains(@key, 'OSSCKEY')]")
                          .evaluate(doc, XPathConstants.NODESET);
              for (int i = 0; i < nodes.getLength(); i++) {
                Element e = (Element) nodes.item(i);
                e.setAttribute(
                    "key",
                    e.getAttribute("key").replace("OSSCKEY", ScilabKeyStroke.getOSMetaKey()));
              }
            } catch (XPathExpressionException e) {
              System.err.println(e);
            }
          }
          Node node = mainDoc.importNode(doc.getDocumentElement(), true);
          NodeList list = root.getElementsByTagName(node.getNodeName());
          if (list.getLength() != 0) {
            root.replaceChild(node, list.item(0));
          }
        } catch (SAXException se) {
          System.err.println(ERROR_READ + xml.getName());
        } catch (IOException ioe) {
          System.err.println(ERROR_READ + xml.getName());
        }
      }
    }

    return mainDoc;
  }