Package org.webharvest.runtime.variables

Examples of org.webharvest.runtime.variables.Variable


     * @param overwrite
     */
    public void defineVariable(String varName, Object varValue, boolean overwrite) {
        ScraperContext context = scraper.getContext();
        if (overwrite || context.get(varName) == null) {
            Variable var = CommonUtil.createVariable(varValue);
            context.put(varName, var);
        }
    }
View Full Code Here


    public ScraperContext(Scraper scraper) {
    this(scraper, null);
    }

  public Variable getVar(String name) {
        Variable value = (Variable) this.get(name);
        if ( value == null && callerContext != null && name.startsWith("caller.") ) {
            return callerContext.getVar(name.substring(7));
        }
        return value;
    }
View Full Code Here

        }
        return value;
    }

    public Object setVar(Object key, Object value) {
        Variable var = CommonUtil.createVariable(value);
        return super.put(key, var);
    }
View Full Code Here

       
        if (ifDefs != null) {
          for (int i = 0; i < ifDefs.length; i++) {
            String condition = BaseTemplater.execute( ifDefs[i].getCondition(), scraper.getScriptEngine() );
            if ( CommonUtil.isBooleanTrue(condition) ) {
              Variable ifResult = new BodyProcessor(ifDefs[i]).run(scraper, context);
                    debug(ifDefs[i], scraper, ifResult);
                    return ifResult;
                }
          }
        }

        BaseElementDef elseDef = caseDef.getElseDef();
        if (elseDef != null) {
          Variable elseResult = new BodyProcessor(elseDef).run(scraper, context);
            debug(elseDef, scraper, elseResult);
            return elseResult;
        }
       
        return new EmptyVariable();
View Full Code Here

    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        ScriptEngine scriptEngine = scraper.getScriptEngine();
        String name = BaseTemplater.execute( httpHeaderDef.getName(), scriptEngine);
      Variable value = getBodyTextContent(httpHeaderDef, scraper, context);
       
      HttpProcessor httpProcessor = scraper.getRunningHttpProcessor();
      if (httpProcessor != null) {
        httpProcessor.addHttpHeader(name, value.toString());
            this.setProperty("Name", name);
        } else {
        throw new HttpException("Usage of http-header processor is not allowed outside of http processor!");
      }
       
