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);
}
}
Aggregations