示例#1
0
 public void testAll() throws Exception {
   XMLHelper xmlHelper = new XMLHelper();
   ClassLoader cl = Thread.currentThread().getContextClassLoader();
   InputStream is = cl.getResourceAsStream("org/hibernate/test/annotations/reflection/orm.xml");
   assertNotNull("ORM.xml not found", is);
   XMLContext context = new XMLContext();
   List errors = new ArrayList();
   SAXReader saxReader =
       xmlHelper.createSAXReader("XML InputStream", errors, EJB3DTDEntityResolver.INSTANCE);
   // saxReader.setValidation( false );
   try {
     saxReader.setFeature("http://apache.org/xml/features/validation/schema", true);
   } catch (SAXNotSupportedException e) {
     saxReader.setValidation(false);
   }
   org.dom4j.Document doc;
   try {
     doc = saxReader.read(new InputSource(new BufferedInputStream(is)));
   } finally {
     try {
       is.close();
     } catch (IOException ioe) {
       // log.warn( "Could not close input stream", ioe );
     }
   }
   assertEquals(0, errors.size());
   context.addDocument(doc);
 }
示例#2
0
 /**
  * Unregisters all JSP page servlets for a plugin.
  *
  * @param webXML the web.xml file containing JSP page names to servlet class file mappings.
  */
 public static void unregisterServlets(File webXML) {
   if (!webXML.exists()) {
     Log.error(
         "Could not unregister plugin servlets, file "
             + webXML.getAbsolutePath()
             + " does not exist.");
     return;
   }
   // Find the name of the plugin directory given that the webXML file
   // lives in plugins/[pluginName]/web/web.xml
   String pluginName = webXML.getParentFile().getParentFile().getParentFile().getName();
   try {
     SAXReader saxReader = new SAXReader(false);
     saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     Document doc = saxReader.read(webXML);
     // Find all <servelt-mapping> entries to discover name to URL mapping.
     List names = doc.selectNodes("//servlet-mapping");
     for (int i = 0; i < names.size(); i++) {
       Element nameElement = (Element) names.get(i);
       String url = nameElement.element("url-pattern").getTextTrim();
       // Destroy the servlet than remove from servlets map.
       GenericServlet servlet = servlets.get(pluginName + url);
       if (servlet != null) {
         servlet.destroy();
       }
       servlets.remove(pluginName + url);
       servlet = null;
     }
   } catch (Throwable e) {
     Log.error(e.getMessage(), e);
   }
 }
