Search in sources :

Example 46 with XObject

use of org.apache.xpath.objects.XObject in project robovm by robovm.

the class Variable method execute.

/**
   * Dereference the variable, and return the reference value.  Note that lazy 
   * evaluation will occur.  If a variable within scope is not found, a warning 
   * will be sent to the error listener, and an empty nodeset will be returned.
   *
   *
   * @param xctxt The runtime execution context.
   *
   * @return The evaluated variable, or an empty nodeset if not found.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt, boolean destructiveOK) throws javax.xml.transform.TransformerException {
    org.apache.xml.utils.PrefixResolver xprefixResolver = xctxt.getNamespaceContext();
    XObject result;
    // XObject result = xctxt.getVariable(m_qname);
    if (m_fixUpWasCalled) {
        if (m_isGlobal)
            result = xctxt.getVarStack().getGlobalVariable(xctxt, m_index, destructiveOK);
        else
            result = xctxt.getVarStack().getLocalVariable(xctxt, m_index, destructiveOK);
    } else {
        result = xctxt.getVarStack().getVariableOrParam(xctxt, m_qname);
    }
    if (null == result) {
        // This should now never happen...
        warn(xctxt, XPATHErrorResources.WG_ILLEGAL_VARIABLE_REFERENCE, //"VariableReference given for variable out "+
        new Object[] { m_qname.getLocalPart() });
        //      (new RuntimeException()).printStackTrace();
        //      error(xctxt, XPATHErrorResources.ER_COULDNOT_GET_VAR_NAMED,
        //            new Object[]{ m_qname.getLocalPart() });  //"Could not get variable named "+varName);
        result = new XNodeSet(xctxt.getDTMManager());
    }
    return result;
//    }
//    else
//    {
//      // Hack city... big time.  This is needed to evaluate xpaths from extensions, 
//      // pending some bright light going off in my head.  Some sort of callback?
//      synchronized(this)
//      {
//      	org.apache.xalan.templates.ElemVariable vvar= getElemVariable();
//      	if(null != vvar)
//      	{
//          m_index = vvar.getIndex();
//          m_isGlobal = vvar.getIsTopLevel();
//          m_fixUpWasCalled = true;
//          return execute(xctxt);
//      	}
//      }
//      throw new javax.xml.transform.TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_VAR_NOT_RESOLVABLE, new Object[]{m_qname.toString()})); //"Variable not resolvable: "+m_qname);
//    }
}
Also used : XObject(org.apache.xpath.objects.XObject) XNodeSet(org.apache.xpath.objects.XNodeSet)

Example 47 with XObject

use of org.apache.xpath.objects.XObject in project fess by codelibs.

the class FessXpathTransformer method storeData.

@Override
protected void storeData(final ResponseData responseData, final ResultData resultData) {
    final DOMParser parser = getDomParser();
    try (final BufferedInputStream bis = new BufferedInputStream(responseData.getResponseBody())) {
        final byte[] bomBytes = new byte[UTF8_BOM_SIZE];
        bis.mark(UTF8_BOM_SIZE);
        final int size = bis.read(bomBytes);
        if (size < 3 || !isUtf8BomBytes(bomBytes)) {
            bis.reset();
        }
        final InputSource is = new InputSource(bis);
        if (responseData.getCharSet() != null) {
            is.setEncoding(responseData.getCharSet());
        }
        parser.parse(is);
    } catch (final Exception e) {
        throw new CrawlingAccessException("Could not parse " + responseData.getUrl(), e);
    }
    final Document document = parser.getDocument();
    if (!fessConfig.isCrawlerIgnoreMetaRobots()) {
        processMetaRobots(responseData, resultData, document);
    }
    final Map<String, Object> dataMap = new LinkedHashMap<>();
    for (final Map.Entry<String, String> entry : fieldRuleMap.entrySet()) {
        final String path = entry.getValue();
        try {
            final XObject xObj = getXPathAPI().eval(document, path);
            final int type = xObj.getType();
            switch(type) {
                case XObject.CLASS_BOOLEAN:
                    final boolean b = xObj.bool();
                    putResultDataBody(dataMap, entry.getKey(), Boolean.toString(b));
                    break;
                case XObject.CLASS_NUMBER:
                    final double d = xObj.num();
                    putResultDataBody(dataMap, entry.getKey(), Double.toString(d));
                    break;
                case XObject.CLASS_STRING:
                    final String str = xObj.str();
                    putResultDataBody(dataMap, entry.getKey(), str);
                    break;
                case XObject.CLASS_NULL:
                case XObject.CLASS_UNKNOWN:
                case XObject.CLASS_NODESET:
                case XObject.CLASS_RTREEFRAG:
                case XObject.CLASS_UNRESOLVEDVARIABLE:
                default:
                    final Node value = getXPathAPI().selectSingleNode(document, entry.getValue());
                    putResultDataBody(dataMap, entry.getKey(), value != null ? value.getTextContent() : null);
                    break;
            }
        } catch (final TransformerException e) {
            logger.warn("Could not parse a value of " + entry.getKey() + ":" + entry.getValue());
        }
    }
    putAdditionalData(dataMap, responseData, document);
    try {
        resultData.setData(SerializeUtil.fromObjectToBinary(dataMap));
    } catch (final Exception e) {
        throw new CrawlingAccessException("Could not serialize object: " + responseData.getUrl(), e);
    }
    resultData.setEncoding(charsetName);
}
Also used : InputSource(org.xml.sax.InputSource) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) CrawlingAccessException(org.codelibs.fess.crawler.exception.CrawlingAccessException) CrawlerSystemException(org.codelibs.fess.crawler.exception.CrawlerSystemException) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) ChildUrlsException(org.codelibs.fess.crawler.exception.ChildUrlsException) LinkedHashMap(java.util.LinkedHashMap) BufferedInputStream(java.io.BufferedInputStream) XObject(org.apache.xpath.objects.XObject) DOMParser(org.cyberneko.html.parsers.DOMParser) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 48 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class PredicatedNodeTest method acceptNode.

//=============== NodeFilter Implementation ===============
/**
   *  Test whether a specified node is visible in the logical view of a
   * TreeWalker or NodeIterator. This function will be called by the
   * implementation of TreeWalker and NodeIterator; it is not intended to
   * be called directly from user code.
   * @param n  The node to check to see if it passes the filter or not.
   * @return  a constant to determine whether the node is accepted,
   *   rejected, or skipped, as defined  above .
   */
