/* HtmlLinkIndex.java * * Copyright (c) 2003, Benja Fallenstein * * This file is part of Storm. * * Storm is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Storm is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Storm; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package org.nongnu.storm.util; import org.nongnu.storm.*; import org.nongnu.storm.impl.AsyncSetCollector; import java.io.*; import java.util.*; /** An IndexType of indices which * * */ public class HtmlLinkIndex { public static final String uri = "urn:urn-5:T7uu149pw0Z0ANuQPwkw1oRVfZVn"; public static final IndexType type = new IndexType(); public static class Link { public final BlockId linkFrom; public final String linkText; protected Link(BlockId f, String t) { linkFrom=f; linkText=t; } } protected IndexedPool pool; protected IndexedPool.DB db; protected static class IndexType implements IndexedPool.IndexType { public Set getMappings(Block block) throws IOException { if(!block.getId().getContentType().equals("text/html")) return Collections.EMPTY_SET; Set mappings = new HashSet(); String s = CopyUtil.readString(block.getInputStream()); int i = 0; while(true) { i = s.indexOf("href=\"urn:x-storm:1.0:", i); if(i < 0) break; i += "href=\"".length(); int j = s.indexOf('"', i); if(j < 0) break; String urn = s.substring(i, j); int k = urn.indexOf('#'); if(k > 0) urn = urn.substring(0, k); j = s.indexOf('>', j); if(j < 0) break; k = s.indexOf("", j); if(k < 0) break; String link = s.substring(j+1, k); mappings.add(new IndexedPool.Mapping(block.getId(), urn, link)); } return mappings; } public Object createIndex(IndexedPool pool, IndexedPool.DB db) { return new HtmlLinkIndex(pool, db); } public String getIndexTypeURI() { return uri; } public String getHumanReadableName() { return ("An index of HTML blocks by block ids they link to."); } } public HtmlLinkIndex(IndexedPool pool, IndexedPool.DB db) { this.pool = pool; this.db = db; } public SetCollector getLinksTo(BlockId id) throws IOException { Collector c = db.get(id.getURI()); final AsyncSetCollector result = new AsyncSetCollector(); c.addCollectionListener(new CollectionListener() { public boolean item(Object item) { IndexedPool.Mapping m = (IndexedPool.Mapping)item; result.receive(new Link(m.block, m.value)); return true; } public void finish(boolean timeout) { result.finish(timeout); } }); return result; } }