File:  [mozdev] / bookmarktools / www / documentation.html
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Thu Feb 5 21:20:24 2009 UTC (10 years, 3 months ago) by maik
Branches: MAIN
CVS tags: HEAD
- source code added

    1: <!-- MAIN CONTENT -->
    2: 
    3: <h2>Content:</h2>
    4: <ul>
    5: 	<li><strong>USER GUIDE</strong></li>
    6: 	<li><strong>XPCOM</strong>
    7: 		<ul>
    8: 			<li><a href="#HowToIndex">HowTo use the index component</a></li>
    9: 			<li><a href="#HowToTidy">HowTo use the tidy component</a></li>
   10: 	    </ul>
   11: 	</li>
   12: 	<li><strong>OTHER</strong></li>
   13: </ul>
   14: 
   15: <br />
   16: 
   17: <h2><a name="HowToIndex">HowTo use the index component (uses clucene-core-0.9.20)</a></h2>
   18: <h3>Writing::</h3>
   19: <pre class="brush: js">
   20: 
   21: 	//initialize file object (location: profile directory)
   22: 	var file = Components.classes["@mozilla.org/file/directory_service;1"]
   23: 				.getService(Components.interfaces.nsIProperties)
   24: 				.get("ProfD", Components.interfaces.nsIFile);
   25: 
   26: 	file.append("index"); //folder named 'index'
   27: 	
   28: 	if(!file.exists() || !file.isDirectory()) 
   29: 		file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
   30: 
   31: 	//initialize xpcom
   32: 	var writer = Components.classes["@bookmarktools.mozdev.org/search/indexWriter;1"].createInstance();
   33: 	writer = writer.QueryInterface(Components.interfaces.ICLWriter);
   34: 	
   35: 	//set path
   36: 	writer.setPath(file.path);
   37: 	
   38: 	//create index document
   39: 	var cl_document = new Object();
   40: 	cl_document.primaryKey = "key0";						//this shoud be a unique key
   41: 	cl_document.name = "CLucene Indexer";					//document name
   42: 	cl_document.lastModifiedDate = new Date().getTime();	//date in millis
   43: 	cl_document.content = "Hello World!";					//some content
   44: 	
   45: 	//append document
   46: 	writer.appendDocument(
   47: 		cl_document.primaryKey,
   48: 		cl_document.name,
   49: 		cl_document.lastModifiedDate,
   50: 		cl_document.content
   51: 	);
   52: 	
   53: 	//optimize data
   54: 	writer.optimize();
   55: 	
   56: </pre>
   57: 
   58: <h3>Searching::</h3>
   59: <pre class="brush: js">
   60: 
   61: 	//initialize xpcom
   62: 	var reader = Components.classes["@bookmarktools.mozdev.org/search/indexReader;1"].createInstance();
   63: 	reader = reader.QueryInterface(Components.interfaces.ICLReader);
   64: 	
   65: 	//set path
   66: 	reader.setPath(file.path); //see section 'Writing'
   67: 	
   68: 	if(reader.exists()){ //check for index data
   69: 		
   70: 		var query = "Hell*";
   71: 		var results = Components.classes["@mozilla.org/array;1"]
   72: 						.createInstance(Components.interfaces.nsIMutableArray);
   73: 		reader.search(query, results);
   74: 		
   75: 		var e = results.enumerate();
   76: 		while (e.hasMoreElements()) {
   77: 			var resultNode = e.getNext().QueryInterface(Components.interfaces.IResultNode);
   78: 			
   79: 			//Object: resultNode
   80: 			//-> resultNode.primaryKey
   81: 			//-> resultNode.score
   82: 			alert(resultNode.primaryKey + "\n" + resultNode.score);
   83: 		}
   84: 		
   85: 	}else {
   86: 		//display message or do nothing
   87: 	}
   88: 	
   89: </pre>
   90: 
   91: <h3>Lookup::</h3>
   92: <pre class="brush: js">
   93: 
   94: 	//initialize xpcom
   95: 	var reader = Components.classes["@bookmarktools.mozdev.org/search/indexReader;1"].createInstance();
   96: 	reader = reader.QueryInterface(Components.interfaces.ICLReader);
   97: 	
   98: 	//set path
   99: 	reader.setPath(file.path); //see section 'Writing'
  100: 	
  101: 	var results = Components.classes["@mozilla.org/array;1"]
  102: 					.createInstance(Components.interfaces.nsIMutableArray);
  103: 	reader.lookup(results);
  104: 	
  105: 	var e = results.enumerate();
  106: 	while (e.hasMoreElements()) {
  107: 		var lookupNode = e.getNext().QueryInterface(Components.interfaces.ILookupNode);
  108: 		
  109: 		//Object: lookupNode
  110: 		//-> lookupNode.primaryKey
  111: 		//-> lookupNode.name
  112: 		alert(lookupNode.primaryKey + "\n" + lookupNode.name);
  113: 	}
  114: 
  115: </pre>
  116: 
  117: <h3>Delete documents::</h3>
  118: <pre class="brush: js">
  119: 
  120: 	//initialize xpcom
  121: 	var writer = Components.classes["@bookmarktools.mozdev.org/search/indexWriter;1"].createInstance();
  122: 	writer = writer.QueryInterface(Components.interfaces.ICLWriter);
  123: 	
  124: 	//set path
  125: 	writer.setPath(file.path); //see section 'Writing'
  126: 	
  127: 	//delete documents with index 0, 5 and 2
  128: 	var docs = [0, 5, 2];
  129: 	writer.deleteDocument(docs, docs.length);
  130: 
  131: </pre>
  132: 
  133: <br />
  134: 
  135: <h2><a name="HowToTidy">HowTo use the tidy component (uses ctidy version 060405)</a></h2>
  136: <h3>Cleanup::</h3>
  137: <pre class="brush: js">
  138: 
  139: 	//initialize xpcom
  140: 	var tidy = Components.classes["@bookmarktools.mozdev.org/repair/tidy;1"].createInstance();
  141: 	tidy = tidy.QueryInterface(Components.interfaces.ITidy);
  142: 	
  143: 	var src = "
  144: 	&lt;html&gt;
  145: 		&lt;head&gt;&lt;/head&gt;
  146: 		&lt;body
  147: 			broken body tag & no entity
  148: 		&lt;/body&gt;
  149: 	&lt;/html>";
  150: 	
  151: 	//result contains the repaired html/xhtml/xml source
  152: 	//otherwise a error message returned
  153: 	var result = tidy.cleanup(src);
  154: 	
  155: 	if(result == "Tidy: no output"){
  156: 		alert("error returned");
  157: 	}else {
  158: 		alert(src);
  159: 	}
  160: 	
  161: </pre>
  162: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>