Search in sources :

Example 1 with LinkCollector

use of org.apache.wiki.LinkCollector in project jspwiki by apache.

the class JSPWikiMarkupParserTest method testCollectingLinksAttachment.

@Test
public void testCollectingLinksAttachment() throws Exception {
    try {
        testEngine.saveText(PAGE_NAME, "content");
        Attachment att = new Attachment(testEngine, PAGE_NAME, "TestAtt.txt");
        att.setAuthor("FirstPost");
        testEngine.getAttachmentManager().storeAttachment(att, testEngine.makeAttachmentFile());
        LinkCollector coll = new LinkCollector();
        LinkCollector coll_others = new LinkCollector();
        String src = "[TestAtt.txt]";
        WikiContext context = new WikiContext(testEngine, new WikiPage(testEngine, PAGE_NAME));
        MarkupParser p = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
        p.addLocalLinkHook(coll_others);
        p.addExternalLinkHook(coll_others);
        p.addAttachmentLinkHook(coll);
        p.parse();
        Collection<String> links = coll.getLinks();
        Assert.assertEquals("no links found", 1, links.size());
        Assert.assertEquals("wrong link", PAGE_NAME + "/TestAtt.txt", links.iterator().next());
        Assert.assertEquals("wrong links found", 0, coll_others.getLinks().size());
    } finally {
        String files = testEngine.getWikiProperties().getProperty(BasicAttachmentProvider.PROP_STORAGEDIR);
        File storagedir = new File(files, PAGE_NAME + BasicAttachmentProvider.DIR_EXTENSION);
        if (storagedir.exists() && storagedir.isDirectory())
            TestEngine.deleteAll(storagedir);
    }
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiPage(org.apache.wiki.WikiPage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Attachment(org.apache.wiki.attachment.Attachment) LinkCollector(org.apache.wiki.LinkCollector) File(java.io.File) Test(org.junit.Test)

Example 2 with LinkCollector

use of org.apache.wiki.LinkCollector in project jspwiki by apache.

the class JSPWikiMarkupParserTest method testCollectingLinks2.

@Test
public void testCollectingLinks2() throws Exception {
    LinkCollector coll = new LinkCollector();
    String src = "[" + PAGE_NAME + "/Test.txt]";
    WikiContext context = new WikiContext(testEngine, new WikiPage(testEngine, PAGE_NAME));
    MarkupParser p = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
    p.addLocalLinkHook(coll);
    p.addExternalLinkHook(coll);
    p.addAttachmentLinkHook(coll);
    p.parse();
    Collection<String> links = coll.getLinks();
    Assert.assertEquals("no links found", 1, links.size());
    Assert.assertEquals("wrong link", PAGE_NAME + "/Test.txt", links.iterator().next());
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiPage(org.apache.wiki.WikiPage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) LinkCollector(org.apache.wiki.LinkCollector) Test(org.junit.Test)

Example 3 with LinkCollector

use of org.apache.wiki.LinkCollector in project jspwiki by apache.

the class JSPWikiMarkupParserTest method testCollectingLinks.

/**
 *  Test collection of links.
 */
@Test
public void testCollectingLinks() throws Exception {
    LinkCollector coll = new LinkCollector();
    String src = "[Test]";
    WikiContext context = new WikiContext(testEngine, new WikiPage(testEngine, PAGE_NAME));
    MarkupParser p = new JSPWikiMarkupParser(context, new BufferedReader(new StringReader(src)));
    p.addLocalLinkHook(coll);
    p.addExternalLinkHook(coll);
    p.addAttachmentLinkHook(coll);
    p.parse();
    Collection<String> links = coll.getLinks();
    Assert.assertEquals("no links found", 1, links.size());
    Assert.assertEquals("wrong link", "Test", links.iterator().next());
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiPage(org.apache.wiki.WikiPage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) LinkCollector(org.apache.wiki.LinkCollector) Test(org.junit.Test)

Example 4 with LinkCollector

use of org.apache.wiki.LinkCollector in project jspwiki by apache.

the class RPCHandler method listLinks.

public Vector listLinks(String pagename) throws XmlRpcException {
    pagename = parsePageCheckCondition(pagename);
    WikiPage page = m_engine.getPage(pagename);
    String pagedata = m_engine.getPureText(page);
    LinkCollector localCollector = new LinkCollector();
    LinkCollector extCollector = new LinkCollector();
    LinkCollector attCollector = new LinkCollector();
    WikiContext context = new WikiContext(m_engine, page);
    context.setVariable(WikiEngine.PROP_REFSTYLE, "absolute");
    m_engine.textToHTML(context, pagedata, localCollector, extCollector, attCollector);
    Vector<Hashtable<String, String>> result = new Vector<Hashtable<String, String>>();
    // 
    for (Iterator<String> i = localCollector.getLinks().iterator(); i.hasNext(); ) {
        String link = i.next();
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", toRPCString(link));
        ht.put("type", LINK_LOCAL);
        if (m_engine.pageExists(link)) {
            ht.put("href", context.getURL(WikiContext.VIEW, link));
        } else {
            ht.put("href", context.getURL(WikiContext.EDIT, link));
        }
        result.add(ht);
    }
    // 
    for (Iterator<String> i = attCollector.getLinks().iterator(); i.hasNext(); ) {
        String link = i.next();
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", toRPCString(link));
        ht.put("type", LINK_LOCAL);
        ht.put("href", context.getURL(WikiContext.ATTACH, link));
        result.add(ht);
    }
    for (Iterator<String> i = extCollector.getLinks().iterator(); i.hasNext(); ) {
        String link = i.next();
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", link);
        ht.put("type", LINK_EXTERNAL);
        ht.put("href", link);
        result.add(ht);
    }
    return result;
}
Also used : WikiContext(org.apache.wiki.WikiContext) Hashtable(java.util.Hashtable) WikiPage(org.apache.wiki.WikiPage) LinkCollector(org.apache.wiki.LinkCollector) Vector(java.util.Vector)

Example 5 with LinkCollector

use of org.apache.wiki.LinkCollector in project jspwiki by apache.

the class RPCHandlerUTF8 method listLinks.

public Vector listLinks(String pagename) throws XmlRpcException {
    pagename = parsePageCheckCondition(pagename);
    WikiPage page = m_engine.getPage(pagename);
    String pagedata = m_engine.getPureText(page);
    LinkCollector localCollector = new LinkCollector();
    LinkCollector extCollector = new LinkCollector();
    LinkCollector attCollector = new LinkCollector();
    WikiContext context = new WikiContext(m_engine, page);
    context.setVariable(WikiEngine.PROP_REFSTYLE, "absolute");
    m_engine.textToHTML(context, pagedata, localCollector, extCollector, attCollector);
    Vector<Hashtable<String, String>> result = new Vector<Hashtable<String, String>>();
    // 
    for (Iterator<String> i = localCollector.getLinks().iterator(); i.hasNext(); ) {
        String link = i.next();
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", link);
        ht.put("type", LINK_LOCAL);
        if (m_engine.pageExists(link)) {
            ht.put("href", context.getViewURL(link));
        } else {
            ht.put("href", context.getURL(WikiContext.EDIT, link));
        }
        result.add(ht);
    }
    // 
    for (String link : attCollector.getLinks()) {
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", link);
        ht.put("type", LINK_LOCAL);
        ht.put("href", context.getURL(WikiContext.ATTACH, link));
        result.add(ht);
    }
    for (String link : extCollector.getLinks()) {
        Hashtable<String, String> ht = new Hashtable<String, String>();
        ht.put("page", link);
        ht.put("type", LINK_EXTERNAL);
        ht.put("href", link);
        result.add(ht);
    }
    return result;
}
Also used : WikiContext(org.apache.wiki.WikiContext) Hashtable(java.util.Hashtable) WikiPage(org.apache.wiki.WikiPage) LinkCollector(org.apache.wiki.LinkCollector) Vector(java.util.Vector)

Aggregations

LinkCollector (org.apache.wiki.LinkCollector)5 WikiContext (org.apache.wiki.WikiContext)5 WikiPage (org.apache.wiki.WikiPage)5 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 Test (org.junit.Test)3 Hashtable (java.util.Hashtable)2 Vector (java.util.Vector)2 File (java.io.File)1 Attachment (org.apache.wiki.attachment.Attachment)1