#include "Xpclucene.h" #include "nsLuceneService.h" #include "nsCOMPtr.h" #include "nsLuceneUtils.h" #include "nsIFile.h" #include "nsILocalFile.h" #include "nsNetUtil.h" #include "nsIExtensionManager.h" #include "prtime.h" #include "nsIDOMHTMLDocument.h" #include "nsIDOMHTMLElement.h" #include "nsIDOMNode.h" #include "nsIDOMNodeList.h" #include "nsIDOMNamedNodeMap.h" #include "nsDirectoryServiceUtils.h" #include "nsAppDirectoryServiceDefs.h" #include "nsIObserver.h" /* Implementation file */ NS_IMPL_ISUPPORTS2(nsLuceneService, nsILuceneService, nsIObserver) nsLuceneService::nsLuceneService() { /* member initializers and constructor code */ } nsLuceneService::~nsLuceneService() { /* destructor code */ } NS_IMETHODIMP nsLuceneService::GetInstallLocation(nsIFile** aInstallLocation) { nsresult rv; nsCOMPtr file; nsCOMPtr em(do_GetService("@mozilla.org/extensions/manager;1")); if(em) { nsCOMPtr installLocation; rv = em->GetInstallLocation(NS_LITERAL_STRING("xpclucene@skrul.com"), getter_AddRefs(installLocation)); NS_ENSURE_SUCCESS(rv, rv); rv = installLocation->GetLocation(getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, rv); rv = file->Append(NS_LITERAL_STRING("xpclucene@skrul.com")); NS_ENSURE_SUCCESS(rv, rv); } else { /* * TODO: read this from an attribute */ nsCOMPtr localFile; NS_NewLocalFile( NS_LITERAL_STRING("/home/steve/dev/mozilla/firefox-debug/dist/bin/extensions/xpclucene@skrul.com"), false, getter_AddRefs(localFile) ); file = do_QueryInterface(localFile, &rv); NS_ENSURE_SUCCESS(rv, rv); } *aInstallLocation = file; NS_ADDREF(*aInstallLocation); return NS_OK; } NS_IMETHODIMP nsLuceneService::SetInstallLocation(nsIFile* aInstallLocation) { return NS_ERROR_NOT_IMPLEMENTED; } NS_IMETHODIMP nsLuceneService::IndexLocation(const nsAString& indexName, nsIFile** _retval) { nsresult rv; nsCOMPtr f; rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(f)); if(NS_SUCCEEDED(rv)) { /* * Append the xpclucene dir and index name to the profle path */ rv = f->Append(NS_LITERAL_STRING("xpclucene")); NS_ENSURE_SUCCESS(rv, rv); rv = f->Append(NS_LITERAL_STRING("indexes")); NS_ENSURE_SUCCESS(rv, rv); } else { /* * TODO: read this from an attribute */ nsCOMPtr localFile; NS_NewLocalFile( NS_LITERAL_STRING("/home/steve/dev/mozilla/extensions/xpclucene/test"), false, getter_AddRefs(localFile) ); f = do_QueryInterface(localFile, &rv); NS_ENSURE_SUCCESS(rv, rv); } rv = f->Append(indexName); NS_ENSURE_SUCCESS(rv, rv); /* * Create the directory if it does not exist */ PRBool exists; rv = f->Exists(&exists); NS_ENSURE_SUCCESS(rv, rv); if(!exists) { rv = f->Create(nsIFile::DIRECTORY_TYPE, 0755); NS_ENSURE_SUCCESS(rv, rv); } *_retval = f; NS_ADDREF(*_retval); return NS_OK; } NS_IMETHODIMP nsLuceneService::ParseDateGMT(const nsAString& date, PRInt64* _retval) { return nsLuceneUtils::ParseRFC3339GMT(date, _retval); } NS_IMETHODIMP nsLuceneService::FormatDateGMT(PRInt64 time, nsAString& _retval) { return nsLuceneUtils::FormatRFC3339GMT(time, _retval); } NS_IMETHODIMP nsLuceneService::FormatDateLocal(PRInt64 time, nsAString& _retval) { return nsLuceneUtils::FormatRFC3339Local(time, _retval); } NS_IMETHODIMP nsLuceneService::ParseIndexDateGMT(const nsAString& date, PRInt64* _retval) { return nsLuceneUtils::ParseIndexDateGMT(date, _retval); } NS_IMETHODIMP nsLuceneService::FormatIndexDateGMT(PRInt64 time, nsAString& _retval) { return nsLuceneUtils::FormatIndexDateGMT(time, _retval); } /* * Turn an HTML document into a string by stringing all the text nodes and * alt tags in the BODY together */ /* AString filterHTMLDocument (in nsIDOMHTMLDocument document); */ NS_IMETHODIMP nsLuceneService::FilterHTMLDocument(nsIDOMNode* node, nsAString& _retval) { nsresult rv; if(node) { rv = FilterHTMLDocumentHelper(node, _retval); NS_ENSURE_SUCCESS(rv, rv); } else { _retval.Assign(EmptyString()); } return NS_OK; } nsresult nsLuceneService::FilterHTMLDocumentHelper(nsIDOMNode* node, nsAString& _retval) { nsresult rv; nsCOMPtr nodeList; rv = node->GetChildNodes(getter_AddRefs(nodeList)); NS_ENSURE_SUCCESS(rv, rv); PRUint32 length; rv = nodeList->GetLength(&length); NS_ENSURE_SUCCESS(rv, rv); for(PRUint32 i = 0; i < length; i++) { nsCOMPtr child; rv = nodeList->Item(i, getter_AddRefs(child)); NS_ENSURE_SUCCESS(rv, rv); PRUint16 type; rv = child->GetNodeType(&type); NS_ENSURE_SUCCESS(rv, rv); if(type == nsIDOMNode::TEXT_NODE) { nsAutoString value; rv = child->GetNodeValue(value); NS_ENSURE_SUCCESS(rv, rv); value.CompressWhitespace(true, true); if(!value.IsEmpty()) { _retval.Append(value); _retval.Append(NS_LITERAL_STRING(" ")); } } else { if(type == nsIDOMNode::ELEMENT_NODE) { /* * Include alt and title attributes */ nsCOMPtr attributes; rv = child->GetAttributes(getter_AddRefs(attributes)); NS_ENSURE_SUCCESS(rv, rv); rv = AppendAttributeValue(attributes, NS_LITERAL_STRING("alt"), _retval); NS_ENSURE_SUCCESS(rv, rv); rv = AppendAttributeValue(attributes, NS_LITERAL_STRING("title"), _retval); NS_ENSURE_SUCCESS(rv, rv); /* * Don't process script or type tags */ nsAutoString name; rv = child->GetNodeName(name); NS_ENSURE_SUCCESS(rv, rv); if(!(name.Equals(NS_LITERAL_STRING("SCRIPT")) || name.Equals(NS_LITERAL_STRING("STYLE")) )) { FilterHTMLDocumentHelper(child, _retval); } } } } return NS_OK; } nsresult nsLuceneService::AppendAttributeValue(nsIDOMNamedNodeMap* attributes, const nsAString& name, nsAString& _retval) { nsresult rv; nsCOMPtr attr; rv = attributes->GetNamedItem(name, getter_AddRefs(attr)); NS_ENSURE_SUCCESS(rv, rv); if(attr) { nsAutoString value; rv = attr->GetNodeValue(value); NS_ENSURE_SUCCESS(rv, rv); value.CompressWhitespace(true, true); if(!value.IsEmpty()) { _retval.Append(value); _retval.Append(NS_LITERAL_STRING(" ")); } } return NS_OK; } // nsIObserver NS_IMETHODIMP nsLuceneService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData) { //nsresult rv; return NS_OK; }