@Inject public AbsolutePathReferenceRewriter() { super(new AbsolutePathReferenceVisitor(AbsolutePathReferenceVisitor.Tags.RESOURCES)); } }
/** * Returns the base uri of the given document. * Base uri is specified as <base href="..."> * @param doc The document. * @return Base uri of the document. */ @VisibleForTesting Uri getBaseUri(Document doc) { String baseHref = getBaseHref(doc); if (baseHref != null) { try { return Uri.parse(baseHref); } catch (Uri.UriException e) { // Ignore. } } return null; }
/** * Returns the uri to resolve any relative url on the current page to. * This is equal to the base uri (in case the page has one) or the current * page uri. * @param gadget The gadget (container for page) being processed. * @param node The current node being processed. * @return The uri to resolve non absolute uri's relative to. */ private Uri getBaseResolutionUri(Gadget gadget, Node node) { Uri pageUri = gadget.getSpec().getUrl(); Uri baseUri = getBaseUri(node.getOwnerDocument()); return baseUri != null ? baseUri : pageUri; }
@Test public void testGetBaseUri() throws Exception { Element baseTag1 = elem("base", "href", "http://www.example1.org"); Element baseTag2 = elem("base", "href", "http://www.example2.org"); Element img = elem("img", "src", RELATIVE_URI.toString()); Element a = elem("a", "href", RELATIVE_URI.toString()); Node[] headNodes = { baseTag1 }; Element html = htmlDoc(headNodes, baseTag2, img, a); AbsolutePathReferenceVisitor visitor = visitorForAllTags(); assertEquals("http://www.example1.org", visitor.getBaseHref(html.getOwnerDocument())); assertEquals("http://www.example1.org", visitor.getBaseUri(html.getOwnerDocument()).toString()); }
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException { Attr nodeAttr = getUriAttributeFromNode(node, tagsToMakeAbsolute); if (nodeAttr != null) { try { Uri nodeUri = Uri.parse(nodeAttr.getValue()); Uri baseUri = getBaseResolutionUri(gadget, node); Uri resolved = baseUri.resolve(nodeUri); if (!resolved.equals(nodeUri)) { nodeAttr.setValue(resolved.toString()); return VisitStatus.MODIFY; } } catch (Uri.UriException e) { // UriException on illegal input. Ignore. } } return VisitStatus.BYPASS; }
@Test public void revisitDoesNothing() throws Exception { assertFalse(visitorForAllTags().revisit(gadget(), null)); }
private VisitStatus getVisitStatus(Node node) throws Exception { return visitorForAllTags().visit(gadget(), node); } }
@Test public void testGetBaseUri() throws Exception { Element baseTag1 = elem("base", "href", "http://www.example1.org"); Element baseTag2 = elem("base", "href", "http://www.example2.org"); Element img = elem("img", "src", RELATIVE_URI.toString()); Element a = elem("a", "href", RELATIVE_URI.toString()); Node[] headNodes = { baseTag1 }; Element html = htmlDoc(headNodes, baseTag2, img, a); AbsolutePathReferenceVisitor visitor = visitorForAllTags(); assertEquals("http://www.example1.org", visitor.getBaseHref(html.getOwnerDocument())); assertEquals("http://www.example1.org", visitor.getBaseUri(html.getOwnerDocument()).toString()); }
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException { Attr nodeAttr = getUriAttributeFromNode(node, tagsToMakeAbsolute); if (nodeAttr != null) { try { Uri nodeUri = Uri.parse(nodeAttr.getValue()); Uri baseUri = getBaseResolutionUri(gadget, node); Uri resolved = baseUri.resolve(nodeUri); if (!resolved.equals(nodeUri)) { nodeAttr.setValue(resolved.toString()); return VisitStatus.MODIFY; } } catch (Uri.UriException e) { // UriException on illegal input. Ignore. } } return VisitStatus.BYPASS; }
@Test public void revisitDoesNothing() throws Exception { assertFalse(visitorForAllTags().revisit(gadget(), null)); }
private VisitStatus getVisitStatus(Node node) throws Exception { return visitorForAllTags().visit(gadget(), node); } }
@Inject public AbsolutePathReferenceRewriter() { super(new AbsolutePathReferenceVisitor(AbsolutePathReferenceVisitor.Tags.RESOURCES)); } }
@Test public void testGetBaseUri() throws Exception { Element baseTag1 = elem("base", "href", "http://www.example1.org"); Element baseTag2 = elem("base", "href", "http://www.example2.org"); Element img = elem("img", "src", RELATIVE_URI.toString()); Element a = elem("a", "href", RELATIVE_URI.toString()); Node[] headNodes = { baseTag1 }; Element html = htmlDoc(headNodes, baseTag2, img, a); AbsolutePathReferenceVisitor visitor = visitorForAllTags(); assertEquals("http://www.example1.org", visitor.getBaseHref(html.getOwnerDocument())); assertEquals("http://www.example1.org", visitor.getBaseUri(html.getOwnerDocument()).toString()); }
/** * Returns the uri to resolve any relative url on the current page to. * This is equal to the base uri (in case the page has one) or the current * page uri. * @param gadget The gadget (container for page) being processed. * @param node The current node being processed. * @return The uri to resolve non absolute uri's relative to. */ private Uri getBaseResolutionUri(Gadget gadget, Node node) { Uri pageUri = gadget.getSpec().getUrl(); Uri baseUri = getBaseUri(node.getOwnerDocument()); return baseUri != null ? baseUri : pageUri; }
public VisitStatus visit(Gadget gadget, Node node) throws RewritingException { Attr nodeAttr = getUriAttributeFromNode(node, tagsToMakeAbsolute); if (nodeAttr != null) { try { Uri nodeUri = Uri.parse(nodeAttr.getValue()); Uri baseUri = getBaseResolutionUri(gadget, node); Uri resolved = baseUri.resolve(nodeUri); if (!resolved.equals(nodeUri)) { nodeAttr.setValue(resolved.toString()); return VisitStatus.MODIFY; } } catch (Uri.UriException e) { // UriException on illegal input. Ignore. } } return VisitStatus.BYPASS; }
/** * Returns the base uri of the given document. * Base uri is specified as <base href="..."> * @param doc The document. * @return Base uri of the document. */ @VisibleForTesting Uri getBaseUri(Document doc) { String baseHref = getBaseHref(doc); if (baseHref != null) { try { return Uri.parse(baseHref); } catch (Uri.UriException e) { // Ignore. } } return null; }
@Test public void revisitDoesNothing() throws Exception { assertFalse(visitorForAllTags().revisit(gadget(), null)); }
private VisitStatus getVisitStatus(Node node) throws Exception { return visitorForAllTags().visit(gadget(), node); } }
@Override protected List<DomWalker.Visitor> makeVisitors(Gadget context, Uri gadgetUri) { return ImmutableList.<DomWalker.Visitor>of(new AbsolutePathReferenceVisitor(tags)); } }
/** * Returns the uri to resolve any relative url on the current page to. * This is equal to the base uri (in case the page has one) or the current * page uri. * @param gadget The gadget (container for page) being processed. * @param node The current node being processed. * @return The uri to resolve non absolute uri's relative to. */ private Uri getBaseResolutionUri(Gadget gadget, Node node) { if (baseUri == null) { Uri pageUri = gadget.getSpec().getUrl(); Uri baseTagUri = getBaseUri(node.getOwnerDocument()); baseUri = baseTagUri != null ? baseTagUri : pageUri; } return baseUri; }