Package org.webharvest.runtime.variables

Examples of org.webharvest.runtime.variables.Variable


        if (maxLoopsString != null && !"".equals(maxLoopsString.trim())) {
            maxLoops = Double.parseDouble(maxLoopsString);
        }

        BaseElementDef loopValueDef = loopDef.getLoopValueDef();
        Variable loopValue = new BodyProcessor(loopValueDef).run(scraper, context);
        debug(loopValueDef, scraper, loopValue);

        List resultList = new ArrayList();

        List list = loopValue != null ? loopValue.toList() : null;
        if (list != null) {
            Variable itemBeforeLoop = (Variable) context.get(item);
            Variable indexBeforeLoop = (Variable) context.get(index);

            List filteredList = filter != null ? createFilteredList(list, filter) : list;
            Iterator it = filteredList.iterator();

            for (int i = 1; it.hasNext() && i <= maxLoops; i++) {
                Variable currElement = (Variable) it.next();

                // define current value of item variable
                if ( item != null && !"".equals(item) ) {
                    context.put(item, currElement);
                }

                // define current value of index variable
                if ( index != null && !"".equals(index) ) {
                    context.put( index, new NodeVariable(String.valueOf(i)) );
                }

                // execute the loop body
                BaseElementDef bodyDef = loopDef.getLoopBodyDef();
                Variable loopResult = bodyDef != null ? new BodyProcessor(bodyDef).run(scraper, context) : new EmptyVariable();
                debug(bodyDef, scraper, loopResult);
                if (!isEmpty) {
                    resultList.addAll( loopResult.toList() );
                }
            }

            // restores previous value of item variable
            if (item != null && itemBeforeLoop != null) {
View Full Code Here


        Filter filter = new Filter(filterStr, list.size());

        Iterator it = list.iterator();
        int index = 1;
        while (it.hasNext()) {
            Variable curr = (Variable) it.next();

            if (filter.isInFilter(index)) {
                if (filter.isUnique) {
                    String currStr = curr.toString();
                    if (!stringSet.contains(curr.toString())) {
                        result.add(curr);
                        stringSet.add(currStr);
                    }
                } else {
                    result.add(curr);
View Full Code Here

      String name = BaseTemplater.execute( httpParamDef.getName(), scraper.getScriptEngine() );
      String isFileStr = BaseTemplater.execute( httpParamDef.getIsfile(), scraper.getScriptEngine() );
        boolean isFile = CommonUtil.getBooleanValue(isFileStr, false);
      String fileName = BaseTemplater.execute( httpParamDef.getFilename(), scraper.getScriptEngine() );
      String contentType = BaseTemplater.execute( httpParamDef.getContenttype(), scraper.getScriptEngine() );
      Variable value = new BodyProcessor(httpParamDef).execute(scraper, context);

      HttpProcessor httpProcessor = scraper.getRunningHttpProcessor();
      if (httpProcessor != null) {
            httpProcessor.addHttpParam(name, isFile, fileName, contentType, value);
            this.setProperty("Name", name);
View Full Code Here

        this.xqueryDef = xqueryDef;
    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        BaseElementDef xqueryElementDef = xqueryDef.getXqDef();
        Variable xq = getBodyTextContent(xqueryElementDef, scraper, context, true);
        debug(xqueryElementDef, scraper, xq);

        String xqExpression = xq.toString().trim();
        XQueryExternalParamDef[] externalParamDefs = xqueryDef.getExternalParamDefs();

        RuntimeConfig runtimeConfig = scraper.getRuntimeConfig();
        final StaticQueryContext sqc = runtimeConfig.getStaticQueryContext();
        final Configuration config = sqc.getConfiguration();

      try {
          final XQueryExpression exp = runtimeConfig.getXQueryExpressionPool().getCompiledExpression(xqExpression);
        final DynamicQueryContext dynamicContext = new DynamicQueryContext(config);

            // define external parameters
            for (int i = 0; i < externalParamDefs.length; i++) {
                XQueryExternalParamDef externalParamDef = externalParamDefs[i];
                String externalParamName = BaseTemplater.execute( externalParamDef.getName(), scraper.getScriptEngine() );
                String externalParamType = BaseTemplater.execute( externalParamDefs[i].getType(), scraper.getScriptEngine() );
                if (externalParamType == null) {
                    externalParamType = DEFAULT_PARAM_TYPE;
                }

                // check if param type is one of allowed
                if ( !ALLOWED_PARAM_TYPES.contains(externalParamType) ) {
                    throw new ScraperXQueryException("Type " + externalParamType + " is not allowed. Use one of " + ALLOWED_PARAM_TYPES.toString());
                }

                if ( externalParamType.endsWith("*") ) {
                    BodyProcessor bodyProcessor = new BodyProcessor(externalParamDef);
                    bodyProcessor.setProperty("Name", externalParamName);
                    bodyProcessor.setProperty("Type", externalParamType);
                    ListVariable listVar = (ListVariable) bodyProcessor.run(scraper, context);
                    debug(externalParamDef, scraper, listVar);
                   
                    Iterator it = listVar.toList().iterator();
                    List paramList = new ArrayList();
                    while (it.hasNext()) {
                        Variable currVar =  (Variable) it.next();
                        paramList.add( castSimpleValue(externalParamType, currVar, sqc) );
                    }

                    dynamicContext.setParameter(externalParamName, paramList);
                } else {
                    KeyValuePair props[] = {new KeyValuePair("Name", externalParamName), new KeyValuePair("Type", externalParamType)};
                    Variable var = getBodyTextContent(externalParamDef, scraper, context, true, props);

                    debug(externalParamDef, scraper, var);
                   
                    Object value = castSimpleValue(externalParamType, var, sqc);
                    dynamicContext.setParameter(externalParamName, value);
View Full Code Here

     * @param scriptable - The Scriptable object that the property was requested from
     * @return String - The property if it exist
     */
    public Object get(String s, Scriptable scriptable) {
        String result = null;
        Variable var = scraperContext.getVar(s);
        if (var != null) {
            result = var.toString();
        }
        return result;
    }
View Full Code Here

     * @param s          - The property requested for
     * @param scriptable - The Scriptable object that the property was requested from
     * @return boolean - Indicating whether the proty exist or not
     */
    public boolean has(String s, Scriptable scriptable) {
        Variable var = scraperContext.getVar(s);
        return var != null;
    }
View Full Code Here

                for (int i = 0; i < defs.length; i++) {
                    HttpProcessor processor = (HttpProcessor) ProcessorResolver.createProcessor(
                            defs[i], scraper.getConfiguration(), scraper);
                    String documentURI = ((HttpDef) processor.getElementDef()).getUrl();
                    HttpInfo httpInfo = (HttpInfo) context.get("http");
                    Variable content = processor.run(scraper, context);

                    try {
                        // A document URI and a charset should be provided.
                        Object inputSource = inputSourceConstructor.newInstance(
                                new Object[]{new ByteArrayInputStream(content.toBinary()), documentURI, httpInfo.charset});
                        Document document = (Document) documentBuilderParse.invoke(documentBuilder, inputSource);
                        Source source = new DOMSource(document);
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        Result domResult = new StreamResult(out);
                        TransformerFactory factory = TransformerFactory.newInstance();
View Full Code Here

                                     PrefetchDataInfo prefetchedData)
            throws DataServiceFault {
        if (prefetchData) {
            return null;
        }
        Variable scrapedOutput = this.getConfig().getScrapedResult(getScraperVariable());
        try {
            OMElement resultEl = AXIOMUtil.stringToOM(scrapedOutput.toString());
            OMElement entryEl, fieldEl;
            DataEntry dataEntry;
            boolean useColumnNumbers = this.isUsingColumnNumbers();
            int i;
            for (Iterator<OMElement> recordItr = resultEl.getChildElements(); recordItr.hasNext();) {
View Full Code Here

TOP

Related Classes of org.webharvest.runtime.variables.Variable

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.