Source Code Cross Referenced for SearchDemo.java in  » Portal » Open-Portal » com » sun » portal » search » demo » 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 » Portal » Open Portal » com.sun.portal.search.demo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
003:         * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
004:         */
005:
006:        package com.sun.portal.search.demo;
007:
008:        import com.sun.portal.search.soif.*;
009:
010:        import java.applet.Applet;
011:        import java.awt.Button;
012:        import java.awt.FlowLayout;
013:        import java.awt.Frame;
014:        import java.awt.Panel;
015:        import java.awt.TextField;
016:        import java.awt.Event;
017:        import java.awt.AWTEvent;
018:
019:        import java.io.*;
020:
021:        /**
022:         * Performs a simple search and displays its results.
023:         */
024:        class SimpleSearch {
025:
026:            String RDMServer;
027:            String dbname;
028:            String SOIFOutputFile;
029:
030:            /**
031:             * SimpleSearch constructor
032:             * @param rdm - the rdm search server, eg, http://portal.siroe.com:2222/mySearch/search
033:             */
034:            public SimpleSearch(String rdm, String db) {
035:                System.out.println("Sun ONE Search Java Demo");
036:                RDMServer = rdm;
037:                dbname = db;
038:            }
039:
040:            public void doSearch(String scope) throws IOException {
041:
042:                /* The Search class encapsulates the search.
043:                 ** It's parameters are:
044:                 **  1) the search string
045:                 **  2) the attributes you want returned, comma delimited
046:                 **  3) sort order, comma delimited, - descending, + ascending
047:                 **  4) first hit
048:                 **  5) number of hits
049:                 **  6) query language, eg search, taxonomy-basic, schema-basic, etc
050:                 **  7) database to search
051:                 **  8) The RDM server URL, eg, http://portal.siroe.com:2222/mySearch/search
052:                 **  9) Access token (null for anonymous access, or valid
053:                 **     Sun ONE Identity Server session id)
054:                 */
055:                Search search = new Search(scope,
056:                        "score,url,title,description", "-score", 1, 20,
057:                        "search", dbname, RDMServer, null);
058:
059:                /* Execute the query. */
060:
061:                System.out.println("\nSearch results for \"" + scope + "\"");
062:
063:                DataOutputStream sos = null;
064:                if (SOIFOutputFile != null) {
065:                    try {
066:                        sos = new DataOutputStream(new FileOutputStream(
067:                                SOIFOutputFile));
068:                    } catch (Exception e1) {
069:                        System.out
070:                                .println("Error: failed to create output file: "
071:                                        + e1);
072:                    }
073:                }
074:
075:                int pagenum = 1;
076:                int pagesize = 10;
077:                SOIFBuffer firstPageSOIF = new SOIFBuffer();
078:
079:                for (; pagenum <= 5; pagenum++) {
080:
081:                    int firstHit = (pagenum - 1) * pagesize + 1;
082:
083:                    try {
084:                        search.doQuery(firstHit, pagesize);
085:                    } catch (Exception ex) {
086:                        ex.printStackTrace();
087:                        break;
088:                    }
089:
090:                    // Check the result count. -1 indicates an error.
091:                    if (search.getResultCount() <= 0)
092:                        break;
093:
094:                    System.out
095:                            .println("==========================================");
096:                    System.out
097:                            .println("page "
098:                                    + pagenum
099:                                    + ": hits "
100:                                    + search.getFirstHit()
101:                                    + " to "
102:                                    + (search.getFirstHit()
103:                                            + search.getResultCount() - 1)
104:                                    + " out of " + search.getHitCount()
105:                                    + " across " + search.getDocumentCount()
106:                                    + " documents");
107:                    System.out
108:                            .println("==========================================");
109:                    System.out.println();
110:
111:                    SOIFInputStream resultStream = search.getResultStream();
112:                    SOIF soif;
113:
114:                    /* Examine the results of the search.  The
115:                     ** results can also be returned as a string
116:                     ** using getResult(), but the SOIF version
117:                     ** parses the string for you.  The following
118:                     ** block loops through a list of SOIF instances.
119:                     */
120:                    for (soif = resultStream.readSOIF(); soif != null; soif = resultStream
121:                            .readSOIF()) {
122:
123:                        if (pagenum == 1)
124:                            firstPageSOIF.write(soif.toByteArray());
125:
126:                        /* use the getValue() method to get
127:                         ** a value associated w/ the specified
128:                         ** attribute.
129:                         */
130:                        String u = soif.getURL();
131:                        String t = soif.getValue("title");
132:                        String d = soif.getValue("description");
133:                        String sc = soif.getValue("score");
134:
135:                        /* do something with the results */
136:                        System.out
137:                                .println("TITLE:       "
138:                                        + t
139:                                        + "\n"
140:                                        + "URL:         "
141:                                        + u
142:                                        + "\n"
143:                                        + "SCORE:       "
144:                                        + sc
145:                                        + "\n"
146:                                        + "DESCRIPTION: "
147:                                        + d
148:                                        + "\n"
149:                                        + "--------------------------------------------\n");
150:                        if (sos != null) {
151:                            try {
152:                                sos.writeBytes(soif.toString());
153:                            } catch (Exception e1) {
154:                                System.out
155:                                        .println("Error: failed to write to SOIF output file: "
156:                                                + e1);
157:                            }
158:                        }
159:
160:                    }
161:
162:                    /* Break if largest hit already displayed */
163:                    if (search.getHitCount() <= (firstHit + pagesize - 1))
164:                        break;
165:
166:                }
167:
168:                if (firstPageSOIF == null)
169:                    System.out.println("No matching documents found.");
170:
171:            }
172:
173:            /**
174:             * @param filename - a file to dump raw SOIF results into - only
175:             * use if running from the comand line or an applet with file
176:             * system access
177:             */
178:            public void setSOIFfile(String filename) {
179:                SOIFOutputFile = filename;
180:            }
181:
182:        }
183:
184:        /**
185:         * A panel with a search box that calls the simple search class.
186:         */
187:        class SearchPanel extends Panel implements 
188:                java.awt.event.ActionListener {
189:
190:            Button searchBtn;
191:            TextField searchTF;
192:            SimpleSearch ss;
193:
194:            /** C'tor */
195:            public SearchPanel(SimpleSearch ss) {
196:                this .ss = ss;
197:                searchBtn = new Button("Search");
198:                searchTF = new TextField("java", 40);
199:                setLayout(new FlowLayout(FlowLayout.CENTER));
200:                add(searchTF);
201:                add(searchBtn);
202:                searchTF.addActionListener(this );
203:                searchBtn.addActionListener(this );
204:            }
205:
206:            public void actionPerformed(java.awt.event.ActionEvent e) {
207:                if (e.getID() == Event.ACTION_EVENT) {
208:                    searchTF.setEditable(false);
209:                    searchBtn.setEnabled(false);
210:                    try {
211:                        ss.doSearch(searchTF.getText());
212:                    } catch (Exception ex) {
213:                        System.out.println("Exception during search: " + ex);
214:                    }
215:                    searchTF.setEditable(true);
216:                    searchBtn.setEnabled(true);
217:                } else
218:                    super .processEvent(e);
219:            }
220:
221:        }
222:
223:        /**
224:         * Applet/application for simple query interface.  Can be used as an
225:         * example for those who want to create their own java interface.
226:         * This example demonstrates search only.  Browse, determining
227:         * the schema of the search server and obtaining the taxonomy
228:         * of the search server will be demonstrated in other examples.
229:         */
230:        public class SearchDemo extends Applet {
231:
232:            /** Run as an applet. */
233:            public void init() {
234:                String rdm = getParameter("RDMServer");
235:                String db = getParameter("Database");
236:                SimpleSearch ss = new SimpleSearch(rdm, db);
237:                SearchPanel sp = new SearchPanel(ss);
238:                setLayout(new FlowLayout(FlowLayout.CENTER));
239:                add(sp);
240:            }
241:
242:            /** Run as an application. */
243:            public static void main(String argv[]) throws Exception {
244:
245:                int args = argv.length;
246:                String SOIFOutputFile = null;
247:
248:                if (args != 1 && args != 2 && args != 3) {
249:                    System.out
250:                            .println("args: RDMServer [query] [soif_output_file_name]");
251:                    return;
252:                }
253:
254:                String rdm = argv[0]; // rdm search server, eg, http://portal.siroe.com:2222/mySearch/search
255:                String db = null; // server will use default db
256:
257:                SimpleSearch ss = new SimpleSearch(rdm, db);
258:
259:                if (args == 3) {
260:                    --args;
261:                    ss.setSOIFfile(argv[2]); // dump raw soif results to this file
262:                }
263:
264:                if (args == 1) {
265:                    // run from a search box
266:                    Frame f = new Frame();
267:                    SearchPanel sp = new SearchPanel(ss);
268:                    f.add(sp);
269:                    f.pack();
270:                    f.show();
271:                } else {
272:                    // run from command line
273:                    String query = argv[1];
274:                    ss.doSearch(query);
275:                }
276:            }
277:
278:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.