Creating PDFs in a Web Application iText - pdf

I know this may be stupid question i am gonna put here since i am a complete java noob. may be you guys can help me out to get this problem solved.
I am working on an application where a user would need the result dislayed on jsp page in pdf form, and the pdf is being generated as output stream in HTTP request, means when a user clicks on a button(on jsp) to generate PDF with result i.e. PDF getting generated on fly and sent to client browser.
Using iText, i am able to generate custom generated pdfs on the fly
Heres the Code snippet
public class PdfSample extends HttpServlet {
public PdfSample() {
super();
}
//connection to DB.... code goes here.....
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, java.io.IOException {
DocumentException ex = null;
ByteArrayOutputStream baosPDF = null;
try {
baosPDF = generatePDFDocumentBytes(req, this.getServletContext());
StringBuffer sbFilename = new StringBuffer();
sbFilename.append("filename_");
sbFilename.append(System.currentTimeMillis());
sbFilename.append(".pdf");
resp.setHeader("Cache-Control", "max-age=30");
resp.setContentType("application/pdf");
StringBuffer sbContentDispValue = new StringBuffer();
sbContentDispValue.append("inline");
sbContentDispValue.append("; filename=");
sbContentDispValue.append(sbFilename);
resp.setHeader("Content-disposition", sbContentDispValue.toString());
resp.setContentLength(baosPDF.size());
ServletOutputStream sos;
sos = resp.getOutputStream();
baosPDF.writeTo(sos);
sos.flush();
} catch (DocumentException dex) {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println(this.getClass().getName() + " caught an exception: "
+ dex.getClass().getName() + "<br>");
writer.println("<pre>");
dex.printStackTrace(writer);
writer.println("</pre>");
} finally {
if (baosPDF != null) {
baosPDF.reset();
}
}
}
protected ByteArrayOutputStream generatePDFDocumentBytes(
final HttpServletRequest req, final ServletContext ctx)
throws DocumentException
{
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter docWriter = null;
try {
docWriter = PdfWriter.getInstance(doc, baosPDF);
doc.addAuthor("Sample");
doc.addCreationDate();
doc.addProducer();
doc.addCreator("Sample");
doc.addTitle("Sample Report");
doc.setPageSize(PageSize.LETTER);
doc.open();
String strServerInfo = ctx.getServerInfo();
if (strServerInfo != null) {
}
doc.add(makeHTTPParameterInfoElement(req));
} catch (DocumentException dex) {
baosPDF.reset();
throw dex;
} finally {
if (doc != null) {
doc.close();
}
if (docWriter != null) {
docWriter.close();
}
}
if (baosPDF.size() < 1) {
throw new DocumentException("document has " + baosPDF.size()
+ " bytes");
}
return baosPDF;
}
protected Element makeHTTPParameterInfoElement(final HttpServletRequest req) {
Table tab = null;
tab = makeTable("", "White Color Car", "Black Color Car", "Total");
return (Element) tab;
}
private static Table makeTable(final String firstColumnTitle,
final String secondColumnTitle, final String thirdColumnTitle,
final String fourthColumnTitle) {
Paragraph paragraph, paragraph1;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
Table tab = null;
try {
tab = new Table(4);
} catch (BadElementException ex) {
throw new RuntimeException(ex);
}
Cell c = null;
try {
paragraph = new Paragraph("Car Report");
paragraph.setAlignment(1);
c = new Cell(paragraph);
c.setColspan(tab.getColumns());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
tab.setBorderWidth(1);
tab.setBorderColor(Color.BLACK);
tab.setPadding(2);
tab.setSpacing(3);
tab.setBackgroundColor(Color.WHITE);
tab.addCell(c);
tab.addCell(new Cell(firstColumnTitle));
tab.addCell(new Cell(secondColumnTitle));
tab.addCell(new Cell(thirdColumnTitle));
tab.addCell(new Cell(fourthColumnTitle));
tab.endHeaders();
String startDate1 = "01/06/2011";
String endDate1 = "11/09/2011";
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
try {
Date startDate = sdf1.parse(startDate1);
Date endDate = sdf1.parse(endDate1);
java.sql.Date sdate = new java.sql.Date(startDate.getTime());
java.sql.Date edate = new java.sql.Date(endDate.getTime());
String newWhiteColor = "select count(*) from .......";
String newBlackColor = "select count(*) from .......";
String totalWhiteColor= "select count(*) from .....";
String totalBlackColor = "select count(*) from .......";
Connection connection = null;
PreparedStatement preparedStatement = null;
PreparedStatement preparedStatement1 = null;
PreparedStatement preparedStatement2 = null;
PreparedStatement preparedStatement3 = null;
ResultSet resultSet = null;
ResultSet resultSet1 = null;
ResultSet resultSet2 = null;
ResultSet resultSet3 = null;
connection = getSimpleConnection1();
// New Car recently sold out - White Color
preparedStatement = connection
.prepareStatement(newWhiteColor);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
val1 = resultSet.getInt(1);
}
// New Car recently sold out - Black Color
preparedStatement3 = connection
.prepareStatement(newBlackColor);
resultSet3 = preparedStatement3.executeQuery();
while (resultSet3.next()) {
val2 = resultSet3.getInt(1);
}
// Total White Color cars sold out
preparedStatement1 = connection
.prepareStatement(totalWhiteColor);
resultSet1 = preparedStatement1.executeQuery();
while (resultSet1.next()) {
val3 = resultSet1.getInt(1);
}
// Total Black Color cars sold out
preparedStatement2 = connection
.prepareStatement(totalBlackColor);
resultSet2 = preparedStatement2.executeQuery();
while (resultSet2.next()) {
val4 = resultSet2.getInt(1);
}
Integer newWhite = val1;
Integer newBlack = val2;
Integer totalWhite = val3;
Integer totalBlack = val4;
Integer totalNewCars = newWhite + new Black;
Integer totalCars= totalWhite + totalBlack ;
tab.addCell(new Cell("New Cars Sold Out"));
tab.addCell(new Cell(newWhite.toString()));
tab.addCell(new Cell(newBlack.toString()));
tab.addCell(new Cell(totalNewCars.toString()));
tab.addCell(new Cell("Total Cars Sold out"));
tab.addCell(new Cell(totalWhite.toString()));
tab.addCell(new Cell(totalBlack.toString()));
tab.addCell(new Cell(totalCars.toString()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tab;
}
}
The Above code fetching the data (count) from the database by making the connection and displaying in cells. with the above code i am able to create pdf on fly and output as showing below.
--------------------------------------------------------------------------------------------------
Car Report
--------------------------------------------------------------------------------------------------
| White Color Car |Black Color Car | Total
--------------------------------------------------------------------------------------------------
New Cars Sold out| 5 | 8 | 13
--------------------------------------------------------------------------------------------------
Total Cars Sold Out| 6 | 4 | 10
--------------------------------------------------------------------------------------------------
Query 1) How Can I align the car report in Center?
Query 2) Data and text are being displayed inside a cell, how can i set the border of the cell to "0".
Query 3) How can i color a particular cell ( the blank one)?
Query 4) Is it possible to Insert a image on top of the header of the pdf?
Any help would be greatly appreciated !!!!!

Query 1) How Can I align the car report in Center?
Solution : c.setHorizontalAlignment(Element.ALIGN_CENTER);

Related

getting specific row values of database table into an arraylist

I am trying to get certain row records from a db_table into an array list. though this has been done many times, mine is a bit more different.
suppose I have a table in my database like this below:
+---------+--------+--------+
| NAME | BILL | CONTACT|
+---------+--------+--------+
| james | 400 | 024669 |
| Mavis | 700 | 025550 |
| john | 650 | 029510 |
| bendo | 340 | 023579 |
+---------+--------+--------+
and I want an array to display records like this:
[james,400,024669]
[Mavis,700, 025550]
[John,650, 029510]
[bendoo,340,023579]
please how do I achieve that. the code I wrote gave me only a last array. and its:
ArrayList<String> inner = null;
try{
String sql="select Name,Score,Contact from members";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
inner = new ArrayList<>();
for(int i=1; i<=columnsNumber; i++){
inner.add(rs.getString(i));
}
}
System.out.println(inner);
}catch(Exception e){
}
but this is the only System.out.PrintIn displayed:
[bendoo,340,023579]
BUILD SUCCESSFUL (total time: 2 minutes 13 seconds)
please how do I get this below as my result:
[james,400,024669]
[Mavis,700, 025550]
[John,650, 029510]
[bendoo,340,023579]
You need to add inner to a global list. Otherwise you lose the row value every time, and keep only the last one.
Your code should be changed to something like:
ArrayList<String> inner = null;
try {
String sql = "select Name, Score, Contact from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
List<List<String>> allRows = new ArrayList<List<String>>(); // add this!
while (rs.next()) {
inner = new ArrayList<>();
for(int i=1; i<=columnsNumber; i++){
inner.add(rs.getString(i));
}
System.out.println("ROW: "+row); // Add this line.
allRows.add(inner); // add this line!
}
for (List<String> row : allRows) {
System.out.println(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
please this is what I have fully so far..
ArrayList<String> inner = null;
try {
String sql = "select Name,Score, Contact from members";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
List<List<String>> allRows = new ArrayList<List<String>>(); // add this!
while (rs.next()) {
inner = new ArrayList<>();
for(int i=1; i<=columnsNumber; i++){
inner.add(rs.getString(i));
}
allRows.add(inner); // add this line!
}
for (List<String> row : allRows) {
System.out.println(row);
}
} catch (SQLException e) {
}
try{
// initialise SMS object.
ZenophSMS sms = new ZenophSMS();
sms.setUser("example#gmail.com");
sms.setPassword("xxxxxxx");
sms.authenticate();
// set message parameters.
sms.setMessage("Hello {$name}, your score is {$score} . please come for you money ");
sms.setSenderId("NICE ONE");
sms.setMessageType(MSGTYPE.TEXT);
// add destinations.
for ( ArrayList<String> recpt : allRows) sms.addRecipient(recpt.get(2), new String[] {recpt.get(0), recpt.get(1)}, false);
// submit the message. Since the total number of destinations is
// less than 400, the submit status of the destinations will be returned.
List<String[]> resp = sms.submit();
// show the status
for (String[] dest : resp)
{ REQSTATUS st = REQSTATUS.fromInt(Integer.parseInt(dest[0]));
if (null != st)
switch (st) {
case SUCCESS:
System.out.println("Submitted: %s"+ dest[1]);
break;
case ERR_INSUFF_CREDIT:
System.out.println("Insufficient Credits: %s"+dest[1]);
break;
default:
System.out.println("Failed: %s"+ dest[1]);
break;
}
}
}
catch (SMSException ex) { System.out.println("Error: " + ex.getMessage()); }
catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); }
this line cant find the array 'allRows'. please what do I do.its giving me an error message that variable cannot be found.
for ( ArrayList<String> recpt : allRows) sms.addRecipient(recpt.get(2),new String[] {recpt.get(0), recpt.get(1)}, false);
Move "inner = new ArrayList<>();" before the "while" statement. You are reinitializing the arraylist for every row.

SQL query into JTable

I've found one totally working query, which gets columns and their data from Oracle DB and puts the output in console printout.
I've spent 3 hours trying to display this data in Swing JTable.
When I am trying to bind data with JTable:
jTable1.setModel(new javax.swing.table.DefaultTableModel(
data, header
));
it keeps telling me that constructor is invalid. That's true, because I need arrays [] and [][] to make that. Any ideas how this can be implemented?
Here is the original query:
package com.javacoderanch.example.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class MetadataColumnExample {
private static final String DRIVER = "oracle.jdbc.OracleDriver";
private static final String URL = "jdbc:oracle:thin:#//XXX";
private static final String USERNAME = "XXX";
private static final String PASSWORD = "XXX";
public static void main(String[] args) throws Exception {
Connection connection = null;
try {
//
// As the usual ritual, load the driver class and get connection
// from database.
//
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//
// In the statement below we'll select all records from users table
// and then try to find all the columns it has.
//
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select *\n"
+ "from booking\n"
+ "where TRACKING_NUMBER = 1000001741");
//
// The ResultSetMetaData is where all metadata related information
// for a result set is stored.
//
ResultSetMetaData metadata = resultSet.getMetaData();
int columnCount = metadata.getColumnCount();
//
// To get the column names we do a loop for a number of column count
// returned above. And please remember a JDBC operation is 1-indexed
// so every index begin from 1 not 0 as in array.
//
ArrayList<String> columns = new ArrayList<String>();
for (int i = 1; i < columnCount; i++) {
String columnName = metadata.getColumnName(i);
columns.add(columnName);
}
//
// Later we use the collected column names to get the value of the
// column it self.
//
while (resultSet.next()) {
for (String columnName : columns) {
String value = resultSet.getString(columnName);
System.out.println(columnName + " = " + value);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
}
Ok I found a bit better way adding SQL query to JTable without even using the ArrayList. Maybe will be helpful for someone, completely working:
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.image.BufferedImage;
public class report extends JFrame {
PreparedStatement ps;
Connection con;
ResultSet rs;
Statement st;
JLabel l1;
String bn;
int bid;
Date d1, d2;
int rows = 0;
Object data1[][];
JScrollPane scroller;
JTable table;
public report() {
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
setSize(600, 600);
setLocation(50, 50);
setLayout(new BorderLayout());
setTitle("Library Report");
try {
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:#XXXXX", "XXXXX", "XXXXX");
} catch (Exception e) {
}
try {
/*
* JDBC 2.0 provides a way to retrieve a rowcount from a ResultSet without having to scan through all the rows
* So we add TYPE_SCROLL_INSENSITIVE & CONCUR_READ_ONLY
*/
st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //Creating Statement Object
} catch (SQLException sqlex) {
System.out.println("!!!###");
}
try {
rs = st.executeQuery("select TRACKING_NUMBER, INCO_TERM_CODE, MODE_OF_TRANSPORT\n"
+ "from salog.booking\n"
+ "where rownum < 5");
// Counting rows
rs.last();
int rows = rs.getRow();
rs.beforeFirst();
System.out.println("cc " + rows);
ResultSetMetaData metaData = rs.getMetaData();
int colummm = metaData.getColumnCount();
System.out.println("colms =" + colummm);
Object[] Colheads = {"BookId", "BookName", "rtyry"};
if (Colheads.length != colummm) {
// System.out.println("EPT!!");
JOptionPane.showMessageDialog(rootPane, "Incorrect Column Headers quantity listed in array! The program will now exit.", "System Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
data1 = new Object[rows][Colheads.length];
for (int i1 = 0; i1 < rows; i1++) {
rs.next();
for (int j1 = 0; j1 < Colheads.length; j1++) {
data1[i1][j1] = rs.getString(j1 + 1);
}
}
JTable table = new JTable(data1, Colheads);
JScrollPane jsp = new JScrollPane(table);
getContentPane().add(jsp);
} catch (Exception e) {
}
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
JFrame frm = new report();
frm.setSize(600, 600);
frm.setLocation(50, 50);
BufferedImage image = null;
frm.setIconImage(image);
frm.setVisible(true);
frm.show();
}
}

ArrayIndexOutOfBounds Exception when adding a document to Lucene Index (v 5.1.0)

I keep getting an ArrayIndexOutOfBounds exception after adding a couple of documents to the index. I have a thread that adds the documents to indexes across multiple machines.
I'm not sure if this has something to do with how I add fields to the Document. The "text" could be of any length.
public static void CreateIndex(){
int count = 0;
try {
IndexWriters.initIndexWriters(); //these are located on different machines on the network
int whichIndex = 0;
FileInputStream fis = new FileInputStream(Constants.MY_XML);
BufferedReader t = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
String title = "";
String articleId = "";
String str;
while ((str = t.readLine()) != null)
{
title = str.replace("<title>", "").replace("</title>", "").trim();
str = t.readLine();
articleId = str.replace("<id>", "").replace("</id>", "").trim();
str = t.readLine();
text = str.replace("<text>", "").replace("</text>", "").trim();
Document lDoc = new Document();
Field id = new TextField("id", articleId, Field.Store.YES);
Field title = new TextField("title", title, Field.Store.YES);
Field text = new TextField("text", text.toString(), Field.Store.YES);
Doc.add(id);
Doc.add(title);
Doc.add(text);
IndexWriters.indW[whichIndex].addDocument(Doc); //exception thrown here after adding couple of documents
whichIndex = (whichIndex+1)%(Constants.NUMNODES*Constants.PER_NODE_INDEXES_COUNT);
++count;
}
articleId = "";
text = null;
title = "";
}
}
}
IndexWriters.closeWriters();
}catch (IOException e) {
e.printStackTrace();
}catch( Exception e){
e.printStackTrace();
}
}
And the stack trace is as below
java.lang.ArrayIndexOutOfBoundsException: 32768
at org.apache.lucene.util.StringHelper.murmurhash3_x86_32(StringHelper.java:188)
at org.apache.lucene.util.BytesRefHash.doHash(BytesRefHash.java:460)
at org.apache.lucene.util.BytesRefHash.rehash(BytesRefHash.java:432)
at org.apache.lucene.util.BytesRefHash.add(BytesRefHash.java:323)
at org.apache.lucene.index.TermsHashPerField.add(TermsHashPerField.java:154)
at org.apache.lucene.index.DefaultIndexingChain$PerField.invert(DefaultIndexingChain.java:657)
at org.apache.lucene.index.DefaultIndexingChain.processField(DefaultIndexingChain.java:344)
at org.apache.lucene.index.DefaultIndexingChain.processDocument(DefaultIndexingChain.java:300)
at org.apache.lucene.index.DocumentsWriterPerThread.updateDocument(DocumentsWriterPerThread.java:232)
at org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:458)
at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1350)
at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1138)
Well, the assumption being made in your code is that:
Constants.NUMNODES*Constants.PER_NODE_INDEXES_COUNT is equal to (or less than) IndexWriters.indW.length
It would appear that assumption is incorrect.
(These are also a bunch of extraneous }s in there, but if it compiles, I'll assume that's just something that got left behind when you edited for posting here)