View Full Code Here

    public Variable execute(Scraper scraper, ScraperContext context) {
        ScriptEngine scriptEngine = scraper.getScriptEngine();

        BaseElementDef patternDef = regexpDef.getRegexpPatternDef();
        Variable patternVar = getBodyTextContent(patternDef, scraper, context, true);
        debug(patternDef, scraper, patternVar);

        BaseElementDef sourceDef = regexpDef.getRegexpSourceDef();
        Variable source = new BodyProcessor(sourceDef).run(scraper, context);
        debug(sourceDef, scraper, source);
       
        String replace = BaseTemplater.execute( regexpDef.getReplace(), scriptEngine);
        boolean isReplace = CommonUtil.isBooleanTrue(replace);

        boolean flagCaseInsensitive = CommonUtil.getBooleanValue( BaseTemplater.execute(regexpDef.getFlagCaseInsensitive(), scriptEngine), false );
        boolean flagMultiline = CommonUtil.getBooleanValue( BaseTemplater.execute(regexpDef.getFlagMultiline(), scriptEngine), false );
        boolean flagDotall = CommonUtil.getBooleanValue( BaseTemplater.execute(regexpDef.getFlagDotall(), scriptEngine), true );
        boolean flagUnicodecase = CommonUtil.getBooleanValue( BaseTemplater.execute(regexpDef.getFlagUnicodecase(), scriptEngine), true );
        boolean flagCanoneq = CommonUtil.getBooleanValue( BaseTemplater.execute(regexpDef.getFlagCanoneq(), scriptEngine), false );

        this.setProperty("Is replacing", String.valueOf(isReplace));
        this.setProperty("Flag CaseInsensitive", String.valueOf(flagCaseInsensitive));
        this.setProperty("Flag MultiLine", String.valueOf(flagMultiline));
        this.setProperty("Flag DotAll", String.valueOf(flagDotall));
        this.setProperty("Flag UnicodeCase", String.valueOf(flagUnicodecase));
        this.setProperty("Flag CanonEq", String.valueOf(flagCanoneq));

        String maxLoopsString = BaseTemplater.execute( regexpDef.getMax(), scriptEngine);
        double maxLoops = Constants.DEFAULT_MAX_LOOPS;
        if (maxLoopsString != null && !"".equals(maxLoopsString.trim())) {
            maxLoops = Double.parseDouble(maxLoopsString);
        }

        this.setProperty("Max loops", String.valueOf(maxLoops));

        int flags = 0;
        if (flagCaseInsensitive) {
            flags |= Pattern.CASE_INSENSITIVE;
        }
        if (flagMultiline) {
            flags |= Pattern.MULTILINE;
        }
        if (flagDotall) {
            flags |= Pattern.DOTALL;
        }
        if (flagUnicodecase) {
            flags |= Pattern.UNICODE_CASE;
        }
        if (flagCanoneq) {
            flags |= Pattern.CANON_EQ;
        }

        Pattern pattern = Pattern.compile(patternVar.toString(), flags);
       
        List resultList = new ArrayList();
       
        List bodyList = source.toList();
        Iterator it = bodyList.iterator();
        while (it.hasNext()) {
          Variable currVar = (Variable) it.next();
          String text = currVar.toString();
           
            Matcher matcher = pattern.matcher(text);
            int groupCount = matcher.groupCount();
           
            StringBuffer buffer = new StringBuffer();
           
            int index = 0;
            while ( matcher.find() ) {
              index++;

              // if index exceeds maximum number of matching sequences exists the loop
                if (maxLoops < index) {
                    break;
                }

              for (int i = 0; i <= groupCount; i++) {
                context.put("_"+i, new NodeVariable(matcher.group(i)));
              }

                BaseElementDef resultDef = regexpDef.getRegexpResultDef();
                Variable result = getBodyTextContent(resultDef, scraper, context, true);
                debug(resultDef, scraper, result);
               
                String currResult = (result == null) ? matcher.group(0) : result.toString();
              if (isReplace) {
                matcher.appendReplacement(buffer, currResult);
              } else {
                resultList.add(new NodeVariable(currResult));
              }
View Full Code Here

        super(scriptDef);
        this.scriptDef = scriptDef;
    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        Variable scriptText = getBodyTextContent(scriptDef, scraper, context);

        String language = BaseTemplater.execute( scriptDef.getLanguage(), scraper.getScriptEngine());
        if (language != null) {
            language = language.toLowerCase();
        }

        String returnExpression = scriptDef.getReturnExpression();

        ScriptEngine scriptEngine = language == null ? scraper.getScriptEngine() : scraper.getScriptEngine(language);
        scriptEngine.eval( scriptText.toString() );

        if (returnExpression != null) {
            String returnExpressionEvaluated = BaseTemplater.execute( scriptDef.getReturnExpression(), scraper.getScriptEngine());
            Object returnValue = scriptEngine.eval(returnExpressionEvaluated);
            return CommonUtil.createVariable(returnValue);
View Full Code Here

        this.callParamDef = callParamDef;
    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        String name = BaseTemplater.execute( callParamDef.getName(), scraper.getScriptEngine() );
      Variable variable =  new BodyProcessor(callParamDef).execute(scraper, context);

        scraper.addFunctionParam(name, variable);
        this.setProperty("Name", name);

      return variable;
View Full Code Here

        this.xsltDef = xsltDef;
    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        BaseElementDef xsltElementDef = xsltDef.getXmlDef();
        Variable xmlStr = getBodyTextContent(xsltElementDef, scraper, context, true);
        debug(xsltElementDef, scraper, xmlStr);

        BaseElementDef stylesheetElementDef = xsltDef.getStylesheetDef();
        Variable stylesheetStr = getBodyTextContent(stylesheetElementDef, scraper, context, true);
        debug(stylesheetElementDef, scraper, stylesheetStr);
     
        try {
        TransformerFactory xformFactory = TransformerFactory.newInstance();
        Source xsl = new StreamSource( new StringReader(stylesheetStr.toString()) );
        Transformer stylesheet = xformFactory.newTransformer(xsl);
        Source request  = new StreamSource( new StringReader(xmlStr.toString()) );
        StringWriter writer = new StringWriter();
        Result response = new StreamResult(writer);
        stylesheet.transform(request, response);
View Full Code Here

    }

    public Variable execute(Scraper scraper, ScraperContext context) {
        try {
            BaseElementDef tryBodyDef = tryDef.getTryBodyDef();
            Variable result = new BodyProcessor(tryBodyDef).run(scraper, context);
            debug(tryBodyDef, scraper, result);

            return result;
        } catch(BaseException e) {
            if ( scraper.getLogger().isInfoEnabled() ) {
                scraper.getLogger().info("Exception caught with try processor: " + e.getMessage());
            }
            BaseElementDef catchValueDef = tryDef.getCatchValueDef();
            Variable result = new BodyProcessor(catchValueDef).run(scraper, context);
            debug(catchValueDef, scraper, result);

            return result;
        }
    }
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.