
net.sourceforge.tink.model.impl.TinkResource Maven / Gradle / Ivy
/**
* Copyright 2008,2009 Ivan SZKIBA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package net.sourceforge.tink.model.impl;
import net.sourceforge.tink.model.Page;
import net.sourceforge.tink.model.TinkContext;
import net.sourceforge.tink.model.TinkException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathFactory;
public class TinkResource extends AbstractTinkComponent
{
private XPath xpath;
public List findResources(Page page) throws TinkException
{
Document doc = page.getDocument();
List refs = new ArrayList();
try
{
findReferences(refs, doc, "//img", "src");
findReferences(refs, doc, "//script", "src");
findReferences(refs, doc, "//link", "href");
}
catch (XPathException x)
{
throw new TinkException(x);
}
// TODO complete this list, add all element with URI attributes....
return refs;
}
@Override public void init(TinkContext ctx) throws TinkException
{
super.init(ctx);
xpath = XPathFactory.newInstance().newXPath();
}
private void findReferences(List refs, Document doc, String expr, String attr) throws XPathException
{
// TODO investigate script without src problem !
NodeList nodes = (NodeList) xpath.evaluate(expr, doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++)
{
Element element = (Element) nodes.item(i);
if (element.hasAttribute(attr))
{
try
{
refs.add(new URI(element.getAttribute(attr)));
}
catch (URISyntaxException x)
{
out.error(x, "Invalid attribute value. %s=%s", attr, element.getAttribute(attr));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy