Search in sources :

Example 1 with FeedHandler

use of lucee.runtime.text.feed.FeedHandler in project Lucee by lucee.

the class Feed method doActionRead.

private void doActionRead() throws IOException, SAXException, PageException {
    required("Feed", "read", "source", source);
    if (outputFile != null && outputFile.exists() && !overwrite)
        throw new ApplicationException("outputFile file [" + outputFile + "] already exists");
    String charset = null;
    // xmlVar
    if (outputFile != null) {
        IOUtil.copy(source, outputFile);
    }
    // outputFile
    String strFeed = null;
    if (!StringUtil.isEmpty(xmlVar)) {
        strFeed = IOUtil.toString(outputFile != null ? outputFile : source, charset);
        pageContext.setVariable(xmlVar, strFeed);
    }
    // Input Source
    InputSource is = null;
    Reader r = null;
    if (strFeed != null)
        is = new InputSource(new StringReader(strFeed));
    else if (outputFile != null)
        is = new InputSource(r = IOUtil.getReader(outputFile, charset));
    else
        is = new InputSource(r = IOUtil.getReader(source, charset));
    is.setSystemId(source.getPath());
    try {
        FeedHandler feed = new FeedHandler(source);
        Struct data = feed.getData();
        // properties
        if (properties != null) {
            String strProp = Caster.toString(properties, null);
            if (strProp == null)
                throw new ApplicationException("attribute [properties] should be of type string");
            pageContext.setVariable(strProp, FeedProperties.toProperties(data));
        }
        // query or enclosure
        lucee.runtime.type.Query qry = null;
        if (query != null || enclosureDir != null) {
            qry = FeedQuery.toQuery(data, feed.hasDC());
        }
        // query
        if (query != null) {
            String strQuery = Caster.toString(query, null);
            if (strQuery == null)
                throw new ApplicationException("attribute [query] should be of type string");
            pageContext.setVariable(strQuery, qry);
        }
        if (enclosureDir != null) {
            int rows = qry.getRowCount();
            String strUrl = null;
            Resource src, dest;
            for (int row = 1; row <= rows; row++) {
                strUrl = Caster.toString(qry.getAt(FeedQuery.LINKHREF, row, null), null);
                if (!StringUtil.isEmpty(strUrl)) {
                    src = ResourceUtil.toResourceNotExisting(pageContext, strUrl);
                    dest = enclosureDir.getRealResource(src.getName());
                    if (!ignoreEnclosureError && !overwriteEnclosure && dest.exists())
                        throw new ApplicationException("enclosure file [" + dest + "] already exists");
                    try {
                        IOUtil.copy(src, dest);
                    } catch (IOException ioe) {
                        if (!ignoreEnclosureError)
                            throw ioe;
                    }
                }
            }
        }
        // name
        if (name != null) {
            String strName = Caster.toString(name, null);
            if (strName == null)
                throw new ApplicationException("attribute [name] should be of type string");
            pageContext.setVariable(strName, data);
        }
    } finally {
        IOUtil.closeEL(r);
    }
}
Also used : InputSource(org.xml.sax.InputSource) HTTPResource(lucee.commons.io.res.type.http.HTTPResource) Resource(lucee.commons.io.res.Resource) Reader(java.io.Reader) StringReader(java.io.StringReader) GetHttpTimeString(lucee.runtime.functions.dateTime.GetHttpTimeString) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) ApplicationException(lucee.runtime.exp.ApplicationException) FeedHandler(lucee.runtime.text.feed.FeedHandler) StringReader(java.io.StringReader) Query(lucee.runtime.type.Query)

Aggregations

IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Resource (lucee.commons.io.res.Resource)1 HTTPResource (lucee.commons.io.res.type.http.HTTPResource)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 GetHttpTimeString (lucee.runtime.functions.dateTime.GetHttpTimeString)1 FeedHandler (lucee.runtime.text.feed.FeedHandler)1 Query (lucee.runtime.type.Query)1 Struct (lucee.runtime.type.Struct)1 InputSource (org.xml.sax.InputSource)1