public static ModelTree getTreeFromLocation(
      String resourceName, String treeName, Delegator delegator, LocalDispatcher dispatcher)
      throws IOException, SAXException, ParserConfigurationException {
    Map<String, ModelTree> modelTreeMap = treeLocationCache.get(resourceName);
    if (modelTreeMap == null) {
      synchronized (TreeFactory.class) {
        modelTreeMap = treeLocationCache.get(resourceName);
        if (modelTreeMap == null) {
          ClassLoader loader = Thread.currentThread().getContextClassLoader();
          if (loader == null) {
            loader = TreeFactory.class.getClassLoader();
          }

          URL treeFileUrl = null;
          treeFileUrl = FlexibleLocation.resolveLocation(resourceName); // , loader);
          Document treeFileDoc = UtilXml.readXmlDocument(treeFileUrl, true);
          modelTreeMap = readTreeDocument(treeFileDoc, delegator, dispatcher, resourceName);
          treeLocationCache.put(resourceName, modelTreeMap);
        }
      }
    }

    ModelTree modelTree = (ModelTree) modelTreeMap.get(treeName);
    if (modelTree == null) {
      throw new IllegalArgumentException(
          "Could not find tree with name ["
              + treeName
              + "] in class resource ["
              + resourceName
              + "]");
    }
    return modelTree;
  }
  public static List<ComponentDef> getComponentsFromConfig(URL configUrl)
      throws ComponentException {
    if (configUrl == null) {
      throw new ComponentException("Component config file does not exist: " + configUrl);
    }

    List<ComponentDef> componentsFromConfig = null;
    Document document = null;
    try {
      document = UtilXml.readXmlDocument(configUrl, true);
    } catch (SAXException e) {
      throw new ComponentException("Error reading the component config file: " + configUrl, e);
    } catch (ParserConfigurationException e) {
      throw new ComponentException("Error reading the component config file: " + configUrl, e);
    } catch (IOException e) {
      throw new ComponentException("Error reading the component config file: " + configUrl, e);
    }

    Element root = document.getDocumentElement();
    List<? extends Element> toLoad = UtilXml.childElementList(root);
    if (UtilValidate.isNotEmpty(toLoad)) {
      componentsFromConfig = new LinkedList<ComponentDef>();
      for (Element element : toLoad) {
        componentsFromConfig.add(new ComponentDef(element));
      }
    }
    return componentsFromConfig;
  }
 public Document getDocument() throws GenericConfigException {
   try {
     return UtilXml.readXmlDocument(this.getStream(), this.xmlFilename, true);
   } catch (org.xml.sax.SAXException e) {
     throw new GenericConfigException("Error reading " + this.toString(), e);
   } catch (javax.xml.parsers.ParserConfigurationException e) {
     throw new GenericConfigException("Error reading " + this.toString(), e);
   } catch (java.io.IOException e) {
     throw new GenericConfigException("Error reading " + this.toString(), e);
   }
 }
