Package org.dom4j

Examples of org.dom4j.XPath


    if (simpleSeqencingUri != null) nsuris.put("imsss", simpleSeqencingUri);

    // Check for organiztaion element. Must provide at least one... title gets ectracted from either
    // the (optional) <title> element or the mandatory identifier attribute.
    // This makes sure, at least a root node gets created in CPManifestTreeModel. 
    XPath meta = rootElement.createXPath("//ns:organization");
    meta.setNamespaceURIs(nsuris);
    Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations?
    if (orgaEl == null) throw new AddingResourceException("resource.no.organisation");

    // Check for at least one <item> element referencing a <resource> of adlcp:scormtype="sco" or "asset",
    // which will serve as an entry point.
    XPath resourcesXPath = rootElement.createXPath("//ns:resources");
    resourcesXPath.setNamespaceURIs(nsuris);
    Element elResources = (Element)resourcesXPath.selectSingleNode(rootElement);
    if (elResources == null) throw new AddingResourceException("resource.no.resource"); // no <resources> element.
    XPath itemsXPath = rootElement.createXPath("//ns:item");
    itemsXPath.setNamespaceURIs(nsuris);
    List items = itemsXPath.selectNodes(rootElement);
    if (items.size() == 0) throw new AddingResourceException("scorm.no.item"); // no <item> element.
   
    // check for scorm 2004 simple sequencing stuff which we do not yet support
    if (seqencingUri != null) {
        XPath seqencingXPath = rootElement.createXPath("//ns:imsss");
        List sequences = seqencingXPath.selectNodes(rootElement);
        if (sequences.size() > 0) throw new AddingResourceException("scorm.found.seqencing"); // seqencing elements found -> scorm 2004
    }
   
    Set set = new HashSet();
    for (Iterator iter = items.iterator(); iter.hasNext();) {
      Element item = (Element) iter.next();
      String identifier = item.attributeValue("identifier");
      //check if identifiers are unique, reject if not so
      if (!set.add(identifier)) throw new AddingResourceException("resource.general.error");//TODO:create special error message for non unique ids
    }
   
   
    for (Iterator iter = items.iterator(); iter.hasNext();) {
      Element item = (Element) iter.next();
      String identifierref = item.attributeValue("identifierref");
      if (identifierref == null) continue;
      XPath resourceXPath = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
      resourceXPath.setNamespaceURIs(nsuris);
      Element elResource = (Element)resourceXPath.selectSingleNode(elResources);
      if (elResource == null) throw new AddingResourceException("resource.no.matching.resource");
      //check for scorm attribute
      Attribute scormAttr = elResource.attribute("scormtype");
      //some packages have attribute written like "scormType"
      Attribute scormAttrUpper = elResource.attribute("scormType");
View Full Code Here


    nsuris.put("ns", nsuri);

    // Check for organiztaion element. Must provide at least one... title gets ectracted from either
    // the (optional) <title> element or the mandatory identifier attribute.
    // This makes sure, at least a root node gets created in CPManifestTreeModel.
    XPath meta = rootElement.createXPath("//ns:organization");
    meta.setNamespaceURIs(nsuris);
    Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations?
    if (orgaEl == null) throw new AddingResourceException("resource.no.organisation");

    // Check for at least one <item> element referencing a <resource>, which will serve as an entry point.
    // This is mandatory, as we need an entry point as the user has the option of setting
    // CPDisplayController to not display a menu at all, in which case the first <item>/<resource>
    // element pair gets displayed.
    XPath resourcesXPath = rootElement.createXPath("//ns:resources");
    resourcesXPath.setNamespaceURIs(nsuris);
    Element elResources = (Element)resourcesXPath.selectSingleNode(rootElement);
    if (elResources == null) throw new AddingResourceException("resource.no.resource"); // no <resources> element.
    XPath itemsXPath = rootElement.createXPath("//ns:item");
    itemsXPath.setNamespaceURIs(nsuris);
    List items = itemsXPath.selectNodes(rootElement);
    if (items.size() == 0) throw new AddingResourceException("resource.no.item"); // no <item> element.
    for (Iterator iter = items.iterator(); iter.hasNext();) {
      Element item = (Element) iter.next();
      String identifierref = item.attributeValue("identifierref");
      if (identifierref == null) continue;
      XPath resourceXPath = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
      resourceXPath.setNamespaceURIs(nsuris);
      Element elResource = (Element)resourceXPath.selectSingleNode(elResources);
      if (elResource == null) throw new AddingResourceException("resource.no.matching.resource");
      if (elResource.attribute("scormtype") != null) return false;
      if (elResource.attribute("scormType") != null) return false;
      if (elResource.attribute("SCORMTYPE") != null) return false;
      if (elResource.attributeValue("href") != null) return true; // success.
View Full Code Here

            if (numPoints <= 0) continue;
            Element conditionvar = (Element) el_respc.selectSingleNode(".//conditionvar");
            // there is an evaluation defined (a "resprocessing" element exists)
            // if (xpath(count(.//varequal) + count(.//not) = count(.//*)) is
            // true, then there are only "not" and "varequal" elements
            XPath xCanHandle = DocumentHelper.createXPath("count(.//varequal) + count(.//not) = count(.//*)");
            boolean canHandle = xCanHandle.matches(conditionvar);
            if (!canHandle) { // maybe we have <condvar> <and> <...>, try again
              Element el_and = (Element) conditionvar.selectSingleNode("and");
              if (el_and != null) {
                canHandle = xCanHandle.matches(el_and);
                if (canHandle) { // simultate the el_and to be the conditionvar
                  conditionvar = el_and;
                }
              } else { // and finally, maybe we have an <or> element ..
                Element el_or = (Element) conditionvar.selectSingleNode("or");
                if (el_or != null) {
                  canHandle = xCanHandle.matches(el_or);
                  if (canHandle) { // simultate the el_and to be the conditionvar
                    conditionvar = el_or;
                  }
                }
              }
View Full Code Here

        String htmlWithMarkup = parsedDatum.getParsedText();
        Document doc = reader.read(new StringInputStream(htmlWithMarkup));
       
        // We have to do helicopter stunts since HTML has a global namespace on it, set
        // at the <html> element level.
        XPath xpath = DocumentHelper.createXPath("/xhtml:html/xhtml:body/xhtml:p");
        Map<String, String> namespaceUris = new HashMap<String, String>();
        namespaceUris.put("xhtml", "http://www.w3.org/1999/xhtml");
        xpath.setNamespaceURIs(namespaceUris);
       
        Node paragraphNode = xpath.selectSingleNode(doc);
        Assert.assertNotNull(paragraphNode);
        Assert.assertEquals("this is a test", paragraphNode.getText());
    }
View Full Code Here

    }

    public Element getElement( String xpathExpr )
        throws XMLException
    {
        XPath xpath = createXPath( xpathExpr );
        Object evaluated = xpath.selectSingleNode( document );

        if ( evaluated == null )
        {
            return null;
        }
View Full Code Here

        }
    }

    private XPath createXPath( String xpathExpr )
    {
        XPath xpath = document.createXPath( xpathExpr );
        if ( !this.namespaceMap.isEmpty() )
        {
            xpath.setNamespaceURIs( this.namespaceMap );
        }
        return xpath;
    }
View Full Code Here

    }

    public boolean hasElement( String xpathExpr )
        throws XMLException
    {
        XPath xpath = createXPath( xpathExpr );
        Object evaluated = xpath.selectSingleNode( document );

        if ( evaluated == null )
        {
            return false;
        }
View Full Code Here

    }

    public String getElementText( Node context, String xpathExpr )
        throws XMLException
    {
        XPath xpath = createXPath( xpathExpr );
        Object evaluated = xpath.selectSingleNode( context );

        if ( evaluated == null )
        {
            return null;
        }
View Full Code Here

    }

    public String getElementText( String xpathExpr )
        throws XMLException
    {
        XPath xpath = createXPath( xpathExpr );
        Object evaluated = xpath.selectSingleNode( document );

        if ( evaluated == null )
        {
            return null;
        }
View Full Code Here

    @SuppressWarnings("unchecked")
    public List<Element> getElementList( String xpathExpr )
        throws XMLException
    {
        XPath xpath = createXPath( xpathExpr );
        Object evaluated = xpath.evaluate( document );

        if ( evaluated == null )
        {
            return null;
        }
View Full Code Here

TOP

Related Classes of org.dom4j.XPath

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.