Package org.webharvest.runtime.variables

Examples of org.webharvest.runtime.variables.ListVariable


     * @throws XPathException
     */
    public static ListVariable createListOfXmlNodes(XQueryExpression exp, DynamicQueryContext dynamicContext) throws XPathException {
        final SequenceIterator iter = exp.iterator(dynamicContext);

        ListVariable listVariable = new ListVariable();
        while (true) {
            Item item = iter.next();
            if (item == null) {
                break;
            }

            XmlNodeWrapper value = new XmlNodeWrapper(item);
            listVariable.addVariable( new NodeVariable(value) );
        }

        return listVariable;
    }
View Full Code Here


            return (Variable) value;
        } else if (value == null) {
            return new EmptyVariable();
        } else if (value instanceof Collection) {
            Collection collection = (Collection) value;
            return new ListVariable(new ArrayList(collection));
        } else if (value instanceof Object[]) {
            List list = Arrays.asList( (Object[]) value );
            return new ListVariable(list);
        } else {
            return new NodeVariable(value);
        }
    }
View Full Code Here

            resultList.add(new NodeVariable(buffer.toString()));
          }
        }
       

        return new ListVariable(resultList);
    }
View Full Code Here

            statement.execute();
            ResultSet resultSet = statement.getResultSet();

            if (resultSet != null) {
                ResultSetMetaData metadata = resultSet.getMetaData();
                ListVariable queryResult = new ListVariable();
                int columnCount = metadata.getColumnCount();
                DbColumnDescription colDescs[] = new DbColumnDescription[columnCount];
                for (int i = 1; i <= columnCount; i++) {
                    String colName = metadata.getColumnLabel(i);
                    int colType = metadata.getColumnType(i);
                    colDescs[i - 1] = new DbColumnDescription(colName, colType);
                }

                int rowCount = 0;
                while ( resultSet.next() && (maxRows < 0 || rowCount < maxRows) ) {
                    Object rowData[] = new Object[columnCount];
                    for (int i = 0; i < columnCount; i++) {
                        switch (colDescs[i].getType()) {
                            case Types.FLOAT:
                                rowData[i] = resultSet.getFloat(i + 1); break;
                            case Types.DOUBLE:
                            case Types.DECIMAL:
                            case Types.NUMERIC:
                            case Types.REAL:
                                rowData[i] = resultSet.getDouble(i + 1); break;
                            case Types.SMALLINT:
                            case Types.INTEGER:
                            case Types.TINYINT:
                                rowData[i] = resultSet.getInt(i + 1); break;
                            case Types.BLOB:
                            case Types.BINARY:
                            case Types.VARBINARY:
                            case Types.LONGVARBINARY:
                                rowData[i] = resultSet.getBytes(i + 1); break;
                            default:
                                rowData[i] = resultSet.getString(i + 1); break;
                        }
                    }

                    queryResult.addVariable( new DbRowVariable(colDescs, rowData) );
                    rowCount++;
                }
                return queryResult;
            } else {
                return new EmptyVariable();
View Full Code Here

            if (index != null && indexBeforeLoop != null) {
                context.put(index, indexBeforeLoop);
            }
        }

        return isEmpty ? new EmptyVariable() : new ListVariable(resultList);
    }
View Full Code Here

                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) );
                    }
View Full Code Here

    @Override
    public Variable executePlugin(Scraper scraper, ScraperContext context) {

        IElementDef[] defs = elementDef.getOperationDefs();
        ListVariable result = new ListVariable();

        String transformError = "Error Transforming document";

        if (defs.length > 0) {

            try {
                Class userAgentInterfaceClass = Class.forName("org.lobobrowser.html.UserAgentContext");
                Class userAgentContextClass = Class.forName("org.lobobrowser.html.test.SimpleUserAgentContext");
                Object userAgentContext = userAgentContextClass.newInstance();

                Class documentBuilderClass = Class.forName("org.lobobrowser.html.parser.DocumentBuilderImpl");
                Constructor documentBuilderConstructor = documentBuilderClass.getConstructor(new Class[]{userAgentInterfaceClass});
                Object documentBuilder = documentBuilderConstructor.newInstance(new Object[]{userAgentContext});

                Class inputSourceClass = Class.forName("org.lobobrowser.html.parser.InputSourceImpl");

                Constructor inputSourceConstructor = inputSourceClass.getConstructor(
                        new Class[]{InputStream.class, String.class, String.class});
                Method documentBuilderParse = documentBuilderClass.getMethod("parse", InputSource.class);

                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();
                        Transformer transformer = factory.newTransformer();
                        transformer.transform(source, domResult);
                        result.addVariable(new NodeVariable(out.toByteArray()));
                    } catch (TransformerException e) {
                        log.error(transformError, e);
                        throw new PluginException(e);
                    } catch (IllegalAccessException e) {
                        throw new PluginException(e);
                    } catch (InstantiationException e) {
                        throw new PluginException(e);
                    } catch (Exception e) {
                        if (e instanceof SAXException) {
                            String msg = "Error parsing content retrieved from the url" + documentURI;
                            log.error(msg, e);
                            throw new PluginException(msg, e);
                        } else if(e instanceof IOException) {
                            String msg = "Error retrieving content from the url" + documentURI;
                            log.error(msg, e);
                            throw new PluginException(msg, e);
                        } else if(e instanceof ClassNotFoundException) {
                            log.error(EXCEPTION, e);
                            throw new PluginException(EXCEPTION, e);
                        } else if(e instanceof InvocationTargetException) {
                            log.error(EXCEPTION, e);
                            throw new PluginException(EXCEPTION, e);
                        } else if(e instanceof IllegalAccessException) {
                            log.error(EXCEPTION, e);
                            throw new PluginException(EXCEPTION, e);
                        } else {
                            String msg = "Error occurred with the content of " + documentURI;
                            log.error(msg, e);
                            throw new PluginException(msg, e);
                        }
                    }
                }

            } catch (ClassNotFoundException e) {
                log.error(EXCEPTION, e);
                throw new PluginException(EXCEPTION, e);
            } catch (InstantiationException e) {
                log.error(EXCEPTION, e);
                throw new PluginException(EXCEPTION, e);
            } catch (IllegalAccessException e) {
                log.error(EXCEPTION, e);
                throw new PluginException(EXCEPTION, e);
            } catch (NoSuchMethodException e) {
                log.error(EXCEPTION, e);
                throw new PluginException(EXCEPTION, e);
            } catch (InvocationTargetException e) {
                log.error(EXCEPTION, e);
                throw new PluginException(EXCEPTION, e);
            }

        } else {
            result.addVariable(new NodeVariable(elementDef.getBodyText()));
        }
        return result;
    }
View Full Code Here

TOP

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

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.