All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jppf.ui.options.xml.OptionDescriptorParser Maven / Gradle / Ivy

There is a newer version: 6.3-alpha
Show newest version
/*
 * JPPF.
 * Copyright (C) 2005-2015 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jppf.ui.options.xml;

import java.io.*;
import java.net.URL;

import javax.xml.parsers.*;

import org.jppf.ui.options.xml.OptionDescriptor.ItemDescriptor;
import org.jppf.ui.options.xml.OptionDescriptor.ListenerDescriptor;
import org.jppf.ui.options.xml.OptionDescriptor.ScriptDescriptor;
import org.jppf.utils.*;
import org.slf4j.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

/**
 * Instances of this class are used to parse an XML document, describing
 * an options page, into a tree of option descriptors.
 * @author Laurent Cohen
 */
public class OptionDescriptorParser {
  /**
   * Logger for this class.
   */
  private static Logger log = LoggerFactory.getLogger(OptionDescriptorParser.class);
  /**
   * Determines whether debug log statements are enabled.
   */
  private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log);
  /**
   * The DOM parser used to build the descriptor tree.
   */
  private DocumentBuilder parser = null;

  /**
   * Initialize this parser.
   * @throws Exception if the DOM parser could not be initialized.
   */
  public OptionDescriptorParser() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    parser = dbf.newDocumentBuilder();
  }

  /**
   * Parse an XML document in a file into a tree of option descriptors.
   * @param docPath the path to XML document to parse.
   * @return an OptionDescriptor instance, root of the generated tree,
   * or null if the document could not be parsed.
   * @throws Exception if an error occurs while parsing the document.
   */
  public OptionDescriptor parse(final String docPath) throws Exception {
    InputStream is = FileUtils.getFileInputStream(docPath);
    if (is == null) {
      URL url = this.getClass().getClassLoader().getResource(docPath);
      is = url.openStream();
    }
    if (is == null) return null;
    Document doc = parser.parse(is);
    return generateTree(findFirstElement(doc));
  }

  /**
   * Parse an XML document in a reader into a tree of option descriptors.
   * @param reader the reader providing the XML document.
   * @return an OptionDescriptor instance, root of the generated tree,
   * or null if the document could not be parsed.
   * @throws Exception if an error occurs while parsing the document.
   */
  public OptionDescriptor parse(final Reader reader) throws Exception {
    InputSource is = new InputSource(reader);
    Document doc = parser.parse(is);
    return generateTree(findFirstElement(doc));
  }

  /**
   * Find the first node in a document that is an element node.
   * @param doc the document whose children are looked up.
   * @return a Node instance if one was found, or null otherwise.
   */
  public Node findFirstElement(final Document doc) {
    NodeList list = doc.getChildNodes();
    for (int i=0; iOptionDescriptor tree from a DOM subtree.
   * @param node the document to generate the tree from.
   * @return an OptionDescriptor instance, root of the generated tree,
   * or null if the document could not be parsed.
   */
  public OptionDescriptor generateTree(final Node node) {
    OptionDescriptor desc = new OptionDescriptor();
    NamedNodeMap attrMap = node.getAttributes();
    desc.type = attrMap.getNamedItem("type").getNodeValue();
    desc.name = attrMap.getNamedItem("name").getNodeValue();
    Node i18nNode = attrMap.getNamedItem("i18n");
    if (i18nNode != null) desc.i18n = i18nNode.getNodeValue();
    NodeList list = node.getChildNodes();
    for (int i=0; iItemDescriptor instance.
   */
  public ItemDescriptor createItemDescriptor(final Node node) {
    ItemDescriptor desc = new ItemDescriptor();
    NamedNodeMap attrMap = node.getAttributes();
    desc.name = attrMap.getNamedItem("name").getNodeValue();
    desc.selected = attrMap.getNamedItem("selected").getNodeValue();
    return desc;
  }

  /**
   * Create a listener descriptor from a DOM node.
   * @param node the node to generate the listener from.
   * @return a ListenerDescriptor instance.
   */
  public ListenerDescriptor createListenerDescriptor(final Node node) {
    ListenerDescriptor desc = new ListenerDescriptor();
    NamedNodeMap attrMap = node.getAttributes();
    desc.type = attrMap.getNamedItem("type").getNodeValue();
    setListenerAttributes(node, desc);
    return desc;
  }

  /**
   * Set the attributes of a listener descriptor.
   * @param node the parent listener node.
   * @param desc the listener descriptor whose attributes have to be set.
   */
  public void setListenerAttributes(final Node node, final ListenerDescriptor desc) {
    NodeList list = node.getChildNodes();
    for (int i=0; iScriptDescriptor instance.
   */
  public ScriptDescriptor createScriptDescriptor(final Node node) {
    ScriptDescriptor desc = new ScriptDescriptor();
    NamedNodeMap attrs = node.getAttributes();
    desc.language = attrs.getNamedItem("language").getNodeValue();
    Node source = attrs.getNamedItem("source");
    if (source != null) desc.source = source.getNodeValue();
    if ((desc.source == null) || "inline".equalsIgnoreCase(desc.source)) {
      NodeList children = node.getChildNodes();
      for (int j=0; jOptionDescriptor instance.
   */
  public OptionDescriptor loadImport(final Node node) {
    NamedNodeMap attrMap = node.getAttributes();
    OptionDescriptor desc = new OptionDescriptor();
    desc.type = "import";
    Node attr = attrMap.getNamedItem("pluggableView");
    desc.setProperty("pluggableView", (attr != null) ? attr.getNodeValue() : "");
    desc.setProperty("source", attrMap.getNamedItem("source").getNodeValue());
    if (attrMap.getNamedItem("location") != null)  desc.setProperty("location", attrMap.getNamedItem("location").getNodeValue());
    Node debugNode = attrMap.getNamedItem("debug");
    if (debugNode != null) desc.setProperty("debug", debugNode.getNodeValue());
    NodeList children = node.getChildNodes();
    for (int j=0; j




© 2015 - 2025 Weber Informatics LLC | Privacy Policy