Exemple #4
0
  private static List<Map<Object, Object>> getListMapsFromXmlFile(String XmlFilePath) {
    List<Map<Object, Object>> listMaps = FastList.newInstance();
    InputStream ins = null;
    URL xmlFileUrl = null;
    Document xmlDocument = null;
    try {
      if (UtilValidate.isNotEmpty(XmlFilePath)) {
        xmlFileUrl = UtilURL.fromFilename(XmlFilePath);
        if (xmlFileUrl != null) ins = xmlFileUrl.openStream();
        if (ins != null) {
          xmlDocument = UtilXml.readXmlDocument(ins, xmlFileUrl.toString());
          List<? extends Node> nodeList =
              UtilXml.childNodeList(xmlDocument.getDocumentElement().getFirstChild());
          for (Node node : nodeList) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
              Map<Object, Object> fields = FastMap.newInstance();
              fields.put(node.getNodeName(), UtilXml.elementValue((Element) node));
              Map<Object, Object> attrFields = getAttributeNameValueMap(node);
              Set<Object> keys = attrFields.keySet();
              Iterator<Object> attrFieldsIter = keys.iterator();
              while (attrFieldsIter.hasNext()) {
                Object key = attrFieldsIter.next();
                fields.put(key, attrFields.get(key));
              }

              List<? extends Node> childNodeList = UtilXml.childNodeList(node.getFirstChild());
              for (Node childNode : childNodeList) {
                fields.put(childNode.getNodeName(), UtilXml.elementValue((Element) childNode));
                attrFields = getAttributeNameValueMap(childNode);
                keys = attrFields.keySet();
                attrFieldsIter = keys.iterator();
                while (attrFieldsIter.hasNext()) {
                  Object key = attrFieldsIter.next();
                  fields.put(key, attrFields.get(key));
                }
              }
              listMaps.add(fields);
            }
          }
        }
      }
    } catch (Exception exc) {
      Debug.logError(exc, "Error reading xml file", module);
    } finally {
      try {
        if (ins != null) ins.close();
      } catch (Exception exc) {
        Debug.logError(exc, "Error reading xml file", module);
      }
    }
    return listMaps;
  }
 private void getLabelsFromOfbizComponents()
     throws IOException, SAXException, ParserConfigurationException {
   List<File> componentsFiles =
       FileUtil.findXmlFiles(
           null, null, "ofbiz-component", "http://ofbiz.apache.org/dtds/ofbiz-component.xsd");
   for (File componentFile : componentsFiles) {
     String filePath = componentFile.getPath();
     Document menuDocument = UtilXml.readXmlDocument(componentFile.toURI().toURL());
     Element rootElem = menuDocument.getDocumentElement();
     for (Element elem1 : UtilXml.childElementList(rootElem)) {
       checkOfbizComponentTag(elem1, filePath);
       for (Element elem2 : UtilXml.childElementList(elem1)) {
         checkOfbizComponentTag(elem2, filePath);
       }
     }
   }
 }
 private void getLabelsFromFormWidgets(String inFile, File file)
     throws MalformedURLException, SAXException, ParserConfigurationException, IOException,
         GenericServiceException {
   Set<String> fieldNames = FastSet.newInstance();
   findUiLabelMapInFile(inFile, file.getPath());
   Document formDocument = UtilXml.readXmlDocument(file.toURI().toURL());
   Element rootElem = formDocument.getDocumentElement();
   for (Element formElement : UtilXml.childElementList(rootElem, "form")) {
     for (Element elem : UtilXml.childElementList(formElement, "auto-fields-service")) {
       getAutoFieldsServiceTag(elem, fieldNames);
     }
     for (Element elem : UtilXml.childElementList(formElement, "auto-fields-entity")) {
       getAutoFieldsEntityTag(elem, fieldNames);
     }
     for (String field : fieldNames) {
       String labelKey = formFieldTitle.concat(field);
       if (this.labelSet.contains(labelKey)) {
         setLabelReference(labelKey, file.getPath());
       }
     }
   }
 }
  public static ModelTree getTreeFromWebappContext(
      String resourceName, String treeName, HttpServletRequest request)
      throws IOException, SAXException, ParserConfigurationException {
    String webappName = UtilHttp.getApplicationName(request);
    String cacheKey = webappName + "::" + resourceName;

    Map<String, ModelTree> modelTreeMap = treeWebappCache.get(cacheKey);
    if (modelTreeMap == null) {
      synchronized (TreeFactory.class) {
        modelTreeMap = treeWebappCache.get(cacheKey);
        if (modelTreeMap == null) {
          ServletContext servletContext = (ServletContext) request.getAttribute("servletContext");
          Delegator delegator = (Delegator) request.getAttribute("delegator");
          LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");

          URL treeFileUrl = servletContext.getResource(resourceName);
          Document treeFileDoc = UtilXml.readXmlDocument(treeFileUrl, true);
          modelTreeMap = readTreeDocument(treeFileDoc, delegator, dispatcher, cacheKey);
          treeWebappCache.put(cacheKey, modelTreeMap);
        }
      }
    }

    ModelTree modelTree = (ModelTree) modelTreeMap.get(treeName);
    if (modelTree == null) {
      throw new IllegalArgumentException(
          "Could not find tree with name ["
              + treeName
              + "] in webapp resource ["
              + resourceName
              + "] in the webapp ["
              + webappName
              + "]");
    }
    return modelTree;
  }
