コード例 #1
0
  /**
   * Test accessing the XML through a value wrapper.
   *
   * <p>
   *
   * @throws Exception in case something goes wrong
   */
  public void testDirectXmlAccesss() throws Exception {

    CmsObject cms = getCmsObject();
    echo("Testing the direct access of the XML through a value wrapper");

    CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);

    cacheSchema(
        resolver,
        "http://www.opencms.org/test3.xsd",
        "org/opencms/xml/content/xmlcontent-definition-3.mod1.xsd");

    cacheSchema(
        resolver,
        "http://www.opencms.org/test4.xsd",
        "org/opencms/xml/content/xmlcontent-definition-4.xsd");

    String content;
    CmsXmlContent xmlcontent;

    // now read the XML content
    content =
        CmsFileUtil.readFile(
            "org/opencms/xml/content/xmlcontent-4.mod1.xml", CmsEncoder.ENCODING_UTF_8);
    xmlcontent = CmsXmlContentFactory.unmarshal(content, CmsEncoder.ENCODING_UTF_8, resolver);
    // validate the XML structure
    xmlcontent.validateXmlStructure(resolver);

    // new create the content access bean
    CmsJspContentAccessBean bean = new CmsJspContentAccessBean(cms, Locale.ENGLISH, xmlcontent);

    // access the content form the default locale
    CmsJspContentAccessValueWrapper cascade = bean.getValue().get("Cascade");

    CmsJspContentAccessValueWrapper link = cascade.getValue().get("VfsLink");
    assertEquals("/index.html", link.toString());
    System.out.println("\n\n-----------------------------");
    System.out.println("<target> : " + link.getXmlText().get("link/target"));
    System.out.println("<uuid>   : " + link.getXmlText().get("link/uuid"));
    System.out.println("-----------------------------\n");

    assertEquals("/sites/default/index.html", link.getXmlText().get("link/target"));
    assertEquals("7d6c22cd-4e3a-11db-9016-5bf59c6009b3", link.getXmlText().get("link/uuid"));
    assertEquals("/sites/default/index.html", link.getXmlText().get("link[1]/target"));
    assertEquals("7d6c22cd-4e3a-11db-9016-5bf59c6009b3", link.getXmlText().get("link[1]/uuid"));

    CmsJspContentAccessValueWrapper html = cascade.getValue().get("Html");
    assertEquals("a=b&c=d", html.getXmlText().get("links/link/query"));
    assertEquals(
        "/sites/default/noexist/index.html", html.getXmlText().get("links/link[3]/target"));

    assertEquals("/sites/default/index.html", cascade.getXmlText().get("VfsLink[1]/link/target"));
    assertEquals("a=b&c=d", cascade.getXmlText().get("Html/links/link/query"));
  }
コード例 #2
0
  /**
   * Updates the OpenCms XML entity resolver cache with a changed XML schema id.
   *
   * <p>
   *
   * @param resolver the OpenCms XML entity resolver to use
   * @param id the XML schema id to update in the resolver
   * @param filename the name of the file in the RFS where to read the new schema content from
   * @throws Exception if something goes wrong
   */
  private void cacheSchema(CmsXmlEntityResolver resolver, String id, String filename)
      throws Exception {

    // fire "clear cache" event to clear up previously cached schemas
    OpenCms.fireCmsEvent(
        new CmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap<String, Object>()));
    // read the XML from the given file and store it in the resolver
    String content = CmsFileUtil.readFile(filename, CmsEncoder.ENCODING_UTF_8);
    CmsXmlContentDefinition definition = CmsXmlContentDefinition.unmarshal(content, id, resolver);
    System.out.println(definition.getSchema().asXML());
    CmsXmlEntityResolver.cacheSystemId(
        id, definition.getSchema().asXML().getBytes(CmsEncoder.ENCODING_UTF_8));
  }