ArrayList Double

Im getting this error
java.lang.NumberFormatException: Invalid double: "-20.528899,"
im using a webservice to get latitude and longitude from bd, and show in a map.
I am not able to pass the latitude and longitude values to list
i am saving lat and long at the same column in db, so this field should be "lat, long" eg "-20.528899, -47.438933" and i need to parse this in the list...can i do this?
public List<Localizacoes> buscarLocalizacoes(){
List<Localizacoes> lista = new ArrayList<Localizacoes>();
SoapObject buscarLocalizacoes = new SoapObject(NAMESPACE, BUSCAR);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(buscarLocalizacoes);
envelope.implicitTypes = true;
HttpTransportSE http = new HttpTransportSE(URL);
try {
http.call("uri:" + BUSCAR, envelope);
Vector<SoapObject> resposta = (Vector<SoapObject>) envelope.getResponse();
for (SoapObject soapObject : resposta){
Localizacoes loc = new Localizacoes();
loc.setId(Integer.parseInt(soapObject.getProperty("id").toString()));
loc.setNome(soapObject.getProperty("nome").toString());
loc.setDescricao(soapObject.getProperty("descricao").toString());
loc.setPosicao(soapObject.getProperty("posicao").toString()); // error in this line
lista.add(loc);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return lista;
}
activity
dao = new LocalizacoesDAO(context);
List<Localizacoes> lista = dao.buscarLocalizacoes();
Log.d("Teste Buscar", lista.toString());
public void setPosicao(LatLng poLatLng) {
this.poLatLng = poLatLng;
this.posicao = String.valueOf(poLatLng.latitude) + " " + String.valueOf(poLatLng.longitude);
}
public void setPosicao(String posicao) {
this.posicao = posicao;
String[] pos = posicao.split(" ");
this.poLatLng = new LatLng(Double.valueOf(pos[0]), Double.valueOf(pos[1]));
}
can anyone help me pls?
Change the following line in your setPosicao(String) method
String[] pos = posicao.split(" ");
INTO
String[] pos = posicao.split(", ");
(Since you have a ',' and a ' ' in the string)

With Lucene 4.3.1, How to get all terms which occur in sub-range of all docs

Suppose a lucene index with fields : date, content.
I want to get all terms value and frequency of docs whose date is yesterday. date field is keyword field. content field is analyzed and indexed.
Pls help me with sample code.
My solution source is as follow ...
/**
*
*
* #param reader
* #param fromDateTime
* - yyyymmddhhmmss
* #param toDateTime
* - yyyymmddhhmmss
* #return
*/
static public String top10(IndexSearcher searcher, String fromDateTime,
String toDateTime) {
String top10Query = "";
try {
Query query = new TermRangeQuery("tweetDate", new BytesRef(
fromDateTime), new BytesRef(toDateTime), true, false);
final BitSet bits = new BitSet(searcher.getIndexReader().maxDoc());
searcher.search(query, new Collector() {
private int docBase;
#Override
public void setScorer(Scorer scorer) throws IOException {
}
#Override
public void setNextReader(AtomicReaderContext context)
throws IOException {
this.docBase = context.docBase;
}
#Override
public void collect(int doc) throws IOException {
bits.set(doc + docBase);
}
#Override
public boolean acceptsDocsOutOfOrder() {
return false;
}
});
//
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43,
EnglishStopWords.getEnglishStopWords());
//
HashMap<String, Long> wordFrequency = new HashMap<>();
for (int wx = 0; wx < bits.length(); ++wx) {
if (bits.get(wx)) {
Document wd = searcher.doc(wx);
//
TokenStream tokenStream = analyzer.tokenStream("temp",
new StringReader(wd.get("content")));
// OffsetAttribute offsetAttribute = tokenStream
// .addAttribute(OffsetAttribute.class);
CharTermAttribute charTermAttribute = tokenStream
.addAttribute(CharTermAttribute.class);
tokenStream.reset();
while (tokenStream.incrementToken()) {
// int startOffset = offsetAttribute.startOffset();
// int endOffset = offsetAttribute.endOffset();
String term = charTermAttribute.toString();
if (term.length() < 2)
continue;
Long wl;
if ((wl = wordFrequency.get(term)) == null)
wordFrequency.put(term, 1L);
else {
wl += 1;
wordFrequency.put(term, wl);
}
}
tokenStream.end();
tokenStream.close();
}
}
analyzer.close();
// sort
List<String> occurterm = new ArrayList<String>();
for (String ws : wordFrequency.keySet()) {
occurterm.add(String.format("%06d\t%s", wordFrequency.get(ws),
ws));
}
Collections.sort(occurterm, Collections.reverseOrder());
// make query string by top 10 words
int topCount = 10;
for (String ws : occurterm) {
if (topCount-- == 0)
break;
String[] tks = ws.split("\\t");
top10Query += tks[1] + " ";
}
top10Query.trim();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
// return top10 word string
return top10Query;
}