Source Code Cross Referenced for BuilderImpl.java in  » Search-Engine » snapper » org » enhydra » snapper » business » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Search Engine » snapper » org.enhydra.snapper.business 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * Milin Radivoj
0003:         * rmilin@gmail.com
0004:         */
0005:        package org.enhydra.snapper.business;
0006:
0007:        import java.io.ByteArrayOutputStream;
0008:        import java.io.File;
0009:        import java.io.IOException;
0010:        import java.io.OutputStream;
0011:
0012:        import java.net.URLEncoder;
0013:        import java.text.ParseException;
0014:        import java.text.SimpleDateFormat;
0015:        import java.util.Date;
0016:        import java.util.Enumeration;
0017:        import java.util.Hashtable;
0018:        import java.util.Properties;
0019:        import java.util.Vector;
0020:
0021:        import org.enhydra.snapper.Snapper;
0022:        import org.enhydra.snapper.SnapperManager;
0023:        import org.enhydra.snapper.api.google.GoogleResultElement;
0024:        import org.enhydra.snapper.api.google.GoogleSearcher;
0025:        import org.enhydra.snapper.api.searching.ResultElement;
0026:        import org.enhydra.snapper.spec.Builder;
0027:        import org.enhydra.snapper.xml.tss.ConfigurationDocument;
0028:        import org.enhydra.snapper.xml.tss.GoogleResultDocument;
0029:        import org.enhydra.snapper.xml.tss.GoogleSearchDocument;
0030:        import org.enhydra.snapper.xml.tss.GoogleSearchSummaryDocument;
0031:        import org.enhydra.snapper.xml.tss.ParameterDocument;
0032:        import org.enhydra.snapper.xml.tss.PathDocument;
0033:        import org.enhydra.snapper.xml.tss.ResultDocument;
0034:        import org.enhydra.snapper.xml.tss.SearchDocument;
0035:        import org.enhydra.snapper.xml.tss.SearchResultDocument;
0036:        import org.enhydra.snapper.xml.tss.SearchSummaryDocument;
0037:        import org.enhydra.snapper.xml.tss.SearchedParametersDocument;
0038:        import org.enhydra.snapper.xml.tss.SiteDocument;
0039:        import org.enhydra.snapper.xml.tss.SummaryDocument;
0040:        import org.enhydra.snapper.xml.tss.ConfigurationDocument.Configuration;
0041:        import org.enhydra.snapper.xml.tss.GoogleResultDocument.GoogleResult;
0042:        import org.enhydra.snapper.xml.tss.GoogleSearchDocument.GoogleSearch;
0043:        import org.enhydra.snapper.xml.tss.GoogleSearchSummaryDocument.GoogleSearchSummary;
0044:        import org.enhydra.snapper.xml.tss.ParameterDocument.Parameter;
0045:        import org.enhydra.snapper.xml.tss.SearchDocument.Search;
0046:        import org.enhydra.snapper.xml.tss.SearchResultDocument.SearchResult;
0047:        import org.enhydra.snapper.xml.tss.SearchedParametersDocument.SearchedParameters;
0048:        import org.enhydra.snapper.xml.tss.SummaryDocument.Summary;
0049:        import org.enhydra.snapper.Log;
0050:        import org.enhydra.snapper.business.SiteListImpl;
0051:        import org.enhydra.snapper.spec.Path;
0052:        import org.enhydra.snapper.spec.Site;
0053:        import org.enhydra.snapper.spec.SiteList;
0054:        import org.enhydra.snapper.spec.SiteListFactory;
0055:
0056:        import com.lutris.logging.Logger;
0057:
0058:        /**
0059:         * @author Milin Radivoj
0060:         */
0061:
0062:        public class BuilderImpl implements  Builder {
0063:
0064:            Properties standardRequestParameters = new Properties();
0065:            Properties siteRequestParameters = new Properties();
0066:
0067:            String searchGoogle = "off";
0068:            Summary result;
0069:
0070:            Mapper mapper = null;
0071:
0072:            public void setStandardRequestParameters(Properties prop) {
0073:                standardRequestParameters = prop;
0074:            }
0075:
0076:            public void setSiteRequestParameters(Properties prop) {
0077:                siteRequestParameters = prop;
0078:            }
0079:
0080:            public void close() {
0081:                try {
0082:                    mapper.close();
0083:                    mapper = null;
0084:                    standardRequestParameters.clear();
0085:                    siteRequestParameters.clear();
0086:                    result = null;
0087:                } catch (Exception ex) {
0088:                }
0089:            }
0090:
0091:            public String getResult() {
0092:                mapper = new Mapper();
0093:                SummaryDocument sd = SummaryDocument.Factory.newInstance();
0094:                result = sd.addNewSummary();
0095:                Configuration one = addConfigurationSection();
0096:                SearchedParameters two = addSearchParameters();
0097:                result.setConfiguration(one);
0098:                result.setSearchedParameters(two);
0099:
0100:                if (searchGoogle.equals("on")
0101:                        && (siteRequestParameters.size() > 0)) {// oba mogu da imaju rezultat
0102:
0103:                    Search free = addSearchResult();
0104:                    result.setSearch(free);
0105:                    GoogleSearch four = addGoogleSearchResult();
0106:                    result.setGoogleSearch(four);
0107:                } else if (searchGoogle.equals("on")) { //nema sajtova za pretragu ide samo google sekcija
0108:                    GoogleSearch four = addGoogleSearchResult();
0109:                    result.setGoogleSearch(four);
0110:
0111:                } else { // mora da imamo neshto pa ce biti ko i do sada kad nishta nije pretrazeno
0112:
0113:                    Search free = addSearchResult();
0114:                    result.setSearch(free);
0115:
0116:                }
0117:
0118:                ByteArrayOutputStream tmpStream = null;
0119:
0120:                try {
0121:
0122:                    tmpStream = new ByteArrayOutputStream();
0123:                    sd.save(tmpStream);
0124:                    return tmpStream.toString("UTF-8");
0125:
0126:                } catch (IOException ex) {
0127:                    Log.logException(ex);
0128:                } finally {
0129:
0130:                    if (tmpStream != null) {
0131:                        try {
0132:                            tmpStream.close();
0133:                        } catch (Exception e) {
0134:                        }
0135:                        tmpStream = null;
0136:                    }
0137:                }
0138:
0139:                return null;
0140:            }
0141:
0142:            public void writeResult(OutputStream os) {
0143:                mapper = new Mapper();
0144:                SummaryDocument sd = SummaryDocument.Factory.newInstance();
0145:                sd.addNewSummary();
0146:                result = sd.getSummary();
0147:
0148:                Configuration one = addConfigurationSection();
0149:                SearchedParameters two = addSearchParameters();
0150:                result.setConfiguration(one);
0151:                result.setSearchedParameters(two);
0152:
0153:                if (searchGoogle.equals("on")
0154:                        && (siteRequestParameters.size() > 0)) {// oba mogu da imaju rezultat
0155:
0156:                    Search free = addSearchResult();
0157:                    result.setSearch(free);
0158:                    GoogleSearch four = addGoogleSearchResult();
0159:                    result.setGoogleSearch(four);
0160:                } else if (searchGoogle.equals("on")) { //nema sajtova za pretragu ide samo google sekcija
0161:                    GoogleSearch four = addGoogleSearchResult();
0162:                    result.setGoogleSearch(four);
0163:
0164:                } else { // mora da imamo neshto pa ce biti ko i dosada kad nishta nije pretrazeno
0165:
0166:                    Search free = addSearchResult();
0167:                    result.setSearch(free);
0168:
0169:                }
0170:
0171:                try {
0172:                    //result.save(os);
0173:                    sd.save(os);
0174:                } catch (IOException ex) {
0175:                    Log.logException(ex);
0176:
0177:                }
0178:
0179:            }
0180:
0181:            private Configuration addConfigurationSection() {
0182:                ConfigurationDocument cd = ConfigurationDocument.Factory
0183:                        .newInstance();
0184:                cd.addNewConfiguration();
0185:                Configuration result = cd.getConfiguration();
0186:                //Hashtable table = mapper.getTable();
0187:
0188:                SiteList sl = null;
0189:                try {
0190:                    sl = SiteListFactory
0191:                            .getSiteList("org.enhydra.snapper.business.SiteListImpl");
0192:                } catch (Exception e1) {
0193:
0194:                    return result;
0195:                }
0196:                Site[] sites = sl.getList();
0197:
0198:                if (sites != null) {
0199:
0200:                    for (int i = 0; i < sites.length; i++) {
0201:                        try {
0202:
0203:                            String siteName = sites[i].getNAME();
0204:                            String indexDir = sites[i].getINDEXDIR();
0205:
0206:                            boolean download = sites[i].getDOWNLOAD();
0207:                            //int size = sites[i].getMAXSIZE();
0208:                            boolean isSearched = sites[i].getSEARCH();
0209:                            //int age = sites[i].getMAXAGE();
0210:
0211:                            int fileNR = sites[i].getFILENR();
0212:                            int queryNR = sites[i].getQUERYNR();
0213:                            String lastQuery = sites[i].getLASTQUERY();
0214:                            String lastUpdate = sites[i].getLASTUPDATE();
0215:
0216:                            SiteDocument ss = SiteDocument.Factory
0217:                                    .newInstance();
0218:                            ss.addNewSite();
0219:                            org.enhydra.snapper.xml.tss.SiteDocument.Site siteSection = ss
0220:                                    .getSite();
0221:
0222:                            siteSection.setName(siteName);
0223:
0224:                            if (!indexDir.equals("")) {
0225:
0226:                                if (Snapper.getFileSeparatorConvention() != null
0227:                                        && Snapper.getFileSeparatorConvention()
0228:                                                .equalsIgnoreCase("unix")) {
0229:                                    indexDir = indexDir.replaceAll("\\\\", "/");
0230:                                }
0231:
0232:                                siteSection.setIndexDir(filter(indexDir));
0233:                            } else {
0234:                                String inDir = filter(Snapper.getIndexDir()
0235:                                        + File.separator + siteName);
0236:                                if (Snapper.getFileSeparatorConvention() != null
0237:                                        && Snapper.getFileSeparatorConvention()
0238:                                                .equalsIgnoreCase("unix")) {
0239:                                    inDir = inDir.replaceAll("\\\\", "/");
0240:                                }
0241:
0242:                                siteSection.setIndexDir(filter(inDir));
0243:                            }
0244:
0245:                            siteSection.setDownload(download);
0246:
0247:                            siteSection.setSearched(isSearched);
0248:
0249:                            siteSection.setFileNR(fileNR);
0250:
0251:                            siteSection.setQueryNR(queryNR);
0252:
0253:                            if (lastQuery != null) {
0254:
0255:                                siteSection.setLastQuery(lastQuery);
0256:
0257:                            } else {
0258:
0259:                                siteSection.setLastQuery("");
0260:                            }
0261:
0262:                            if (lastUpdate != null) {
0263:                                siteSection.setLastUpdate(lastUpdate);
0264:                            } else {
0265:                                siteSection.setLastUpdate("");
0266:
0267:                            }
0268:
0269:                            Path[] pathArray;
0270:                            pathArray = sites[i].getPathList();
0271:
0272:                            if (pathArray != null)
0273:                                for (int num = 0; num < pathArray.length; num++) {
0274:                                    Path currentPath = pathArray[num];
0275:
0276:                                    if (currentPath == null)
0277:                                        continue;
0278:
0279:                                    PathDocument pd = org.enhydra.snapper.xml.tss.PathDocument.Factory
0280:                                            .newInstance();
0281:                                    pd.addNewPath();
0282:                                    org.enhydra.snapper.xml.tss.PathDocument.Path pathXML = pd
0283:                                            .getPath();
0284:
0285:                                    String pathTypeString = currentPath
0286:                                            .getType();
0287:
0288:                                    pathXML.setType(pathTypeString);
0289:
0290:                                    String pathRoot = currentPath.getRoot();
0291:
0292:                                    if (Snapper.getFileSeparatorConvention() != null
0293:                                            && Snapper
0294:                                                    .getFileSeparatorConvention()
0295:                                                    .equalsIgnoreCase("unix")) {
0296:                                        pathRoot = pathRoot.replaceAll("\\\\",
0297:                                                "/");
0298:                                    }
0299:
0300:                                    pathXML.setRoot(filter(pathRoot));
0301:                                    String mappingRoot = currentPath
0302:                                            .getMappingRoot();
0303:
0304:                                    pathXML.setMappingRoot(filter(mappingRoot));
0305:                                    siteSection.addNewPath();
0306:                                    siteSection.setPathArray(num, pathXML);
0307:                                }
0308:
0309:                            result.addNewSite();
0310:                            result.setSiteArray(i, siteSection);
0311:
0312:                        } catch (Exception e) {
0313:                            Log.logException(e);
0314:                            sites = null;
0315:                        }
0316:                    }
0317:                }
0318:                try {
0319:
0320:                    if (SiteListImpl.forAllDocumentGroup != null) {
0321:                        Vector groupNames = (Vector) SiteListImpl.forAllDocumentGroup
0322:                                .getListOfDocumentGroup();
0323:                        if (groupNames != null) {
0324:                            for (int i = 0; i < groupNames.size(); i++) {
0325:                                org.enhydra.snapper.xml.tss.DocumentGroupDocument dgd = org.enhydra.snapper.xml.tss.DocumentGroupDocument.Factory
0326:                                        .newInstance();
0327:                                dgd.addNewDocumentGroup();
0328:                                org.enhydra.snapper.xml.tss.DocumentGroupDocument.DocumentGroup dg1 = dgd
0329:                                        .getDocumentGroup();
0330:                                dg1.setName((String) groupNames.elementAt(i));
0331:                                result.addNewDocumentGroup();
0332:                                result.setDocumentGroupArray(i, dg1);
0333:                            }
0334:                        }
0335:                    }
0336:                } catch (Exception e) {
0337:                    Log.logException(e);
0338:                }
0339:
0340:                return result;
0341:            }
0342:
0343:            private SearchedParameters addSearchParameters() {
0344:
0345:                SearchedParametersDocument spd = SearchedParametersDocument.Factory
0346:                        .newInstance();
0347:                spd.addNewSearchedParameters();
0348:                SearchedParameters result = spd.getSearchedParameters();
0349:                int i = 0;
0350:                for (Enumeration e = standardRequestParameters.propertyNames(); e
0351:                        .hasMoreElements();) {
0352:                    String name = (String) e.nextElement();
0353:                    String value = (String) standardRequestParameters
0354:                            .getProperty(name);
0355:
0356:                    if (name != null && name.equals("searchGoogle"))
0357:                        if (value != null && value.equals("on"))
0358:                            searchGoogle = "on";
0359:
0360:                    ParameterDocument pd = ParameterDocument.Factory
0361:                            .newInstance();
0362:                    pd.addNewParameter();
0363:                    Parameter perameter = pd.getParameter();
0364:
0365:                    perameter.setKey(name);
0366:                    perameter.setValue(filter(value));
0367:                    result.addNewParameter();
0368:                    result.setParameterArray(i, perameter);
0369:                    i++;
0370:                }
0371:                for (Enumeration e = siteRequestParameters.propertyNames(); e
0372:                        .hasMoreElements();) {
0373:                    String name = (String) e.nextElement();
0374:                    String value = (String) siteRequestParameters
0375:                            .getProperty(name);
0376:
0377:                    ParameterDocument pd = ParameterDocument.Factory
0378:                            .newInstance();
0379:                    pd.addNewParameter();
0380:                    Parameter perameter = pd.getParameter();
0381:
0382:                    perameter.setKey(name);
0383:                    perameter.setValue(filter(value));
0384:                    result.addNewParameter();
0385:                    result.setParameterArray(i, perameter);
0386:                    i++;
0387:                }
0388:
0389:                return result;
0390:            }
0391:
0392:            private GoogleSearch addGoogleSearchResult() {
0393:
0394:                GoogleSearchDocument gsd = org.enhydra.snapper.xml.tss.GoogleSearchDocument.Factory
0395:                        .newInstance();
0396:                gsd.addNewGoogleSearch();
0397:                GoogleSearch result = gsd.getGoogleSearch();
0398:
0399:                org.enhydra.snapper.xml.tss.GoogleSearchResultDocument gsrd = org.enhydra.snapper.xml.tss.GoogleSearchResultDocument.Factory
0400:                        .newInstance();
0401:                gsrd.addNewGoogleSearchResult();
0402:                org.enhydra.snapper.xml.tss.GoogleSearchResultDocument.GoogleSearchResult searchResultNew = gsrd
0403:                        .getGoogleSearchResult();
0404:
0405:                GoogleSearcher googleSearcher = null;
0406:
0407:                String term = standardRequestParameters.getProperty("contents");
0408:                String frS = standardRequestParameters.getProperty("from");
0409:                String toS = standardRequestParameters.getProperty("to");
0410:
0411:                int searchFrom = 0;
0412:                int searchTo = 0;
0413:
0414:                if ((frS == null && toS == null)) {
0415:                    searchFrom = 0;
0416:                    searchTo = 10;
0417:                }
0418:
0419:                if ((frS != null && toS == null)) {
0420:                    int fr = (new Integer(frS)).intValue();
0421:                    searchFrom = fr;
0422:                    searchTo = fr + 10;
0423:                }
0424:
0425:                if ((frS == null && toS != null)) {
0426:                    int to = (new Integer(toS)).intValue();
0427:                    searchFrom = to - 10;
0428:                    searchTo = to;
0429:                }
0430:
0431:                if ((frS != null && toS != null)) {
0432:                    searchTo = (new Integer(toS)).intValue();
0433:                    searchFrom = (new Integer(frS)).intValue();
0434:
0435:                    int range = searchTo - searchFrom + 1;
0436:                    int maxRange = 10;
0437:
0438:                    if (maxRange != 0 && range > maxRange) {
0439:                        searchTo = searchFrom + 9;
0440:                    }
0441:                }
0442:
0443:                int resultEnd;
0444:                int resultBegin;
0445:                int totalResultsCount;
0446:                int limit = 0;
0447:                double searchTime;
0448:
0449:                int resultLimit = Snapper.getGoogleResultLimit();
0450:
0451:                try {
0452:
0453:                    try {
0454:                        googleSearcher = SnapperManager.getInstance()
0455:                                .getGoogleSearcherFactory().getSearcher();
0456:                    } catch (Exception e) {
0457:                        Log.logException(e);
0458:                        googleSearcher = SnapperManager.getInstance()
0459:                                .getGoogleSearcherFactory().newSearcher();
0460:                    }
0461:
0462:                    GoogleResultElement[] resultElements = null;
0463:                    org.enhydra.snapper.api.google.GoogleSearchResult googleSearchResult = googleSearcher
0464:                            .doGoogleSearch(term, searchFrom - 1, 10, false,
0465:                                    "", false, "", "", "");
0466:
0467:                    resultBegin = (new Integer(googleSearchResult
0468:                            .getBeginIndex())).intValue();
0469:                    resultEnd = (new Integer(googleSearchResult.getEndIndex()))
0470:                            .intValue();
0471:
0472:                    limit = resultEnd - resultBegin + 1;
0473:
0474:                    if (limit > resultLimit)
0475:                        limit = resultLimit;
0476:
0477:                    totalResultsCount = (new Integer(googleSearchResult
0478:                            .getDocsNumber())).intValue();
0479:                    searchTime = (new Double(googleSearchResult
0480:                            .getSearchedTime())).doubleValue();
0481:                    resultElements = googleSearchResult.getResultElements();
0482:
0483:                    String searchTimeString = (new Double(searchTime))
0484:                            .toString();
0485:
0486:                    GoogleSearchSummaryDocument gsd1 = GoogleSearchSummaryDocument.Factory
0487:                            .newInstance();
0488:                    gsd1.addNewGoogleSearchSummary();
0489:                    GoogleSearchSummary searchSummary = gsd1
0490:                            .getGoogleSearchSummary();
0491:
0492:                    searchSummary.setBeginIndex(resultBegin);
0493:
0494:                    searchSummary.setDocsNumber(totalResultsCount);
0495:
0496:                    searchSummary.setEndIndex(resultBegin + limit - 1);
0497:
0498:                    searchSummary.setSearchedTerm(filter(term));
0499:
0500:                    searchSummary.setSearchedTime(searchTimeString);
0501:
0502:                    result.setGoogleSearchSummary(searchSummary);
0503:
0504:                    for (int i = 0; i < limit; i++) {
0505:                        GoogleResultDocument grd = GoogleResultDocument.Factory
0506:                                .newInstance();
0507:                        grd.addNewGoogleResult();
0508:                        GoogleResult newResult = grd.getGoogleResult();
0509:
0510:                        String contentResult = resultElements[i].getContent();
0511:                        String titleResult = resultElements[i].getTitle();
0512:                        String urlResult = resultElements[i].getURL();
0513:
0514:                        newResult.setTitle(filter(titleResult));
0515:
0516:                        contentResult = replaceAll(contentResult, "\n", " ");
0517:
0518:                        newResult.setContent(filter(contentResult));
0519:
0520:                        newResult.setURL(filter(urlResult));
0521:
0522:                        newResult.setSiteName("Google");
0523:
0524:                        searchResultNew.addNewGoogleResult();
0525:                        searchResultNew.setGoogleResultArray(i, newResult);
0526:                    }
0527:
0528:                    result.setGoogleSearchResult(searchResultNew);
0529:                    mapper = null;
0530:
0531:                } catch (Throwable f) {
0532:                    Log.log("The call to the Google Web APIs failed : "
0533:                            + f.getMessage());
0534:
0535:                    GoogleSearchDocument gsd1 = GoogleSearchDocument.Factory
0536:                            .newInstance();
0537:                    GoogleSearch failResult = gsd1.addNewGoogleSearch();
0538:                    org.enhydra.snapper.xml.tss.GoogleSearchResultDocument.GoogleSearchResult failSearchResultNew = failResult
0539:                            .addNewGoogleSearchResult();
0540:                    org.enhydra.snapper.xml.tss.GoogleSearchSummaryDocument.GoogleSearchSummary failSummary = failResult
0541:                            .addNewGoogleSearchSummary();
0542:
0543:                    failSummary.setBeginIndex(0);
0544:
0545:                    failSummary.setDocsNumber(0);
0546:
0547:                    failSummary.setEndIndex(0);
0548:
0549:                    failSummary.setSearchedTerm(filter(term));
0550:
0551:                    failSummary.setSearchedTime("0");
0552:
0553:                    GoogleResultDocument.GoogleResult newResult = failSearchResultNew
0554:                            .addNewGoogleResult();
0555:
0556:                    newResult
0557:                            .setContent("The call to the Google Web failed, try refresh button or try again latter!");
0558:
0559:                    newResult.setURL("");
0560:
0561:                    newResult.setTitle("");
0562:
0563:                    newResult.setSiteName("Google");
0564:
0565:                    return failResult;
0566:
0567:                }
0568:                return result;
0569:            }
0570:
0571:            private Search addSearchResult() {
0572:
0573:                org.enhydra.snapper.api.searching.SearchResult searceherResult = null;
0574:
0575:                SearcherBaseImpl searcher = new SearcherBaseImpl();
0576:
0577:                try {
0578:
0579:                    searcher.setUpSearcher(siteRequestParameters, mapper);
0580:                    searcher.setUpSearcherQuery(standardRequestParameters);
0581:
0582:                    try {
0583:                        searcher.searchDocs();
0584:                    } catch (IOException e) {
0585:
0586:                        if (searcher != null) {
0587:                            searcher.close();
0588:                            searcher = null;
0589:                        }
0590:
0591:                        mapper = null;
0592:
0593:                        SearchDocument sd = SearchDocument.Factory
0594:                                .newInstance();
0595:
0596:                        SearchDocument.Search searchO = sd.addNewSearch();
0597:
0598:                        SearchResult searchResultNew = searchO
0599:                                .addNewSearchResult();
0600:
0601:                        SearchSummaryDocument.SearchSummary searchSummaryO = searchO
0602:                                .addNewSearchSummary();
0603:
0604:                        searchSummaryO.setBeginIndex(0);
0605:
0606:                        searchSummaryO.setDocsNumber(0);
0607:
0608:                        searchSummaryO.setEndIndex(0);
0609:
0610:                        searchSummaryO.setSearchedTerm("");
0611:
0612:                        searchSummaryO.setSearchedTime("0");
0613:
0614:                        ResultDocument.Result newResult1 = searchResultNew
0615:                                .addNewResult();
0616:
0617:                        newResult1.setContent(e.getMessage());
0618:
0619:                        return searchO;
0620:                    }
0621:
0622:                    int docsNumber = searcher.getSearchedDocsNumber();
0623:
0624:                    String frS = standardRequestParameters.getProperty("from");
0625:                    String toS = standardRequestParameters.getProperty("to");
0626:
0627:                    if ((frS == null && toS == null))
0628:                        searceherResult = searcher.getSearchResult(1, 10);
0629:
0630:                    if ((frS != null && toS == null)) {
0631:                        int fr = (new Integer(frS)).intValue();
0632:                        searceherResult = searcher.getSearchResult(fr, fr + 9);
0633:                    }
0634:
0635:                    if ((frS == null && toS != null)) {
0636:                        int to = (new Integer(toS)).intValue();
0637:                        searceherResult = searcher.getSearchResult(to - 9, to);
0638:                    }
0639:
0640:                    if ((frS != null && toS != null)) {
0641:                        int to = (new Integer(toS)).intValue();
0642:                        int fr = (new Integer(frS)).intValue();
0643:
0644:                        int range = to - fr;
0645:                        int maxRange = Snapper.getMaxRange();
0646:
0647:                        if (maxRange != 0 && range > maxRange) {
0648:                            to = fr + maxRange - 1;
0649:                        }
0650:                        searceherResult = searcher.getSearchResult(fr, to);
0651:                    }
0652:
0653:                    ResultElement re[] = searceherResult.getResultElements();
0654:
0655:                    SearchDocument sd = SearchDocument.Factory.newInstance();
0656:                    sd.addNewSearch();
0657:                    SearchDocument.Search searchO = sd.getSearch();
0658:
0659:                    SearchResultDocument srn = SearchResultDocument.Factory
0660:                            .newInstance();
0661:                    srn.addNewSearchResult();
0662:                    SearchResult searchResultNew = srn.getSearchResult();
0663:
0664:                    SearchSummaryDocument sro = SearchSummaryDocument.Factory
0665:                            .newInstance();
0666:                    sro.addNewSearchSummary();
0667:                    SearchSummaryDocument.SearchSummary searchSummaryO = sro
0668:                            .getSearchSummary();
0669:
0670:                    searchSummaryO.setBeginIndex(Integer
0671:                            .parseInt(searceherResult.getBeginIndex()));
0672:
0673:                    searchSummaryO.setDocsNumber(Integer
0674:                            .parseInt(searceherResult.getDocsNumber()));
0675:
0676:                    searchSummaryO.setEndIndex(Integer.parseInt(searceherResult
0677:                            .getEndIndex()));
0678:
0679:                    searchSummaryO.setSearchedTerm(searceherResult
0680:                            .getSearchedTerm());
0681:
0682:                    searchSummaryO.setSearchedTime(searceherResult
0683:                            .getSearchedTime());
0684:
0685:                    searchO.setSearchSummary(searchSummaryO);
0686:
0687:                    String metaURL = Snapper.getMetaURL();
0688:
0689:                    if (re != null)
0690:                        for (int i = 0; i < re.length; i++) {
0691:                            String absolutFileName = re[i].getAbsolutFileName();
0692:                            String absolutFilePath = re[i].getAbsolutFilePath();
0693:                            String content = re[i].getContent();
0694:                            String fileType = re[i].getFileType();
0695:                            String luceneID = re[i].getLuceneID();
0696:                            String metaData = re[i].getMetaData();
0697:                            long modified = re[i].getModified();
0698:
0699:                            long created = re[i].getCreated();
0700:                            long accessed = re[i].getAccessed();
0701:
0702:                            String owner = re[i].getOwner();
0703:
0704:                            long size = re[i].getSize();
0705:
0706:                            String properties = re[i].getProperties();
0707:                            String title = re[i].getTitle();
0708:                            String siteName = re[i].getSiteName();
0709:                            String score = re[i].getScore();
0710:
0711:                            long sentDate = re[i].getMailSentDate();
0712:                            long receivedDate = re[i].getMailReceivedDate();
0713:
0714:                            String author = re[i].getAuthor();
0715:                            String lastSavedBy = re[i].getLastSavedBy();
0716:
0717:                            String mailFrom = re[i].getFrom();
0718:                            String mailTo = re[i].getTo();
0719:                            String mailCC = re[i].getCC();
0720:                            String mailBCC = re[i].getBCC();
0721:                            String mailSubject = re[i].getSubject();
0722:
0723:                            String full = absolutFilePath;
0724:
0725:                            ResultDocument nrr = ResultDocument.Factory
0726:                                    .newInstance();
0727:                            nrr.addNewResult();
0728:                            ResultDocument.Result newResult = nrr.getResult();
0729:
0730:                            if (Snapper.getPreviewURL() != null
0731:                                    && !Snapper.getPreviewURL().equals("")) {
0732:                                newResult.setPreviewURL(Snapper.getPreviewURL()
0733:                                        + "&id="
0734:                                        + luceneID
0735:                                        + "&amp;siteName="
0736:                                        + URLEncoder.encode(siteName, "UTF-8")
0737:                                        + "&amp;query="
0738:                                        + URLEncoder.encode(searceherResult
0739:                                                .getSearchedTerm(), "UTF-8"));
0740:                            }
0741:
0742:                            String insideConteinerFilePath = mapper
0743:                                    .getInsideContainerFilePath(full);
0744:                            if (insideConteinerFilePath != null) {
0745:                                int indexOf = full.indexOf('|');
0746:                                full = full.substring(0, indexOf);
0747:                            }
0748:
0749:                            newResult.setSiteName(filter(siteName));
0750:
0751:                            newResult.setTitle(filter(title));
0752:
0753:                            newResult.setFileType(fileType);
0754:
0755:                            int indexOF = absolutFileName.lastIndexOf(".");
0756:                            if (indexOF > 0)
0757:                                newResult.setFileName(filter(absolutFileName
0758:                                        .substring(0, absolutFileName
0759:                                                .lastIndexOf("."))));
0760:                            else
0761:                                newResult.setFileName(filter(absolutFileName));
0762:
0763:                            newResult
0764:                                    .setAbsolutFileName(filter(absolutFileName));
0765:
0766:                            newResult.setAbsolutDirPath(filter(mapper
0767:                                    .getAbsulutDirPath(full, siteName)));
0768:
0769:                            String temp = full;
0770:
0771:                            if (Snapper.getFileSeparatorConvention() != null
0772:                                    && Snapper.getFileSeparatorConvention()
0773:                                            .equalsIgnoreCase("unix")) {
0774:                                temp = temp.replaceAll("\\\\", "/");
0775:                            }
0776:
0777:                            newResult.setAbsolutFilePath(filter(temp));
0778:
0779:                            newResult.setRelativeDirPath(filter(mapper
0780:                                    .getRelativeDirPath(full, absolutFileName,
0781:                                            siteName)));
0782:
0783:                            newResult.setRelativeFilePath(filter(mapper
0784:                                    .getRelativeFilePath(full, absolutFileName,
0785:                                            siteName)));
0786:
0787:                            if (insideConteinerFilePath != null) {
0788:                                newResult
0789:                                        .setInsideContainerFilePath(filter(insideConteinerFilePath));
0790:                            }
0791:
0792:                            newResult
0793:                                    .setMapped(filter(mapper
0794:                                            .mapFullPathToLogical(full,
0795:                                                    siteName, null)));
0796:
0797:                            newResult.setGrantedDownload(mapper
0798:                                    .getGrantedDownload(siteName));
0799:
0800:                            newResult.setScore(score);
0801:
0802:                            newResult.setLuceneID(Integer.parseInt(luceneID));
0803:
0804:                            String patern = standardRequestParameters
0805:                                    .getProperty("resultDatePattern");
0806:
0807:                            if (patern == null)
0808:                                patern = Snapper.getResultDatePattern();
0809:
0810:                            String modifiedS = "";
0811:                            try {
0812:
0813:                                modifiedS = convertDate(modified, patern);
0814:
0815:                            } catch (Exception e) {
0816:                                modifiedS = "";
0817:                            }
0818:
0819:                            newResult.setModified(modifiedS);
0820:
0821:                            if (accessed > 0) {
0822:                                String accessedS = "";
0823:                                try {
0824:                                    accessedS = convertDate(accessed, patern);
0825:
0826:                                } catch (Exception e) {
0827:                                    accessedS = "";
0828:                                }
0829:
0830:                                newResult.setAccessed(accessedS);
0831:                            }
0832:
0833:                            if (created > 0) {
0834:                                String createdS = "";
0835:                                try {
0836:                                    createdS = convertDate(created, patern);
0837:                                } catch (Exception e) {
0838:                                    createdS = "";
0839:                                }
0840:                                newResult.setCreated(createdS);
0841:                            }
0842:
0843:                            if (sentDate > 0) {
0844:                                String sentDateS = "";
0845:                                try {
0846:                                    //sentDate = convertMailDate(sentDate,patern);
0847:                                    sentDateS = convertMailDate(sentDate,
0848:                                            patern);
0849:                                } catch (Exception e) {
0850:                                    // sentDate = re[i].getMailSentDate();
0851:                                    sentDateS = "";
0852:                                }
0853:                                newResult.setMailSentDate(sentDateS);
0854:
0855:                            }
0856:
0857:                            if (receivedDate > 0) {
0858:                                String receivedDateS = "";
0859:                                try {
0860:                                    //receivedDate = convertMailDate(receivedDate,patern);
0861:                                    receivedDateS = convertMailDate(
0862:                                            receivedDate, patern);
0863:                                } catch (Exception e) {
0864:                                    receivedDateS = "";
0865:                                }
0866:                                newResult.setMailReceivedDate(receivedDateS);
0867:
0868:                            }
0869:
0870:                            long result = size / 1000;
0871:
0872:                            if (result > 0) {
0873:                                String r = Long.toString(result) + " KB";
0874:                                newResult.setSize(r);
0875:                            } else {
0876:                                String r = Long.toString(size) + " B";
0877:                                newResult.setSize(r);
0878:                            }
0879:
0880:                            if (owner != null) {
0881:                                newResult.setOwner(owner);
0882:                            }
0883:
0884:                            if (author != null) {
0885:                                newResult.setAuthor(author);
0886:                            }
0887:
0888:                            if (lastSavedBy != null) {
0889:                                newResult.setLastSavedBy(lastSavedBy);
0890:                            }
0891:
0892:                            if (mailFrom != null) {
0893:                                newResult.setFrom(mailFrom);
0894:                            }
0895:
0896:                            if (mailTo != null) {
0897:                                newResult.setTo(mailTo);
0898:                            }
0899:
0900:                            if (mailCC != null) {
0901:                                newResult.setCC(mailCC);
0902:                            }
0903:
0904:                            if (mailBCC != null) {
0905:                                newResult.setBCC(mailBCC);
0906:                            }
0907:
0908:                            if (mailSubject != null) {
0909:                                newResult.setSubject(mailSubject);
0910:                            }
0911:
0912:                            newResult.setContent(filter(content));
0913:
0914:                            if (properties != null && !properties.equals("")) {
0915:                                newResult.setProperties(filter(properties));
0916:                            }
0917:
0918:                            if (metaData != null && !metaData.equals("")) {
0919:                                newResult.setMetaData(filter(metaData));
0920:                            }
0921:                            searchResultNew.addNewResult();
0922:                            searchResultNew.setResultArray(i, newResult);
0923:
0924:                        }
0925:
0926:                    searchO.setSearchResult(searchResultNew);
0927:
0928:                    searcher.close();
0929:                    searcher = null;
0930:                    mapper = null;
0931:                    return searchO;
0932:
0933:                } catch (Exception ex) {
0934:
0935:                    if (searcher != null) {
0936:                        try {
0937:                            searcher.close();
0938:                        } catch (Exception ex1) {
0939:                        }
0940:
0941:                    }
0942:
0943:                    searcher = null;
0944:                    mapper = null;
0945:                    Log.logException(ex);
0946:
0947:                    SearchDocument sd = SearchDocument.Factory.newInstance();
0948:
0949:                    SearchDocument.Search searchO = sd.addNewSearch();
0950:
0951:                    SearchResult searchResultNew = searchO.addNewSearchResult();
0952:
0953:                    SearchSummaryDocument.SearchSummary searchSummaryO = searchO
0954:                            .addNewSearchSummary();
0955:
0956:                    searchSummaryO.setBeginIndex(0);
0957:
0958:                    searchSummaryO.setDocsNumber(0);
0959:
0960:                    searchSummaryO.setEndIndex(0);
0961:
0962:                    searchSummaryO.setSearchedTerm(" ");
0963:
0964:                    searchSummaryO.setSearchedTime("0");
0965:
0966:                    ResultDocument.Result newResult1 = searchResultNew
0967:                            .addNewResult();
0968:
0969:                    newResult1.setContent(" ");
0970:
0971:                    return searchO;
0972:
0973:                }
0974:
0975:            }
0976:
0977:            public static String replaceAll(String input, String forReplace,
0978:                    String replaceWith) {
0979:                if (input == null)
0980:                    return null;
0981:                StringBuffer result = new StringBuffer();
0982:                boolean hasMore = true;
0983:                while (hasMore) {
0984:                    int start = input.indexOf(forReplace);
0985:                    int end = start + forReplace.length();
0986:                    if (start != -1) {
0987:                        result.append(input.substring(0, start) + replaceWith);
0988:                        input = input.substring(end);
0989:                    } else {
0990:                        hasMore = false;
0991:                        result.append(input);
0992:                    }
0993:                }
0994:                if (result.toString().equals(""))
0995:                    return input; // nothing is changed
0996:                else
0997:                    return result.toString();
0998:            }
0999:
1000:            private String filter(String stringToFilter) {
1001:                return stringToFilter;
1002:            }
1003:
1004:            public String convertDate(long date, String patern) {
1005:                String result;
1006:                try {
1007:                    SimpleDateFormat outformatter = new SimpleDateFormat(patern);
1008:                    Date d = new Date(date);
1009:                    result = outformatter.format(d);
1010:                } catch (Exception e) {
1011:                    Snapper.getLogChanel().write(
1012:                            Logger.WARNING,
1013:                            "Problem with pattern \n caught a " + e.getClass()
1014:                                    + "\n with message: " + e.getMessage());
1015:                    return null;
1016:                }
1017:
1018:                return result;
1019:
1020:            }
1021:
1022:            public String convertMailDate(long date, String patern) {
1023:                String result;
1024:                //SimpleDateFormat outformatter = new SimpleDateFormat("EEE, d MMM yyyy hh:mm aaa");
1025:                SimpleDateFormat outformatter = new SimpleDateFormat(patern);
1026:                try {
1027:                    Date d = new Date(date);
1028:                    result = outformatter.format(d);
1029:                } catch (Exception e) {
1030:                    Snapper.getLogChanel().write(
1031:                            Logger.WARNING,
1032:                            "Problem with pattern \n caught a " + e.getClass()
1033:                                    + "\n with message: " + e.getMessage());
1034:                    return null;
1035:                }
1036:
1037:                return result;
1038:
1039:            }
1040:
1041:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.