示例#3
0
 /**
  * Registers all JSP page servlets for a plugin.
  *
  * @param manager the plugin manager.
  * @param plugin the plugin.
  * @param webXML the web.xml file containing JSP page names to servlet class file mappings.
  */
 public static void registerServlets(PluginManager manager, Plugin plugin, File webXML) {
   pluginManager = manager;
   if (!webXML.exists()) {
     Log.error(
         "Could not register plugin servlets, file "
             + webXML.getAbsolutePath()
             + " does not exist.");
     return;
   }
   // Find the name of the plugin directory given that the webXML file
   // lives in plugins/[pluginName]/web/web.xml
   String pluginName = webXML.getParentFile().getParentFile().getParentFile().getName();
   try {
     // Make the reader non-validating so that it doesn't try to resolve external
     // DTD's. Trying to resolve external DTD's can break on some firewall configurations.
     SAXReader saxReader = new SAXReader(false);
     try {
       saxReader.setFeature(
           "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     } catch (SAXException e) {
       Log.warn("Error setting SAXReader feature", e);
     }
     Document doc = saxReader.read(webXML);
     // Find all <servlet> entries to discover name to class mapping.
     List classes = doc.selectNodes("//servlet");
     Map<String, Class> classMap = new HashMap<String, Class>();
     for (int i = 0; i < classes.size(); i++) {
       Element servletElement = (Element) classes.get(i);
       String name = servletElement.element("servlet-name").getTextTrim();
       String className = servletElement.element("servlet-class").getTextTrim();
       classMap.put(name, manager.loadClass(plugin, className));
     }
     // Find all <servelt-mapping> entries to discover name to URL mapping.
     List names = doc.selectNodes("//servlet-mapping");
     for (int i = 0; i < names.size(); i++) {
       Element nameElement = (Element) names.get(i);
       String name = nameElement.element("servlet-name").getTextTrim();
       String url = nameElement.element("url-pattern").getTextTrim();
       // Register the servlet for the URL.
       Class servletClass = classMap.get(name);
       if (servletClass == null) {
         Log.error("Unable to load servlet, " + name + ", servlet-class not found.");
         continue;
       }
       Object instance = servletClass.newInstance();
       if (instance instanceof GenericServlet) {
         // Initialize the servlet then add it to the map..
         ((GenericServlet) instance).init(servletConfig);
         servlets.put(pluginName + url, (GenericServlet) instance);
       } else {
         Log.warn("Could not load " + (pluginName + url) + ": not a servlet.");
       }
     }
   } catch (Throwable e) {
     Log.error(e.getMessage(), e);
   }
 }
示例#4
0
  protected org.dom4j.io.SAXReader getSAXReader(boolean validate) {
    org.dom4j.io.SAXReader reader = null;

    if (!PropsValues.XML_VALIDATION_ENABLED) {
      validate = false;
    }

    try {
      reader = new org.dom4j.io.SAXReader(new SAXParser(), validate);

      reader.setEntityResolver(new EntityResolver());

      reader.setFeature(_FEATURES_DYNAMIC, validate);
      reader.setFeature(_FEATURES_VALIDATION, validate);
      reader.setFeature(_FEATURES_VALIDATION_SCHEMA, validate);
      reader.setFeature(_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, validate);

      if (!validate) {
        reader.setFeature(_FEATURES_LOAD_DTD_GRAMMAR, validate);
        reader.setFeature(_FEATURES_LOAD_EXTERNAL_DTD, validate);
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn("XSD validation is disabled because " + e.getMessage());
      }

      reader = new org.dom4j.io.SAXReader(false);

      reader.setEntityResolver(new EntityResolver());
    }

    return reader;
  }
示例#5
0
  public static org.dom4j.Element readStrToDOM4J(final String data) throws Exception {
    SAXReader saxReader = new SAXReader();

    saxReader.setValidation(false);
    saxReader.setStripWhitespaceText(true);
    saxReader.setFeature("http://xml.org/sax/features/namespaces", false);
    saxReader.getXMLReader().setFeature("http://xml.org/sax/features/namespaces", false);

    // saxReader.setFeature("http://apache.org/xml/features/validation/schema", false);
    // saxReader.setFeature("http://xml.org/sax/features/validation", false);
    // saxReader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
    //                   (FormViewFactory.class.getResource("../form.xsd")).getPath());

    org.dom4j.Document document = saxReader.read(new StringReader(data));
    return document.getRootElement();
  }
  public Document getPath(String filePath, String coding) {

    SAXReader saxReader = new SAXReader();

    Document document = null;
    try {
      saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
      BufferedReader read =
          new BufferedReader(
              new InputStreamReader(new FileInputStream(new File(filePath)), coding));
      document = saxReader.read(read);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return document;
  }
示例#7
0
  /**
   * Reads file and returns source model data. For each source definition, a {@link GEMSourceData}
   * is created and stored in a list.
   */
  @SuppressWarnings("unchecked")
  public List<GEMSourceData> read() {
    if (System.getProperty("openquake.nrml.schema") == null)
      throw new RuntimeException("Set openquake.nrml.schema property  to the NRML schema path");

    this.sourceList.clear();

    File xml = new File(path);
    SAXReader reader = new SAXReader(true);
    Document doc = null;
    try {
      reader.setFeature("http://apache.org/xml/features/validation/schema", true);
      reader.setProperty(
          "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
          "http://www.w3.org/2001/XMLSchema");
      reader.setProperty(
          "http://java.sun.com/xml/jaxp/properties/schemaSource",
          "file://" + System.getProperty("openquake.nrml.schema"));
      doc = reader.read(xml);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    Element sourceModel = doc.getRootElement().element(SOURCE_MODEL);
    Iterator i = sourceModel.elements().iterator();

    while (i.hasNext()) {
      Element elem = (Element) i.next();
      String elemName = elem.getName();
      if (elemName.equalsIgnoreCase(SIMPLE_FAULT)) {
        sourceList.add(getSimpleFaultSourceData(deltaMFD, elem));
      } else if (elemName.equalsIgnoreCase(COMPLEX_FAULT)) {
        sourceList.add(getComplexFaultSourceData(deltaMFD, elem));
      } else if (elemName.equalsIgnoreCase(AREA)) {
        sourceList.add(getAreaSourceData(deltaMFD, elem));
      } else if (elemName.equalsIgnoreCase(POINT)) {
        sourceList.add(getPointSourceData(deltaMFD, elem));
      }
    }
    return sourceList;
  }