I am trying to get the current page no using PDF box reader.
Hear is what i have written the code.
public class PDFTextExtractor{
ArrayList extractText(String fileName) throws Exception {
PDDocument document = null;
try {
document = PDDocument.load( new File(fileName) );
PDFTextAnalyzer stripper = new PDFTextAnalyzer();
stripper.setSortByPosition( true );
stripper.setStartPage( 0 );
stripper.setEndPage( document.getNumberOfPages() );
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
return stripper.getCharactersList();
}
finally {
if( document != null ) {
document.close();
}
}
}
And when i am trying to get the details i am writing the following code.
public class PDFTextAnalyzer extends PDFTextStripper {
public PDFTextAnalyzer() throws IOException {
super();
// TODO Auto-generated constructor stub
}
private ArrayList<CharInfo> charactersList = new ArrayList<CharInfo>();
public ArrayList<CharInfo> getCharactersList() {
return charactersList;
}
public void setCharactersList(ArrayList<CharInfo> charactersList) {
this.charactersList = charactersList;
}
#Override
protected void writeString(String string, List<TextPosition> textPositions)
throws IOException {
System.out.println("----->"+document.getPages().getCount());
/* for(int i = 0 ; i < document.getPages().getCount();i++)
{
*/
float docHeight = +document.getPage(1).getMediaBox().getHeight();
for (TextPosition text : textPositions) {
/*
* System.out.println((int)text.getUnicode().charAt(0)+" "+text.
* getUnicode()+ " [(X=" + text.getXDirAdj()+" "+text.getX() + ",Y="
* + text.getYDirAdj() + ") height=" + text.getHeightDir() +
* " width=" + text.getWidthDirAdj() + "]");
*/
System.out.println("<-->"+text.toString());
charactersList.add(new CharInfo(
text.getUnicode(),
text.getXDirAdj(),
docHeight - text.getYDirAdj(),
text.getWidthDirAdj(),
text.getHeightDir(),
text.getFontSizeInPt(),
1, // Page number of current text
text.getFont().getFontDescriptor().getFontName(),
text.getFont().getFontDescriptor().getFontFamily()
)
);
}
But i am unable to fetch the page number. See the line comment "Page number of current text".Is there any way to fetch the page number.
How about this.getCurrentPageNo()?
Related
I am trying to extract the text from the PDF document, I have inherited the PDFTextStripper as my use-case requied me to extract the text with more information not only the text part of it.
public class PDFBoxToTextBox extends PDFTextStripper {
private StringBuilder notHorizontalOriented;
public StringBuilder getNotHorizontalOriented() {
return notHorizontalOriented;
}
public void setNotHorizontalOriented(StringBuilder notHorizontalOriented) {
this.notHorizontalOriented = notHorizontalOriented;
}
public PDFBoxToTextBox() throws IOException {
super();
this.notHorizontalOriented = new StringBuilder();
// TODO Auto-generated constructor stub
}
public static String getTextBloks(InputStream in, Integer pageNum) {
try {
byte[] targetArray = IOUtils.toByteArray(in);
PDDocument document;
document = Loader.loadPDF(targetArray);
PDPage doc = document.getPage(pageNum - 1);
String page_height = String.valueOf(doc.getMediaBox().getHeight());
String page_width = String.valueOf(doc.getMediaBox().getWidth());
// Instantiate PDFTextStripper class
PDFBoxToTextBox pdfStripper = new PDFBoxToTextBox();
pdfStripper.setSortByPosition(true);
//pdfStripper.setShouldSeparateByBeads(false);
pdfStripper.setDropThreshold(0.5f);
pdfStripper.setIndentThreshold(.5f);
pdfStripper.setLineSeparator("\n");
pdfStripper.setWordSeparator("\t");
System.out.println("Line seperator ::" + pdfStripper.getWordSeparator() + "::");
// Retrieving text from PDF document
pdfStripper.setStartPage(pageNum);
pdfStripper.setEndPage(pageNum);
// System.out.println(pdfStripper.getTextMatrix());
String text = pdfStripper.getText(document);
System.out.println(text);
List tBlocks = new ArrayList<String>();
for (String w : text.split("\\|")) {
tBlocks.add(w);
// System.out.println(w);
}
LineCatcher lineCatcher = new LineCatcher(doc);
for(Map line :lineCatcher.getAllHorizontalPath(doc)){
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(line);
tBlocks.add(json);
}
Map<String, Object> result = new HashMap<String, Object>();
// Closing the document
document.close();
result.put("page_width", page_width);
result.put("page_height", page_height);
result.put("text_blocks", tBlocks);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(result);
System.out.println("NOT horizontal tex : "+ pdfStripper.getNotHorizontalOriented().toString());
System.out.println("JSON to Return");
System.out.println(json);
return json;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private int getTextOrientation(TextPosition text) {
Matrix m = text.getTextMatrix().clone();
m.concatenate(text.getFont().getFontMatrix());
int angle = (int) Math.round(Math.toDegrees(Math.atan2(m.getShearY(), m.getScaleY())));
return angle;
}
#Override
public void setDropThreshold(float dropThresholdValue) {
// TODO Auto-generated method stub
super.setDropThreshold(dropThresholdValue);
}
#Override
public void setIndentThreshold(float indentThresholdValue) {
// TODO Auto-generated method stub
super.setIndentThreshold(indentThresholdValue);
}
#Override
protected void writeWordSeparator() throws IOException {
output.write(" | ");
}
#Override
protected void writeLineSeparator() throws IOException {
output.write(" | ");
}
#Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
// ("TextBlock", ["x", "y", "w", "h", "text", "bold", "font_style","data_type"])
Map<String, String> textBlock = new HashMap<String, String>();
String prevBaseFont = "";
String x = String.valueOf(textPositions.get(0).getXDirAdj());
String y = String.valueOf(textPositions.get(0).getYDirAdj());
String w = String.valueOf(textPositions.get(0).getWidth());
String h = String.valueOf(textPositions.get(0).getHeightDir());
int rotation = 0;
Map<Float, List> charHeightMap = new HashMap<Float, List>();
for (TextPosition position : textPositions) {
if (charHeightMap.containsKey(position.getHeightDir())) {
charHeightMap.get(position.getHeightDir()).add(position.getUnicode());
} else {
List<String> chars = new ArrayList<String>();
chars.add(position.getUnicode());
charHeightMap.put(position.getHeightDir(), chars);
}
}
Float maxHeight = null;
int maxSize = 0;
for (Float ht : charHeightMap.keySet()) {
if (charHeightMap.get(ht).size() > maxSize) {
maxHeight = ht;
}
}
StringBuilder builder = new StringBuilder();
StringBuilder fbuilder = new StringBuilder();
for (TextPosition position : textPositions) {
// if (position.getHeightDir() != maxHeight) {
// continue;
// }
position.getRotation();
String baseFont = position.getFont().getFontDescriptor().getFontName();
if (baseFont != null && !baseFont.equals(prevBaseFont)) {
// fbuilder.append('[').append(baseFont).append(']');
fbuilder.append(baseFont);
prevBaseFont = baseFont;
}
builder.append(position.getUnicode());
}
// System.out.println(rotation);
// System.out.println(textPositions.get(0));
Float wid = textPositions.get(textPositions.size() - 1).getXDirAdj() - textPositions.get(0).getXDirAdj();
String font = fbuilder.toString();
textBlock.put("text", builder.toString());
textBlock.put("font", font);
Boolean bold = true ? font.toLowerCase().contains("bold") : false;
textBlock.put("bold", bold.toString());
textBlock.put("x", x);
textBlock.put("y", y);
textBlock.put("w", wid.toString());
textBlock.put("h", h);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(textBlock);
writeString(json);
}
protected void processTextPosition(TextPosition text) {
java.awt.Composite com;
Color col;
// System.out.println(this.getGraphicsState().getTextState().getRenderingMode());
System.out.println(text);
System.out.println(this.getTextOrientation(text));
switch (this.getGraphicsState().getTextState().getRenderingMode()) {
case FILL:
if (this.getTextOrientation(text) == 0 || this.getTextOrientation(text) == 90) {
super.processTextPosition(text);
} else {
this.notHorizontalOriented.append(text.getUnicode());
}
break;
case STROKE:
if (this.getTextOrientation(text) == 0 || this.getTextOrientation(text) == 90) {
super.processTextPosition(text);
} else {
System.out.println(text.getUnicode() + ", X translate : "
+ this.getGraphicsState().getCurrentTransformationMatrix().getShearX() + ", Y translate : "
+ this.getGraphicsState().getCurrentTransformationMatrix().getTranslateY() + ", angle: "
+ this.getTextOrientation(text));
this.notHorizontalOriented.append(text.getUnicode());
}
break;
case NEITHER:
System.out.println(this.getGraphicsState().getNonStrokingJavaComposite().toString());
break;
default:
System.out.println(this.getGraphicsState().getNonStrokingJavaComposite().toString());
System.out.println(this.getGraphicsState().getNonStrokingColor().getColorSpace().getName());
}
}
}
Unfortunately I cannot upload the PDF file, but I have included the image(cropped) to understand the text layout and associated values.
There are two issues that I am facing now:
Some of the numerical value is getting split, in the image value 2,020,735 is getting split as 2 and ,020,735 two separate text blocks. When I set the sortByPosition to false I am getting the correct numerical values but, I need the sortByPosition set to true
The orientation angle is 90 in this case for the text in PDF which is surprising, as all the text is horizontal as displayed in the image above
I am not sure when I am going wrong.
Getting this Error when called from Soap UI "Pdf indirect object belongs to other PDF document". Below is my code for pdf generation :
Main Method from where execution of print starts:
printFolioActivity:
public void printFolioActivity(final FolioActivityTO folio,
final String printerName, final MediaTray mediaTray,
final ServiceContext context) throws ServiceException {
// S-845164 - Changes done to use new itext library
String fileName = " ";
folioActivityDocumentAssemblerTemplate = getTemplateBean();
final String method = "printFolioActivity";
logEntering(method, context);
LOGGER.info("Print Services printFolioActivity using " + "folio : "
+ folio + "printerName : " + printerName + " mediaTray : "
+ mediaTray);
if (!DocumentServiceUtil.isNullOrEmpty(folio)
&& !DocumentServiceUtil.isNullOrEmpty(printerName)
&& !DocumentServiceUtil.isNullOrEmpty(mediaTray)) {
//FolioActivityDocumentAssemblerTemplate template = new FolioActivityDocumentAssemblerTemplate();
// Calling Folio Acitivity Template to generate the pdf
final byte[] document = folioActivityDocumentAssemblerTemplate
.generateFolioActivityDocument(folio);
if (save_gen_pdf) {
fileName = saveLocalService.save(
DocumentServiceConstant.FILE_NAME_FOLIO_ACTIVITY,
document);
}
PrintServiceFacade.getInstance().print(printerName, mediaTray,
document, context);
if(Objects.nonNull(fileName)) {
deleteFile(fileName);
}
logExiting(method);
} else {
LOGGER.error("In printFolioActivity FolioActivityTO is null or Empty OR printerName is null or Empty OR mediaTray is null or Empty");
}
}
generateFolioActivityDocument:
public byte[] generateFolioActivityDocument(FolioActivityTO folioActivityTO) {
LOGGER.info("FolioActivityDocumentAssemblerTemplate generateFolioActivityDocument :: START");
FacilityServiceFacade facilityServiceFacade = getBeanFacade();
currentDate =facilityServiceFacade.getCurrentDate();
OutputStream out = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(out);
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
pdfDoc.setDefaultPageSize(FolioActivityTemplate.PAGE_SIZE_LANDSCAPE);
PdfCanvas pdfCanvas = new PdfCanvas(
document.getPdfDocument().addNewPage(FolioActivityTemplate.PAGE_SIZE_LANDSCAPE));
FolioActivityTemplate template = new FolioActivityTemplate(false);
generateFolioActivity(pdfCanvas, document, folioActivityTO, template);
document.close(); // throwing exception here
totalPDFPages = 0;
LOGGER.info("FolioActivityDocumentAssemblerTemplate generateFolioActivityDocument :: END");
return ((ByteArrayOutputStream) out).toByteArray();
}
FolioActivityTemplate class has all the constants declared for styles,fonts.
GenerateFolioActivity:
public void generateFolioActivity(PdfCanvas pdfCanvas, Document document, FolioActivityTO folioActivityTO,
FolioActivityTemplate template) {
LOGGER.info("FolioActivityDocumentAssemblerTemplate generateFolioActivity :: START");
insertTitle(pdfCanvas, folioActivityTO.getGuestInformation(), template.activityTitleStyle,
template.packageInfoStyle);
LOGGER.info("FolioActivityDocumentAssemblerTemplate generateFolioActivity :: END");
}
insertTitle:
protected static void insertTitle(PdfCanvas pdfCanvas, FolioActivityGuestInformation guestInfo, Style titleStyle,
Style packageStyle) {
LOGGER.info("FolioActivityDocumentAssemblerTemplate insertTitle :: START : GuestInfo : " + guestInfo);
drawElementFromStyle(pdfCanvas, FolioActivityTemplate.ACTIVITY_TITLE, titleStyle, 0);
if (guestInfo == null) {
return;
}
drawElementFromStyle(pdfCanvas, guestInfo.getPackageCode() + FolioActivityTemplate.TEXT_PACKAGE_CODE_SEPARATOR
+ guestInfo.getPackageCodeDescription(), packageStyle, 0);
drawElementFromStyle(pdfCanvas,
FolioActivityTemplate.GI_ARRIVAL_TEXT + getPackageEffectiveDate(guestInfo.getPackageDateEffectiveFrom())
+ FolioActivityTemplate.TEXT_DATE_EFFECTIVE_SEPARATOR + FolioActivityTemplate.GI_DEPARTURE_TEXT
+ getPackageEffectiveDate(guestInfo.getPackageDateEffectiveTo()),
packageStyle, 1);
LOGGER.info("FolioActivityDocumentAssemblerTemplate insertTitle :: END");
}
drawElementFromStyle:
protected static void drawElementFromStyle(final PdfCanvas at, final String element, final Style style,
final int offsetCount) {
at.beginText();
float fontSize = style.getFontSize().floatValue();
if (!DocumentServiceUtil.isNullOrEmpty(element) && element.length() > 50 && fontSize > 17) {
fontSize = Float.parseFloat("17.0");
}
at.setFontAndSize(style.getFontType(), fontSize);
if (element != null) {
at.moveText(style.getX().floatValue(),
(style.getY().floatValue() - (offsetCount * style.getCapHeight().floatValue()))).showText(element);
}
at.endText();
}
Exception:
soap:Faultsoap:ServerUnexpected Error occurred : printFolioActivity : Pdf indirect object belongs to other PDF document. Copy object to current pdf document.<ServiceException xmlns:ns2="http://exception.service.com/" EXCEPTIONprintFolioActivity : Pdf indirect object belongs to other PDF document. Copy object to current pdf document.
I'd like to get all filenames of attachments/embedded files of a PDF document. I've been searching for a long time now, but my code still doesn't work.
What I tried:
File input = new File(inputfile); // Input File Path, Given as param from args[]
pd = PDDocument.load(input);
PDDocumentNameDictionary names = new PDDocumentNameDictionary(pd.getDocumentCatalog());
PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();
Map<String, COSObjectable> existedNames = efTree.getNames();
System.out.println(existedNames);//Print Embedded-Filenames to console
pd.close();
I don't know if it is even possible to print the content of a MAP to console. I'm coding in eclipse which doesn't give me any errors. But when I run the jar File I get always: NullPointerException at org.apache.pdfbox.pdmodel.PDDocument.getDocumentCatalog(PDDocument.java:778)
Any ideas or help? Many thanks...
Finally found a solution. For anyone with the same problem, the following code worked for me:
PDDocument pd;
File input = new File(inputfile); // Input File
pd = PDDocument.load(input);
//Writes all embedded Filenames (from pdf document) into Logfile
try{
PDDocumentCatalog catalog = pd.getDocumentCatalog();
PDDocumentNameDictionary names = catalog.getNames();
PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
Map<String, COSObjectable> embeddedFileNames = embeddedFiles.getNames();
//For-Each Loop is used to list all embedded files (if there is more than one)
for (Map.Entry<String, COSObjectable> entry : embeddedFileNames.entrySet())
{
//You might need to configure the logger first
logger.info("Inputfile: " + inputfile +"Found embedded File: " + entry.getKey() + ":");
}
}
catch (Exception e){
System.out.println("Document has no attachments. ");
}
Here's the ExtractEmbeddedFiles example from the source code download:
public final class ExtractEmbeddedFiles
{
private ExtractEmbeddedFiles()
{
}
/**
* This is the main method.
*
* #param args The command line arguments.
*
* #throws IOException If there is an error parsing the document.
*/
public static void main( String[] args ) throws IOException
{
if( args.length != 1 )
{
usage();
System.exit(1);
}
else
{
PDDocument document = null;
try
{
File pdfFile = new File(args[0]);
String filePath = pdfFile.getParent() + System.getProperty("file.separator");
document = PDDocument.load(pdfFile );
PDDocumentNameDictionary namesDictionary =
new PDDocumentNameDictionary( document.getDocumentCatalog() );
PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
if (efTree != null)
{
Map<String, PDComplexFileSpecification> names = efTree.getNames();
if (names != null)
{
extractFiles(names, filePath);
}
else
{
List<PDNameTreeNode<PDComplexFileSpecification>> kids = efTree.getKids();
for (PDNameTreeNode<PDComplexFileSpecification> node : kids)
{
names = node.getNames();
extractFiles(names, filePath);
}
}
}
// extract files from annotations
for (PDPage page : document.getPages())
{
for (PDAnnotation annotation : page.getAnnotations())
{
if (annotation instanceof PDAnnotationFileAttachment)
{
PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
extractFile(filePath, fileSpec.getFilename(), embeddedFile);
}
}
}
}
finally
{
if( document != null )
{
document.close();
}
}
}
}
private static void extractFiles(Map<String, PDComplexFileSpecification> names, String filePath)
throws IOException
{
for (Entry<String, PDComplexFileSpecification> entry : names.entrySet())
{
String filename = entry.getKey();
PDComplexFileSpecification fileSpec = entry.getValue();
PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
extractFile(filePath, filename, embeddedFile);
}
}
private static void extractFile(String filePath, String filename, PDEmbeddedFile embeddedFile)
throws IOException
{
String embeddedFilename = filePath + filename;
File file = new File(filePath + filename);
System.out.println("Writing " + embeddedFilename);
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
fos.write(embeddedFile.toByteArray());
}
finally
{
IOUtils.closeQuietly(fos);
}
}
private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpec )
{
// search for the first available alternative of the embedded file
PDEmbeddedFile embeddedFile = null;
if (fileSpec != null)
{
embeddedFile = fileSpec.getEmbeddedFileUnicode();
if (embeddedFile == null)
{
embeddedFile = fileSpec.getEmbeddedFileDos();
}
if (embeddedFile == null)
{
embeddedFile = fileSpec.getEmbeddedFileMac();
}
if (embeddedFile == null)
{
embeddedFile = fileSpec.getEmbeddedFileUnix();
}
if (embeddedFile == null)
{
embeddedFile = fileSpec.getEmbeddedFile();
}
}
return embeddedFile;
}
/**
* This will print the usage for this program.
*/
private static void usage()
{
System.err.println( "Usage: java " + ExtractEmbeddedFiles.class.getName() + " <input-pdf>" );
}
}
I am developping J2EE application with appfuse , i have a webform called easyVolAction that contain a method search() I need to save the result of search method in database but and an excpetion is generated when clicking in the action search :NullPointerException in object trajet .I created TrajetDaoHibernate:
public TrajetDaoHibernate() {
super(TrajetModel.class);
}
/**
* {#inheritDoc}
*/
#SuppressWarnings("unchecked")
public List<TrajetModel> getTrajet() {
Session session = getSessionFactory().getCurrentSession();
Query qry = session.createQuery("from TrajetModel u order by upper(u.id)");
return qry.list();
}
/**
* {#inheritDoc}
*/
public TrajetModel saveTrajet(TrajetModel trajet) {
if (log.isDebugEnabled()) {
log.debug("user's id: " + trajet.getId());
}
Session session = getSessionFactory().getCurrentSession();
session.saveOrUpdate(trajet);
// necessary to throw a DataIntegrityViolation and catch it in UserManager
session.flush();
return trajet;
}
#Override
public TrajetModel save(TrajetModel trajet) {
return this.saveTrajet(trajet);
}
and TrajetDao:
public interface TrajetDao extends GenericDao {
List<TrajetModel> getTrajet();
TrajetModel saveTrajet(TrajetModel trajet);
}
and trajetManager:
#Service("trajetManager")
public class TrajetModelImpl extends GenericManagerImpl<TrajetModel, Long> implements TrajetManager {
private TrajetDao trajetDao;
#Autowired
public void setTrajetModelDao(TrajetDao trajetDao) {
this.dao = trajetDao;
this.trajetDao = trajetDao;
}
/**
* {#inheritDoc}
*/
public TrajetModel getTrajet(String trajetId) {
return trajetDao.get(new Long(trajetId));
}
/**
* {#inheritDoc}
*/
public List<TrajetModel> getTrajet() {
return trajetDao.getAllDistinct();
}
/**
* {#inheritDoc}
*/
public TrajetModel saveTrajet(TrajetModel trajet) throws TrajetExistsException {
try {
return trajetDao.saveTrajet(trajet);
} catch (DataIntegrityViolationException e) {
//e.printStackTrace();
log.warn(e.getMessage());
throw new TrajetExistsException("Trajet '" + trajet.getNom() + "' already exists!");
} catch (JpaSystemException e) { // needed for JPA
//e.printStackTrace();
log.warn(e.getMessage());
throw new TrajetExistsException("Trajet '" + trajet.getNom() + "' already exists!");
}
}
}
finnaly the action where i declare the search method:
public String recherche() throws IOException, TrajetExistsException {
HttpServletRequest request = (HttpServletRequest) FacesContext
.getCurrentInstance().getExternalContext().getRequest();
// String url1 =
// FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("hidden");
String departAller = request.getParameter("easyVolRobot:villeDepart");
String arriveeAller = request.getParameter("easyVolRobot:villeArrivee");
String jourAller = request.getParameter("easyVolRobot:jourDep");
String moisAller = request.getParameter("easyVolRobot:dateDep");
String jourRetour = request.getParameter("easyVolRobot:jourDep");
String moisRetour = request.getParameter("easyVolRobot:dateArr");
String jourAllerPlus1 = jourAller + 1;
parametre = "departAller=" + departAller + "&arriveeAller="
+ arriveeAller + "&jourAller=" + jourAller + "&moisAller="
+ moisAller + "&jourRetour=" + jourRetour + "&moisRetour="
+ moisRetour;
parametre1 = "departAller=" + departAller + "&arriveeAller="
+ arriveeAller + "&jourAller=" + jourAllerPlus1 + "&moisAller="
+ moisAller + "&jourRetour=" + jourRetour + "&moisRetour="
+ moisRetour;
String response = sendGetRequest(url, parametre);
// insert();
PrintStream out = null;
try {
out = new PrintStream(new FileOutputStream(
"/data/crawl/root/siteSNCF.html"));
out.print(response);
} finally {
if (out != null)
out.close();
}
// tableau de resultats des trajets
List<TrajetModel> listTrajets = new ArrayList<TrajetModel>();
// trajet
//TrajetModel trajet = new TrajetModel();
File input = new File("/data/crawl/root/siteSNCF.html");
Document doc = Jsoup.parse(input, "UTF-8",
"http://www.easyvols.org/france-voyage");
for (Element vol : doc.select("div.vols")) {
//trajet = new TrajetModel();
for (Element allerRetour : vol.select("div.aller-retour")) {
Elements aeroport = allerRetour.select("div.aeroport");
System.out.println(aeroport.text());
Elements depart = allerRetour.select("div.depart");
Elements arrive = allerRetour.select("div.arrivee");
Elements date = allerRetour.select("div.date");
trajet.setNom(aeroport.text());
trajet.setVilleDepart(depart.text());
trajet.setVilleArrive(arrive.text());
trajet.sethArrive(12);
trajet.sethDepart(11);
trajet.sethReqt(14);
}
Elements prix2 = vol.select("div.tarif");
trajet.setPrice(prix2.text());
trajet = trajetManager.saveTrajet(trajet);
System.out.println(trajet);}
return"mainMenu";
}
why you comment this line out?
//TrajetModel trajet = new TrajetModel();
I see no other line where you create the TrajetModel.
If that object is not initiated you get the NPE here:
trajet.setNom(aeroport.text());
I have created an application that records a series of longitude and latitude values in a SQLite database and display them as a coloured track on a MapActivity.
I now want to be able to export this data somehow (preferably to a file) so a user can upload the values to a website showing a Google Map API.
My question is: what would be the quickest way to export the data (and in what file format: GPX, XML, CSV) to the SD card located on the Android device.
Many thanks.
Ok Just add this class in your Android Project (with your respective modifications)
public class DatabaseAssistant
{
private static final String EXPORT_FILE_NAME = "/sdcard/datanaexport.xml";
private Context _ctx;
private SQLiteDatabase _db;
private Exporter _exporter;
public DatabaseAssistant( Context ctx, SQLiteDatabase db )
{
_ctx = ctx;
_db = db;
try
{
// create a file on the sdcard to export the
// database contents to
File myFile = new File( EXPORT_FILE_NAME );
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
BufferedOutputStream bos = new BufferedOutputStream( fOut );
_exporter = new Exporter( bos );
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void exportData( )
{
log( "Exporting Data" );
try
{
_exporter.startDbExport( _db.getPath() );
// get the tables out of the given sqlite database
String sql = "SELECT * FROM sqlite_master";
Cursor cur = _db.rawQuery( sql, new String[0] );
Log.d("db", "show tables, cur size " + cur.getCount() );
cur.moveToFirst();
String tableName;
while ( cur.getPosition() < cur.getCount() )
{
tableName = cur.getString( cur.getColumnIndex( "name" ) );
log( "table name " + tableName );
// don't process these two tables since they are used
// for metadata
if ( ! tableName.equals( "android_metadata" ) &&
! tableName.equals( "sqlite_sequence" ) )
{
exportTable( tableName );
}
cur.moveToNext();
}
_exporter.endDbExport();
_exporter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void exportTable( String tableName ) throws IOException
{
_exporter.startTable(tableName);
// get everything from the table
String sql = "select * from " + tableName;
Cursor cur = _db.rawQuery( sql, new String[0] );
int numcols = cur.getColumnCount();
log( "Start exporting table " + tableName );
// // logging
// for( int idx = 0; idx < numcols; idx++ )
// {
// log( "column " + cur.getColumnName(idx) );
// }
cur.moveToFirst();
// move through the table, creating rows
// and adding each column with name and value
// to the row
while( cur.getPosition() < cur.getCount() )
{
_exporter.startRow();
String name;
String val;
for( int idx = 0; idx < numcols; idx++ )
{
name = cur.getColumnName(idx);
val = cur.getString( idx );
log( "col '" + name + "' -- val '" + val + "'" );
_exporter.addColumn( name, val );
}
_exporter.endRow();
cur.moveToNext();
}
cur.close();
_exporter.endTable();
}
private void log( String msg )
{
Log.d( "DatabaseAssistant", msg );
}
class Exporter
{
private static final String CLOSING_WITH_TICK = "'>";
private static final String START_DB = "<export-database name='";
private static final String END_DB = "</export-database>";
private static final String START_TABLE = "<table name='";
private static final String END_TABLE = "</table>";
private static final String START_ROW = "<row>";
private static final String END_ROW = "</row>";
private static final String START_COL = "<col name='";
private static final String END_COL = "</col>";
private BufferedOutputStream _bos;
public Exporter() throws FileNotFoundException
{
this( new BufferedOutputStream(
_ctx.openFileOutput( EXPORT_FILE_NAME,
Context.MODE_WORLD_READABLE ) ) );
}
public Exporter( BufferedOutputStream bos )
{
_bos = bos;
}
public void close() throws IOException
{
if ( _bos != null )
{
_bos.close();
}
}
public void startDbExport( String dbName ) throws IOException
{
String stg = START_DB + dbName + CLOSING_WITH_TICK;
_bos.write( stg.getBytes() );
}
public void endDbExport() throws IOException
{
_bos.write( END_DB.getBytes() );
}
public void startTable( String tableName ) throws IOException
{
String stg = START_TABLE + tableName + CLOSING_WITH_TICK;
_bos.write( stg.getBytes() );
}
public void endTable() throws IOException
{
_bos.write( END_TABLE.getBytes() );
}
public void startRow() throws IOException
{
_bos.write( START_ROW.getBytes() );
}
public void endRow() throws IOException
{
_bos.write( END_ROW.getBytes() );
}
public void addColumn( String name, String val ) throws IOException
{
String stg = START_COL + name + CLOSING_WITH_TICK + val + END_COL;
_bos.write( stg.getBytes() );
}
}
class Importer
{
}
}
instatiate DatabaseAssistant Class
DatabaseAssistant DA = new DatabaseAssistant(myContext, mySQLiteDatabase);
Do you want export data??? so...
DA.exportData();
;) hope this help!
Jorgesys
Hi LordSnoutimus i had a very similar situation so I used this code.
Export an Android SQLite db to an XML file on the SD Card
very useful, just make few changes for your app :)
so lets code it!
Jorgesys