public short acceptNode(int n) {
    XPathContext xctxt = m_lpi.getXPathContext();
    try {
        xctxt.pushCurrentNode(n);
        XObject score = execute(xctxt, n);
        // System.out.println("\n::acceptNode - score: "+score.num()+"::");
        if (score != NodeTest.SCORE_NONE) {
            if (getPredicateCount() > 0) {
                countProximityPosition(0);
                if (!executePredicates(n, xctxt))
                    return DTMIterator.FILTER_SKIP;
            }
            return DTMIterator.FILTER_ACCEPT;
        }
    } catch (javax.xml.transform.TransformerException se) {
        // TODO: Fix this.
        throw new RuntimeException(se.getMessage());
    } finally {
        xctxt.popCurrentNode();
    }
    return DTMIterator.FILTER_SKIP;
}
Also used : XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Example 49 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class FuncSystemProperty method execute.

/**
   * Execute the function.  The function must return
   * a valid object.
   * @param xctxt The current execution context.
   * @return A valid XObject.
   *
   * @throws javax.xml.transform.TransformerException
   */
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException {
    String fullName = m_arg0.execute(xctxt).str();
    int indexOfNSSep = fullName.indexOf(':');
    String result = null;
    String propName = "";
    // List of properties where the name of the
    // property argument is to be looked for.
    Properties xsltInfo = new Properties();
    loadPropertyFile(XSLT_PROPERTIES, xsltInfo);
    if (indexOfNSSep > 0) {
        String prefix = (indexOfNSSep >= 0) ? fullName.substring(0, indexOfNSSep) : "";
        String namespace;
        namespace = xctxt.getNamespaceContext().getNamespaceForPrefix(prefix);
        propName = (indexOfNSSep < 0) ? fullName : fullName.substring(indexOfNSSep + 1);
        if (namespace.startsWith("http://www.w3.org/XSL/Transform") || namespace.equals("http://www.w3.org/1999/XSL/Transform")) {
            result = xsltInfo.getProperty(propName);
            if (null == result) {
                warn(xctxt, XPATHErrorResources.WG_PROPERTY_NOT_SUPPORTED, //"XSL Property not supported: "+fullName);
                new Object[] { fullName });
                return XString.EMPTYSTRING;
            }
        } else {
            warn(xctxt, XPATHErrorResources.WG_DONT_DO_ANYTHING_WITH_NS, new Object[] { namespace, //"Don't currently do anything with namespace "+namespace+" in property: "+fullName);
            fullName });
            try {
                //if secure procession is enabled only handle required properties do not not map any valid system property
                if (!xctxt.isSecureProcessing()) {
                    result = System.getProperty(propName);
                } else {
                    warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
                    new Object[] { fullName });
                }
                if (null == result) {
                    return XString.EMPTYSTRING;
                }
            } catch (SecurityException se) {
                warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
                new Object[] { fullName });
                return XString.EMPTYSTRING;
            }
        }
    } else {
        try {
            //if secure procession is enabled only handle required properties do not not map any valid system property
            if (!xctxt.isSecureProcessing()) {
                result = System.getProperty(fullName);
            } else {
                warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
                new Object[] { fullName });
            }
            if (null == result) {
                return XString.EMPTYSTRING;
            }
        } catch (SecurityException se) {
            warn(xctxt, XPATHErrorResources.WG_SECURITY_EXCEPTION, //"SecurityException when trying to access XSL system property: "+fullName);
            new Object[] { fullName });
            return XString.EMPTYSTRING;
        }
    }
    if (propName.equals("version") && result.length() > 0) {
        try {
            // Needs to return the version number of the spec we conform to.
            return new XString("1.0");
        } catch (Exception ex) {
            return new XString(result);
        }
    } else
        return new XString(result);
}
Also used : XString(org.apache.xpath.objects.XString) XObject(org.apache.xpath.objects.XObject) XString(org.apache.xpath.objects.XString) Properties(java.util.Properties)