Exemple #8
0
  /**
   * getXMLValue
   *
   * <p>From a XML element, get a values map
   *
   * @param fileFullPath File path to parse
   * @return Map contains asked attribute values by attribute name
   */
  public static Map<String, Object> getXMLValue(String fileFullPath, Locale locale)
      throws IllegalStateException, IOException {

    /* VARIABLES */
    Document document;
    Element rootElt;
    Map<String, Map<String, String>> valueMap = FastMap.newInstance();
    Map<String, Object> result = FastMap.newInstance();

    /* PARSING */
    try {
      document = UtilXml.readXmlDocument(new FileInputStream(fileFullPath), fileFullPath);
    } catch (ParserConfigurationException e) {
      String errMsg =
          UtilProperties.getMessage(
                  resource, "ImageTransform.errors_occured_during_parsing", locale)
              + " ImageProperties.xml "
              + e.toString();
      Debug.logError(errMsg, module);
      result.put("errorMessage", "error");
      return result;
    } catch (SAXException e) {
      String errMsg =
          UtilProperties.getMessage(
                  resource, "ImageTransform.errors_occured_during_parsing", locale)
              + " ImageProperties.xml "
              + e.toString();
      Debug.logError(errMsg, module);
      result.put("errorMessage", "error");
      return result;
    } catch (IOException e) {
      String errMsg =
          UtilProperties.getMessage(
                  resource,
                  "ImageTransform.error_prevents_the document_from_being_fully_parsed",
                  locale)
              + e.toString();
      Debug.logError(errMsg, module);
      result.put("errorMessage", "error");
      return result;
    }
    // set Root Element
    try {
      rootElt = document.getDocumentElement();
    } catch (IllegalStateException e) {
      String errMsg =
          UtilProperties.getMessage(
                  resource, "ImageTransform.root_element_has_not_been_set", locale)
              + e.toString();
      Debug.logError(errMsg, module);
      result.put("errorMessage", "error");
      return result;
    }

    /* get NAME and VALUE */
    List<? extends Element> children =
        UtilXml.childElementList(
            rootElt); // FIXME : despite upgrading to jdom 1.1, it seems that getChildren is pre 1.5
                      // java code (ie getChildren does not retun List<Element> but only List)
    for (Element currentElt : children) {
      Map<String, String> eltMap = FastMap.newInstance();
      List<? extends Element> children2 = UtilXml.childElementList(currentElt);
      if (children2.size() > 0) {
        Map<String, String> childMap = FastMap.newInstance();
        // loop over Children 1st level
        for (Element currentChild : children2) {
          childMap.put(currentChild.getAttribute("name"), currentChild.getAttribute("value"));
        }
        valueMap.put(currentElt.getAttribute("name"), childMap);
      } else {
        eltMap.put(currentElt.getAttribute("name"), currentElt.getAttribute("value"));
        valueMap.put(currentElt.getNodeName(), eltMap);
      }
    }

    result.put("responseMessage", "success");
    result.put("xml", valueMap);
    return result;
  }
  protected static Element getRootElement(HttpServletRequest request) {
    if (Debug.infoOn()) {
      Debug.log("Applet config file: " + ldapConfig);
    }
    File configFile = new File(ldapConfig);
    FileInputStream configFileIS = null;
    Element rootElement = null;
    try {
      configFileIS = new FileInputStream(configFile);
      Document configDoc =
          UtilXml.readXmlDocument(configFileIS, "LDAP configuration file " + ldapConfig);
      rootElement = configDoc.getDocumentElement();
    } catch (FileNotFoundException e) {
      Debug.logError(e, "Error calling userLogin service", module);
      Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage());
      String errMsg =
          UtilProperties.getMessage(
              resourceWebapp,
              "loginevents.following_error_occurred_during_login",
              messageMap,
              UtilHttp.getLocale(request));
      request.setAttribute("_ERROR_MESSAGE_", errMsg);
    } catch (SAXException e) {
      Debug.logError(e, "Error calling userLogin service", module);
      Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage());
      String errMsg =
          UtilProperties.getMessage(
              resourceWebapp,
              "loginevents.following_error_occurred_during_login",
              messageMap,
              UtilHttp.getLocale(request));
      request.setAttribute("_ERROR_MESSAGE_", errMsg);
    } catch (ParserConfigurationException e) {
      Debug.logError(e, "Error calling userLogin service", module);
      Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage());
      String errMsg =
          UtilProperties.getMessage(
              resourceWebapp,
              "loginevents.following_error_occurred_during_login",
              messageMap,
              UtilHttp.getLocale(request));
      request.setAttribute("_ERROR_MESSAGE_", errMsg);
    } catch (IOException e) {
      Debug.logError(e, "Error calling userLogin service", module);
      Map<String, String> messageMap = UtilMisc.toMap("errorMessage", e.getMessage());
      String errMsg =
          UtilProperties.getMessage(
              resourceWebapp,
              "loginevents.following_error_occurred_during_login",
              messageMap,
              UtilHttp.getLocale(request));
      request.setAttribute("_ERROR_MESSAGE_", errMsg);
    } finally {
      if (configFileIS != null) {
        try {
          configFileIS.close();
        } catch (IOException e) {
        }
      }
    }

    return rootElement;
  }