Example 50 with XObject

use of org.apache.xpath.objects.XObject in project j2objc by google.

the class JAXPVariableStack method getVariableOrParam.

public XObject getVariableOrParam(XPathContext xctxt, QName qname) throws TransformerException, IllegalArgumentException {
    if (qname == null) {
        //JAXP 1.3 spec says that if variable name is null then 
        // we need to through IllegalArgumentException
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ARG_CANNOT_BE_NULL, new Object[] { "Variable qname" });
        throw new IllegalArgumentException(fmsg);
    }
    javax.xml.namespace.QName name = new javax.xml.namespace.QName(qname.getNamespace(), qname.getLocalPart());
    Object varValue = resolver.resolveVariable(name);
    if (varValue == null) {
        String fmsg = XSLMessages.createXPATHMessage(XPATHErrorResources.ER_RESOLVE_VARIABLE_RETURNS_NULL, new Object[] { name.toString() });
        throw new TransformerException(fmsg);
    }
    return XObject.create(varValue, xctxt);
}
Also used : QName(org.apache.xml.utils.QName) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Aggregations

XObject (org.apache.xpath.objects.XObject)102 TransformerException (javax.xml.transform.TransformerException)24 DTM (org.apache.xml.dtm.DTM)24 XPathContext (org.apache.xpath.XPathContext)24 XNodeSet (org.apache.xpath.objects.XNodeSet)14 DTMIterator (org.apache.xml.dtm.DTMIterator)12 VariableStack (org.apache.xpath.VariableStack)10 Vector (java.util.Vector)8 QName (org.apache.xml.utils.QName)8 Expression (org.apache.xpath.Expression)8 Node (org.w3c.dom.Node)7 DTMAxisTraverser (org.apache.xml.dtm.DTMAxisTraverser)6 XMLString (org.apache.xml.utils.XMLString)6 org.apache.xpath (org.apache.xpath)6 XString (org.apache.xpath.objects.XString)6 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4 QName (javax.xml.namespace.QName)4 XPathFunction (javax.xml.xpath.XPathFunction)4 XPathFunctionException (javax.xml.xpath.XPathFunctionException)4