Read file.xml in FTP - java

I have a problem reading an .xml file in an FTP, and the following message appears: "ftp:\cifpag\FilesNotes\Maxdata_Venda_209016.XML (The syntax of the file name, directory name, or volume label is incorrect) ", in the code this commenting on the tests that already were done and what happens in each step, it follows the code below:
public class ImportXmlFromFTP {
private static Connection conexao;
private String endereco_ftp;
private String usuario;
private String senha;
private String caminho_ftp;
private String caminho_local;
private String valor;
public ImportXmlFromFTP(){
}
public void inicia() throws SocketException, IOException, SQLException {
//DADOS CONEXAO
this.conexao = ConexaoBancoDeDados.getConexao();
String sql = "SELECT daea_endereco_ftp, daea_usuario, daea_senha, daea_caminho_ftp, daea_caminho_local FROM sistema.dados_envio_arquivos";
PreparedStatement stmtSelect = this.conexao.prepareStatement(sql);
ResultSet rs = stmtSelect.executeQuery();
List<DadosEnviaArquivos> listaCliente = new ArrayList<DadosEnviaArquivos>();
while(rs.next()){
DadosEnviaArquivos dados = new DadosEnviaArquivos();
endereco_ftp = rs.getString("daea_endereco_ftp");
usuario = rs.getString("daea_usuario");
senha = rs.getString("daea_senha");
caminho_ftp = rs.getString("daea_caminho_ftp");
caminho_local = rs.getString("daea_caminho_local");
}
//CONEXAO COM FTP
FTPClient ftp = new FTPClient();
ftp.connect(endereco_ftp);
ftp.login( usuario, senha );
ftp.changeWorkingDirectory (caminho_ftp);
//PASTA COM OS ARQUIVOS
int m = 0;
String nomeArquivo = "";
File caminhoParaFTP = new File(caminho_local + "/");
File arquivos[] = caminhoParaFTP.listFiles();
while (m != arquivos.length){
nomeArquivo = arquivos[m].getName();
FileInputStream arqEnviar = new FileInputStream(caminhoParaFTP + "/" + nomeArquivo);
//IMPORTA PARA O FTP
if (ftp.storeFile (nomeArquivo, arqEnviar)) {
System.out.println("Arquivo armazenado com sucesso!");
arqEnviar.close();
//APAGA ARQUIVO DA PASTA LOCAL
File file = new File(caminhoParaFTP+"/"+nomeArquivo);
file.delete();
m++;
}else{
System.out.println ("Erro ao armazenar o arquivo.");
}
}
ftp.disconnect();
//LER ARQUIVO DO FTP E IMPORTAR PARA O SISTEMA
//PEGAR PASTA FTP
classe_FTP ClienteFTP = new classe_FTP();
ClienteFTP.Conectar(endereco_ftp, usuario, senha, 21);
String caminho = caminho_ftp;
ArrayList<String> nomes=new ArrayList<String>();
FTPFile[] arquivosFTP = ClienteFTP.Dir(caminho);
if (arquivosFTP != null) {
int length = arquivosFTP.length;
for (int g = 0; g < length; ++g) {
FTPFile p = arquivosFTP[g];
if (p.isFile()) {
String arquivoNominal = p.getName();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
try {
//BEGIN TESTES
FileInputStream stream = new FileInputStream("ftp:\\\\cifensa.com.br\\ArquivosNotas\\" + arquivoNominal);
//InputStream is = new FileInputStream("ftp:\\\\" +"\\\\cifensa.com.br//ArquivosNotas//" + arquivoNominal);
//InputStream stream = ftp.retrieveFileStream(p.getName());
//System.out.println("Caminho: "+stream.toString());
//END TO TESTE
Document doc = builder.parse(stream);
NodeList listaDeVenda = doc.getElementsByTagName("venda");
int tamanhoDaLista = listaDeVenda.getLength();
NodeList listaDeProdutos = doc.getElementsByTagName("item");
int tamanhoDaListaDeProdutos = listaDeProdutos.getLength();
for(int k = 0; k < tamanhoDaListaDeProdutos; k++){
Pedido pedido = new Pedido();
String slq = "INSERT INTO sistema.pedido(pedi_produto, pedi_quantidade, pedi_preco_unidade_produto, pedi_cliente, pedi_numero_documento, pedi_data_documento, pedi_cliente_cpf, " +
"pedi_numero_documento_fiscal, pedi_unidade, pedi_total_desconto_produto, pedi_valor_desconto_produto, pedi_porcetagem_desconto_produto, " +
"pedi_empresa, pedi_vendedor, pedi_operacao ) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conexao.prepareStatement(slq);
Node noDeProdutos = listaDeProdutos.item(k);
if(noDeProdutos.getNodeType() == Node.ELEMENT_NODE){
Element elementoProduto = (Element) noDeProdutos;
NodeList listaNosFilhosProdutos = elementoProduto.getChildNodes();
int tamandoListaNosFilhosProdutos = listaNosFilhosProdutos.getLength();
for(int l = 0; l < tamandoListaNosFilhosProdutos; l++){
Node nosFilhoProdutos = listaNosFilhosProdutos.item(l);
if(nosFilhoProdutos.getNodeType() == Node.ELEMENT_NODE){
Element elementoNoProduto = (Element) nosFilhoProdutos;
switch(elementoNoProduto.getTagName()){
case "VDIPRONOME":
stmt.setString(1, elementoNoProduto.getTextContent());
break;
case "VDIQTDE":
stmt.setDouble(2, Double.parseDouble(elementoNoProduto.getTextContent()));
break;
case "VDIVALOR":
stmt.setDouble(3, Double.parseDouble(elementoNoProduto.getTextContent()));
break;
}
}
}
}
for(int i = 0; i < tamanhoDaLista; i++ ){
Node noDeVenda = listaDeVenda.item(i);
if(noDeVenda.getNodeType() == Node.ELEMENT_NODE){
Element elementoVenda = (Element) noDeVenda;
NodeList listaNosFilhosVenda = elementoVenda.getChildNodes();
int tamanhoListaNosFilhosVenda = listaNosFilhosVenda.getLength();
for(int j = 0; j < tamanhoListaNosFilhosVenda; j++){
Node noFilhosVenda = listaNosFilhosVenda.item(j);
if(noFilhosVenda.getNodeType() == Node.ELEMENT_NODE){
Element elementoNoVenda = (Element) noFilhosVenda;
switch(elementoNoVenda.getTagName()){
case "VEDCLINOME":
stmt.setString(4, elementoNoVenda.getTextContent());
break;
case "VEDID":
stmt.setInt(5, Integer.parseInt(elementoNoVenda.getTextContent()));
break;
case "VEDABERTURA":
try{
String dataSemFormatacao = elementoNoVenda.getTextContent();
Timestamp ts = Timestamp.valueOf(dataSemFormatacao);
stmt.setTimestamp(6, ts);
} catch (Exception e) {
e.printStackTrace();
}
break;
case "CLICPFCGC":
stmt.setString(7, elementoNoVenda.getTextContent());
break;
}
}
}
}
stmt.setInt(8, 123);
stmt.setString(9, "UN");
stmt.setDouble(10, 23.4);
stmt.setDouble(11, 14.3);
stmt.setDouble(12, 14.5);
stmt.setInt(13, 1);
stmt.setInt(14, 1);
stmt.setInt(15, 1);
stmt.execute();
stmt.close();
System.out.println("Importado com sucesso!");
}//FIM DO FOR DE DADOS USUARIO
}//FIM DO FOR DE PRODUTOS
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}
}
}
public static String getFileExtensionName(File f) {
if (f.getName().indexOf(".") == -1) {
return "";
} else {
return f.getName().substring(f.getName().length() - 3, f.getName().length());
}
}
public void para(){
}
}

You can read this example. https://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example
Step 1: Download file to local store.
Step 2: Read file XML.
Good luck,

I was able to execute the code by transforming the XML file into a string and then returning to be an xml, I hope it helps but someone.
public class MyFTPClass {
private static FTPFile[] obterArquivosDiretorio(FTPClient ftp, String dirPath) throws IOException {
String cwd = ftp.printWorkingDirectory();
ftp.changeWorkingDirectory(dirPath);
FTPFile[] files = ftp.listFiles();
ftp.changeWorkingDirectory(cwd);
return files;
}
public static void main(String args[]) throws SAXException, ParserConfigurationException {
// Create an instance of FTPClient
FTPClient ftp = new FTPClient();
try {
// Establish a connection with the FTP URL
ftp.connect("caminho_ftp");
// Enter user details : user name and password
boolean isSuccess = ftp.login("usuario", "senha");
if (isSuccess) {
// Fetch the list of names of the files. In case of no files an
// empty array is returned
String path = "ArquivosNotas";
FTPFile[] listedDirectories = obterArquivosDiretorio(ftp, path);
int countXml = 1;
// Iterate on the returned list to obtain name of each file
for (FTPFile file : listedDirectories) {
if (file.getName().toLowerCase().contains("xml")) {
System.out.println();
System.out.println("Lendo " + countXml + " xml");
System.out.println();
InputStream stream = ftp.retrieveFileStream("ArquivosNotas/" + file.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
// xml vai virar uma string para depois fazer o parse para o document
StringBuilder sb = new StringBuilder();
String inline = "";
while ((inline = reader.readLine()) != null) {
sb.append(inline);
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// convert string do xml para xml em document
Document doc = (Document) builder.parse(new ByteArrayInputStream(sb.toString().getBytes()));
NodeList listaDeVenda = doc.getElementsByTagName("venda");
int tamanhoDaLista = listaDeVenda.getLength();
NodeList listaDeProdutos = doc.getElementsByTagName("item");
int tamanhoDaListaDeProdutos = listaDeProdutos.getLength();
System.out.println();
System.out.println("Qtde itens xml " + tamanhoDaListaDeProdutos);
System.out.println("Finalizado " + countXml + " xml");
System.out.println();
countXml++;
stream.close();
reader.close();
ftp.completePendingCommand();
}
}
}
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Related

error after committing code in git (jsp+servlets)

I have a java web project (jsp + servlets) that the local code works the property registry, if I commit to git and then give a git pull or git clone the property registry stops working, but none appear error, it simply does not record the data and only returns to the success page, this problem only occurs when I try to commit this version.
I tried to create a new repository and upload it as a new project but the problem continues
this is the DAO class
public boolean cadastrar(Imovel imovel) throws SQLException {
Connection connection = ConnectionFactory.getConexao();
//Endereço
String INSERTIMOVEL = "INSERT INTO Imovel VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String INSERTENDERECO = "INSERT INTO Endereco VALUES (DEFAULT,?,?,?,?,?,?,?)";
smt = connection.prepareStatement(INSERTENDERECO, Statement.RETURN_GENERATED_KEYS);
smt.setString(1, imovel.getEndereco().getLogradouro());
smt.setString(2, imovel.getEndereco().getComplemento());
smt.setInt(3, imovel.getEndereco().getNumero());
smt.setString(4, imovel.getEndereco().getCidade());
smt.setString(5, imovel.getEndereco().getCep());
smt.setString(6, imovel.getEndereco().getBairro());
smt.setString(7, imovel.getEndereco().getEstado());
smt.execute();
rs = smt.getGeneratedKeys();
rs.next();
//imovel
smt = connection.prepareStatement(INSERTIMOVEL);
smt.setString(1, imovel.getTitulo());
smt.setString(2, imovel.getDescricao());
smt.setString(3, "Em Análise");
smt.setString(4, "Ativo");
smt.setDouble(5, imovel.getValor());
smt.setDouble(6, imovel.getArea_total());
smt.setDouble(7, imovel.getArea_edificada());
smt.setInt(8, imovel.getComodos());
smt.setInt(9, imovel.getVagas_garagem());
smt.setInt(10, imovel.getBanheiros());
smt.setTimestamp(11, timestamp);
smt.setString(12, imovel.getDiretorio_imagem());
smt.setString(13, imovel.getTipo_imovel());
smt.setInt(14, imovel.getUsuario().getId_usuario());
smt.setInt(15, rs.getInt(1));
boolean rowInserted = smt.executeUpdate() > 0;
rs.close();
smt.close();
connection.close();
return rowInserted;
}
and this is the controller (i use command and factory patterns)
#Override
public String executar(HttpServletRequest request, HttpServletResponse response) {
try {
HttpSession usuarioLogado = request.getSession();
Sessao sessao = (Sessao) usuarioLogado.getAttribute("usuarioLogado");
Part filePart = request.getPart("uploadFile"); //
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); //
InputStream fileContent = filePart.getInputStream();
imovel.setDiretorio_imagem(sessao.getId_usuario() + File.separator + fileName);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fileContent.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
byte[] bytes = os.toByteArray();
// cria o diretorio de upload
// esse caminho e relativo ao diretorio da aplicacao
ServletContext context = request.getServletContext();
String uploadPath = context.getRealPath("/") + "Resources\\upload" + File.separator + sessao.getId_usuario();
// caso o diretorio nao exista o bloco abaixo cria o mesmo
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
//converte o array de bytes em file e grava no diretorio
File f = new File(uploadPath + File.separator + fileName);
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(bytes);
}
//Imovel Requests
String titulo = request.getParameter("titulo");
String descricao = request.getParameter("descricao");
int comodos = Integer.parseInt(request.getParameter("comodos"));
int banheiro = Integer.parseInt(request.getParameter("banheiro"));
int garagem = Integer.parseInt(request.getParameter("garagem"));
Double valor = Double.parseDouble(request.getParameter("valorimovel"));
Double areatotal = Double.parseDouble(request.getParameter("areatotal"));
Double areaedificada = Double.parseDouble(request.getParameter("areaedificada"));
String tpimovel = request.getParameter("tpimovel");
imovel.setTitulo(titulo);
imovel.setDescricao(descricao);
imovel.setComodos(comodos);
imovel.setBanheiros(banheiro);
imovel.setVagas_garagem(garagem);
imovel.setValor(valor);
imovel.setArea_total(areatotal);
imovel.setArea_edificada(areaedificada);
imovel.setTipo_imovel(tpimovel);
imovel.getUsuario().setId_usuario(sessao.getId_usuario());
//Endereço Requests
String logradouro = request.getParameter("logradouro");
int numero = Integer.parseInt(request.getParameter("numero"));
String complemento = request.getParameter("complemento");
String cidade = request.getParameter("cidade");
String estado = request.getParameter("estado");
String cep = request.getParameter("cep");
String bairro = request.getParameter("bairro");
//Endereço Set's
imovel.getEndereco().setLogradouro(logradouro);
imovel.getEndereco().setNumero(numero);
imovel.getEndereco().setComplemento(complemento);
imovel.getEndereco().setCidade(cidade);
imovel.getEndereco().setEstado(estado);
imovel.getEndereco().setCep(cep);
imovel.getEndereco().setBairro(bairro);
ImovelDAO dao = new ImovelDAO();
if (dao.cadastrar(imovel)) {
request.setAttribute("msg", "Seu imóvel foi cadastrado e passará por uma análise, fique de olho no seu email!");
return "index.jsp";
} else {
request.setAttribute("msgerro", "Ocorreu um erro ao tentar cadastrar o imóvel, tente novamente");
return "index.jsp";
}
} catch (SQLException | NumberFormatException | IOException | ServletException | MessagingException ex) {
request.setAttribute("msgerro", ex.getMessage());
return "index.jsp";
}
}
This line smt = connection.prepareStatement(INSERTIMOVEL); closes the previous statement and ResultSet. Get the value before that line.
int val = rs.getInt(1);
smt = connection.prepareStatement(INSERTIMOVEL);
// ...
smt.setInt(15, val);
If that is not the issue, please edit your question to include any exceptions or a diff with the last working version.

Exporting data to PDF JAVA, Eclipse, Jasper

I tried to make an exporting data to PDF but when I try to export it, the pdf can't show up like "no data found"
this code on bean
public JasperPrint exportTo() {
if(this.listReportMaster == null || this.listReportMaster.isEmpty()){
FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");
RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);
return null;
}
String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/report/PRPKReportPDF.jasper");
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(this.listReportMaster);
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(path, null, beanCollectionDataSource);
return jasperPrint;
} catch (JRException e) {
e.printStackTrace();
return null;
}
}
public void exportToPdf(ActionEvent actionEvent){
if(this.lazyMasterReportDataModel != null){
System.out.println("masuk exporttopdf");
String sql = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getSqlReportPrint();
List<Object> listObject = ((LazyMasterReportDataModel) this.lazyMasterReportDataModel).getObjectSqlListReportPrint();
this.listReportMaster = reportMasterPRPKController.getPRPKForReport(sql, listObject);
JasperPrint jasperPrint = exportTo();
String fileName = "PRPKNew_Report".concat("_").concat(".pdf");
if(jasperPrint != null) reportMasterPRPKController.exportToPDF(fileName, jasperPrint);
else System.out.println("jasperprint null");
}else{
System.out.println("keluar exporttopdf");
FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found");
RequestContext.getCurrentInstance().showMessageInDialog(messageFailed);
}
}
every I try to export it, always show "no data found" which is the program run this code FacesMessage messageFailed = new FacesMessage(FacesMessage.SEVERITY_INFO,"Info","No data found"); and which meam the "this.lazyMasterReportDataModel" is null but when I check it again, there's nothing wrong on code, I don't know if it have a wrong code or deficiency code
this the lazy code
List<ReportMasterPRPK> listMasterPRPK = new ArrayList<>();
ReportMasterPRPKQuery reportMasterPRPKQuery = new ReportMasterPRPKQuery();
Page page = new Page();
String order = "GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name ORDER BY a.created_date ";
String columname = "";
String sql = "";
List<Object> objectSqlList = new ArrayList<>();
String sqlReport = "";
String sqlReportPrint = "";
List<Object> objectSqlListReport = new ArrayList<>();
List<Object> objectSqlListReportPrint = new ArrayList<>();
String flag;
public LazyMasterReportDataModel() {
}
public LazyMasterReportDataModel(String flag) { //ini
this.flag = flag;
}
public LazyMasterReportDataModel(String sqlReport, List<Object> objectSqlListReport) {
this.sqlReport = sqlReport;
this.objectSqlListReport = objectSqlListReport;
}
#Override
public List<ReportMasterPRPK> load(int first, int pageSize, String sortField, SortOrder sortOrder,
Map<String, Object> filters) {
page.setLimit(pageSize);
page.setOffset(first);
if(this.sqlReport != null){
this.sql = this.sqlReport;
this.objectSqlList = this.objectSqlListReport;
}else{
sql = "";
objectSqlList = new ArrayList<>();
//objectSqlList.clear();
}
if(flag != null){ //ini
if(flag.equals("no selected")){
sql = sql+" AND c.is_selected = 'n' ";
}
}
if (filters != null){
for(String key: filters.keySet()){
String filterColumnName = "";
for(Field field : ReportMasterPRPK.class.getDeclaredFields()){
if(field.getName().equals(key)) filterColumnName = field.getAnnotation(Column.class).value();
}
if(filters.get(key) instanceof String){
if("receivedDate".equals(key)){
if(((String)filters.get(key)).trim().length() > 20){
String startDate = "'" + filters.get(key).toString().substring(0, 10) + "'";
String endDate = "'" + filters.get(key).toString().substring(11, 21) + "'";
sql = sql + " AND " + filterColumnName + " BETWEEN " + startDate + " AND " + endDate+ " ";
}
}else{
if(((String) filters.get(key)).trim().length() > 0){
sql = sql+"AND "+filterColumnName+" ILIKE ? ";
String value = "%"+filters.get(key)+"%";
objectSqlList.add(value);
}
}
}else{
if(((String[]) filters.get(key)).length > 0){
sql = sql+" AND "+filterColumnName+" in ";
String value = "(";
for(String string : (String[]) filters.get(key)){
value = value+"'"+string+"',";
}
value = value.substring(0, value.length()-1)+") ";
sql = sql + value;
}
}
}
}
if(sortField != null){
for(Field field : ReportMasterPRPK.class.getDeclaredFields()){
if(field.getName().equals(sortField)) columname = field.getAnnotation(Column.class).value();
}
if(sortOrder.toString().equals("ASCENDING")) order = " ASC";
else order = " DESC";
sql = sql+" GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name ORDER BY "+columname+" "+order;
System.out.println("sql sort: "+sql+" : "+objectSqlList.size());
}else{
sql = sql + order;
}
sqlReportPrint = sql;
objectSqlListReportPrint = objectSqlList;
this.listMasterPRPK = reportMasterPRPKQuery.retrivePage(page, sql, objectSqlList.toArray());
int dataSize = reportMasterPRPKQuery.retrieveMaxRow(sql, objectSqlList.toArray());
this.setRowCount(dataSize);
//objectSqlList.clear();
if(this.sqlReport != null){
this.sql = this.sqlReport;
this.objectSqlList = this.objectSqlListReport;
}else{
sql = "";
objectSqlList.clear();
}
order = "GROUP a.prpk_number, a.prpk_type_id, a.created_date, a.pic_prpk_id, a.business_unit_id, a.pic_department_id, a.prpk_desc, a.prpk_request, a.prpk_background, a.prpk_analysis_benefit, a.priority_level_id, a.cost, b.prpk_type_name, c.business_unit, d.department_name, e.priority_name, f.user_name ORDER BY a.created_date ";
return listMasterPRPK;
}
public List<ReportMasterPRPK> calculateRownum(List<ReportMasterPRPK> listMasterPRPK, int first){
int i = 1;
for (ReportMasterPRPK masterPRPK : listMasterPRPK) {
masterPRPK.setRownum(first + i);
i++;
}
return listMasterPRPK;
}
public String getSqlReportPrint() {
return sqlReportPrint;
}
public void setSqlReportPrint(String sqlReportPrint) {
this.sqlReportPrint = sqlReportPrint;
}
public List<Object> getObjectSqlListReportPrint() {
return objectSqlListReportPrint;
}
public void setObjectSqlListReportPrint(List<Object> objectSqlListReportPrint) {
this.objectSqlListReportPrint = objectSqlListReportPrint;
}
sorry before, if my english is to bad, but I hope you understand about what I mean...
thanks before...

How to get bookmarked table and rows in text file?

I'm having the docx file with the following string
"My name is santhanam"
"I'm from India"
"I love docx4j"
And I bookmarked the above three paragraph with the bookmark name para0,para1,para2. I need to get the output as text file with following string
{para0}My name is santhanam{para0}
{para1}I'm from India{para1}
{para2}I love docx4j{para2}
Which I already succeeded with following code.
public class GetBookMark {
public static void main(String[] args) throws Exception {
String docString = "";
String outputfilepath = "5.txt";
String inputfilepath = "bookmark.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
// String bookmark[] = new String[100000];
GetBookMark gb = new GetBookMark();
ClassFinder finder = new ClassFinder(CTBookmark.class); // <----- change
// this to suit
new TraversalUtil(documentPart.getContent(), finder);
for (Object o : finder.results)
{
CTBookmark BookMkStart = (CTBookmark) o;
String BookMarkName = BookMkStart.getName();
if (BookMarkName.startsWith("para")) {
P p = gb.findBookmarkedParagraphInMainDocumentPart(BookMarkName, documentPart);
List<Object> texts = getAllElementFromObject(p, Text.class);
if (texts.size() == 0) {
} else {
Text t1st = (Text) texts.get(0);
t1st.setValue("<" + BookMarkName + ">" + t1st.getValue());
Text tLast = (Text) texts.get(texts.size() - 1);
tLast.setValue(tLast.getValue() + "</" + BookMarkName + ">");
}
for (Object o1 : texts) {
Text t = (Text) o1;
docString += t.getValue();
}
docString += "\r\n";
}
}
// System.out.println("Document\n---------------\n" + docString);
try {
// BufferedWriter bw = new BufferedWriter(new
// FileWriter(outputfilepath));
Writer writer = new OutputStreamWriter(new FileOutputStream(outputfilepath), "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(docString);
bw.close();
} catch (Exception e) {
System.out.println("Exception while writing to file : " + e);
}
}
public static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
List<Object> result = new ArrayList<Object>();
if (obj instanceof JAXBElement)
obj = ((JAXBElement<?>) obj).getValue();
if (obj.getClass().equals(toSearch))
result.add(obj);
else if (obj instanceof ContentAccessor) {
List<?> children = ((ContentAccessor) obj).getContent();
for (Object child : children) {
result.addAll(getAllElementFromObject(child, toSearch));
}
}
return result;
}
private P findBookmarkedParagraphInMainDocumentPart(String name, MainDocumentPart documentPart)
throws JAXBException, Docx4JException {
final String xpath = "//w:bookmarkStart[#w:name='" + name + "']/..";
List<Object> objects = documentPart.getJAXBNodesViaXPath(xpath, false);
return (org.docx4j.wml.P) XmlUtils.unwrap(objects.get(0));
}
// No xpath implementation for other parts than main document; traverse
// manually
private P findBookmarkedParagraphInPart(Object parent, String bookmark) {
P p = traversePartForBookmark(parent, bookmark);
return p;
}
// Used internally by findBookmarkedParagrapghInPart().
private P traversePartForBookmark(Object parent, String bookmark) {
P p = null;
List children = TraversalUtil.getChildrenImpl(parent);
if (children != null) {
for (Object o : children) {
o = XmlUtils.unwrap(o);
if (o instanceof CTBookmark) {
if (((CTBookmark) o).getName().toLowerCase().equals(bookmark)) {
return (P) parent; // If bookmark found, the surrounding
// P is what is interesting.
}
}
p = traversePartForBookmark(o, bookmark);
if (p != null) {
break;
}
}
}
return p;
}
}
Now I bookmarked the docx file which contains tables with table0 to table(etc) with bookmarked (Tr)rows and (Tc)cells.Is it possible to get the output as
{table0}{row0}{cello}{para0}text string{para0}{para1}text string{para1}{cell0}{row0}
{row1}{cell1}{para2}text string{para2}{para3}text string{para3}{cell1}{row1}{table0}
Thanks in advance.
UPDATE
Now,I'm halfway there with the following code
public class GetBookMark {
public static void main(String[] args) throws Exception {
String docString = "";
String outputfilepath = "BMChapter 14.txt";
String inputfilepath = "BMTable.docx";
String rowbm = null;
String tblbm = null;
String parabm = null;
String cellbm = null;
String tblparabm = null;
List<Object> tblTexts = null;
String partDocString = null;
String prtblbm = null;
String prrowbm = null;
String prcellbm = null;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
GetBookMark gb = new GetBookMark();
ClassFinder finder = new ClassFinder(CTBookmark.class); // <----- change
new TraversalUtil(documentPart.getContent(), finder);
for (Object o : finder.results) {
CTBookmark BookMkStart = (CTBookmark) o;
String BookMarkName = BookMkStart.getName();
if (BookMarkName.startsWith("para")) {
P p = gb.findBookmarkedParagraphInMainDocumentPart(BookMarkName, documentPart);
List<Object> texts = getAllElementFromObject(p, Text.class);
if (texts.size() == 0) {
} else {
Text t1st = (Text) texts.get(0);
t1st.setValue("<" + BookMarkName + ">" + t1st.getValue());
Text tLast = (Text) texts.get(texts.size() - 1);
tLast.setValue(tLast.getValue() + "</" + BookMarkName + ">");
}
for (Object o1 : texts) {
Text t = (Text) o1;
docString += t.getValue();
}
docString += "\r\n";
} else {
if (BookMarkName.startsWith("table")) {
// rowbm = "</"+BookMarkName+">";
// tblbm = "<"+BookMarkName+">";
tblbm = BookMarkName;
}
if (BookMarkName.startsWith("row")) {
// rowbm = "</"+BookMarkName+">" +rowbm;
// tblbm +="<"+BookMarkName+">";
rowbm = BookMarkName;
}
if (BookMarkName.startsWith("cell")) {
// rowbm = "</"+BookMarkName+">" +rowbm;
// tblbm+="<"+BookMarkName+">";
cellbm = BookMarkName;
}
if (BookMarkName.startsWith("tble")) {
// rowbm = "</"+BookMarkName+">" +rowbm;
// tblbm+="<"+BookMarkName+">";
tblparabm = BookMarkName;
P p = gb.findBookmarkedParagraphInMainDocumentPart(BookMarkName, documentPart);
List<Object> texts = getAllElementFromObject(p, Text.class);
if (texts.size() == 0) {
} else {
if (prtblbm != tblbm) {
docString += "<" + tblbm + ">";
}
if (prrowbm != rowbm) {
docString += "<" + rowbm + ">";
}
if (prcellbm != cellbm) {
docString += "<" + cellbm + ">";
}
Text t1st = (Text) texts.get(0);
t1st.setValue("<" + tblparabm + ">" + t1st.getValue());
Text tLast = (Text) texts.get(texts.size() - 1);
tLast.setValue(tLast.getValue() + "</" + tblparabm + ">");
}
prtblbm = tblbm;
prrowbm = rowbm;
prcellbm = cellbm;
}
for (Object o1 : texts) {
Text t = (Text) o1;
docString += t.getValue();
}
docString += "\r\n";
}
}
try {
Writer writer = new OutputStreamWriter(new FileOutputStream(outputfilepath), "UTF-8");
BufferedWriter bw = new BufferedWriter(writer);
bw.write(docString);
bw.close();
} catch (Exception e) {
System.out.println("Exception while writing to file : " + e);
}
}
System.out.println(docString);
}

How to change text size to print on thermal printer

I have a java applicato that use the thermal print to printer a text. I use this printer to print the order for the bar.
This is an example of the ticket:
--> BAR
Coca Cola 1 x 1.5
Fanta 1 x 1.5
-------------------
TOTALE 3.00 euro
And the code to print this ticket works.
But I wanto to change the font size because the font is to small. How can I change the font ??
This is the code that I use to create and printer a ticket:
PrinterBean localPrinterBean = null;
StringBuffer localStringBuffer = null;
HashMap localHashMap=null;
if(emettiScontrinoCartaceo){
//PER LA STAMPA SCOTRNIO
localPrinterBean = getPrinterBean();
localStringBuffer = new StringBuffer();
localHashMap = null;
if ((localPrinterBean.isPrn_Enabled()) && (!localPrinterBean.isPrn_Onlyticket()))
{
//log.info("" + localPrinterBean.getPrn_driver());
localHashMap = PrinterManager.loadEscDriver(localPrinterBean);
//localStringBuffer.append("\n");
localStringBuffer.append(String.format("\t%s->> %s%s\n", new Object[] { localHashMap.get("size2w"), "BAR", localHashMap.get("size2w-off") }));
localStringBuffer.append("\n");
}
//FINE STAMPA SCOTNRINO
}
while (it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
ArticoliScontrini artSco = (ArticoliScontrini) (entry.getValue());
//devo inserire nella lista le righe di stampa dello scontrino
//INIZIO STAMPA SCONTRINO
localStringBuffer.append(String.format("%s\n", new Object[] { artSco.getDescrizioneArticolo() }));
String quantita = artSco.getQuantita() + " x " +artSco.getPrezzoTotStringFormattato();//toString(artSco.getQuantita(), true);
localStringBuffer.append(String.format("\t %s", new Object[] { quantita }));
localStringBuffer.append("\n");
//FINE STAMPA SCONTRINO
}
localStringBuffer.append("------------------------------------------");
localStringBuffer.append("\n");
String totale = "TOTALE " + decimalFormatter2.format(totaleScontrinoCartaceo) + " euro";
localStringBuffer.append(String.format("%s\n", new Object[] { totale }));
String str = localStringBuffer.toString();
try{
cbPrintBuffer(localPrinterBean);
}
catch(Exception e){
VisualMessageScontrini.getErroreStampaScontrino();
}
public void cbPrintBuffer(PrinterBean paramPrinterBean)
throws Exception
{
/*Object localObject1 = JabirCfg.hexItIfn(paramPrinterBean.getPrn_Hexinit());
if ((localObject1 != null) && (!((String)localObject1).equals(""))) {
printRawBytes(paramPrinterBean, ((String)localObject1).getBytes());
}*/
Object localObject1 = null;
if (paramPrinterBean.hasPrn_Bitopt(2))
{
localObject1 = new FileOutputStream(paramPrinterBean.getPrn_driver());
((FileOutputStream)localObject1).write(cbGetFlow());
((FileOutputStream)localObject1).close();
return;
}
localObject1 = PrinterManager.getPrintService_cached(paramPrinterBean);
if (paramPrinterBean.isPrnGd())
{
boolean bool = paramPrinterBean.hasPrn_Bitopt(32);
PrinterJob localObject2 = PrinterJob.getPrinterJob();
((PrinterJob)localObject2).setPrintService((PrintService)localObject1);
((PrinterJob)localObject2).setJobName("easy ticket");
PageFormat localPageFormat = ((PrinterJob)localObject2).defaultPage();
PrinterExtra.deserialize_pagejson_to_PageFormat(paramPrinterBean.getPrn_pagejson(), localPageFormat);
Printable localPrintable = null;
for (int i = 0;; i++)
{
if (bool) {
localPrintable = cbPrintable(i);
} else {
localPrintable = cbPrintable(-1);
}
if (localPrintable == null) {
break;
}
((PrinterJob)localObject2).setPrintable(localPrintable, ((PrinterJob)localObject2).validatePage(localPageFormat));
((PrinterJob)localObject2).print();
if (!bool) {
break;
}
String str = JabirCfg.hexItIfn("0x1b 0x6d");
printRawBytes(paramPrinterBean, str.getBytes());
}
return;
}
DocPrintJob localDocPrintJob = ((PrintService)localObject1).createPrintJob();
Object localObject2 = new SimpleDoc(cbGetFlow(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
localDocPrintJob.print((Doc)localObject2, new HashPrintRequestAttributeSet());
}

How to improve the speed of this code?

I'm trying to import all googlebooks-1gram files into a postgresql database. I wrote the following Java code for that:
public class ToPostgres {
public static void main(String[] args) throws Exception {
String filePath = "./";
List<String> files = new ArrayList<String>();
for (int i =0; i < 10; i++) {
files.add(filePath+"googlebooks-eng-all-1gram-20090715-"+i+".csv");
}
Connection c = null;
try {
c = DriverManager.getConnection("jdbc:postgresql://localhost/googlebooks",
"postgres", "xxxxxx");
} catch (SQLException e) {
e.printStackTrace();
}
if (c != null) {
try {
PreparedStatement wordInsert = c.prepareStatement(
"INSERT INTO words (word) VALUES (?)", Statement.RETURN_GENERATED_KEYS
);
PreparedStatement countInsert = c.prepareStatement(
"INSERT INTO wordcounts (word_id, \"year\", total_count, total_pages, total_books) " +
"VALUES (?,?,?,?,?)"
);
String lastWord = "";
Long lastId = -1L;
for (String filename: files) {
BufferedReader input = new BufferedReader(new FileReader(new File(filename)));
String line = "";
while ((line = input.readLine()) != null) {
String[] data = line.split("\t");
Long id = -1L;
if (lastWord.equals(data[0])) {
id = lastId;
} else {
wordInsert.setString(1, data[0]);
wordInsert.executeUpdate();
ResultSet resultSet = wordInsert.getGeneratedKeys();
if (resultSet != null && resultSet.next())
{
id = resultSet.getLong(1);
}
}
countInsert.setLong(1, id);
countInsert.setInt(2, Integer.parseInt(data[1]));
countInsert.setInt(3, Integer.parseInt(data[2]));
countInsert.setInt(4, Integer.parseInt(data[3]));
countInsert.setInt(5, Integer.parseInt(data[4]));
countInsert.executeUpdate();
lastWord = data[0];
lastId = id;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
However, when running this for ~3 hours it only placed 1.000.000 entries in the wordcounts table. When I check the amount of lines in the entire 1gram dataset it's 500.000.000 lines. So to import everything would take about 62.5 days, I can accept that it imports in about a week, but 2 months? I think I'm doing something seriously wrong here(I do have a server that runs 24/7, so I can actually run it for this long, but faster would be nice XD)
EDIT: This code is how I solved it:
public class ToPostgres {
public static void main(String[] args) throws Exception {
String filePath = "./";
List<String> files = new ArrayList<String>();
for (int i =0; i < 10; i++) {
files.add(filePath+"googlebooks-eng-all-1gram-20090715-"+i+".csv");
}
Connection c = null;
try {
c = DriverManager.getConnection("jdbc:postgresql://localhost/googlebooks",
"postgres", "xxxxxx");
} catch (SQLException e) {
e.printStackTrace();
}
if (c != null) {
c.setAutoCommit(false);
try {
PreparedStatement wordInsert = c.prepareStatement(
"INSERT INTO words (id, word) VALUES (?,?)"
);
PreparedStatement countInsert = c.prepareStatement(
"INSERT INTO wordcounts (word_id, \"year\", total_count, total_pages, total_books) " +
"VALUES (?,?,?,?,?)"
);
String lastWord = "";
Long id = 0L;
for (String filename: files) {
BufferedReader input = new BufferedReader(new FileReader(new File(filename)));
String line = "";
int i = 0;
while ((line = input.readLine()) != null) {
String[] data = line.split("\t");
if (!lastWord.equals(data[0])) {
id++;
wordInsert.setLong(1, id);
wordInsert.setString(2, data[0]);
wordInsert.executeUpdate();
}
countInsert.setLong(1, id);
countInsert.setInt(2, Integer.parseInt(data[1]));
countInsert.setInt(3, Integer.parseInt(data[2]));
countInsert.setInt(4, Integer.parseInt(data[3]));
countInsert.setInt(5, Integer.parseInt(data[4]));
countInsert.executeUpdate();
lastWord = data[0];
if (i % 10000 == 0) {
c.commit();
}
if (i % 100000 == 0) {
System.out.println(i+" mark file "+filename);
}
i++;
}
c.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
I reached 1.5 million rows in about 15 minutes now. That's fast enough for me, thanks all!
JDBC connections have autocommit enabled by default, which carries a per-statement overhead. Try disabling it:
c.setAutoCommit(false)
then commit in batches, something along the lines of:
long ops = 0;
for(String filename : files) {
// ...
while ((line = input.readLine()) != null) {
// insert some stuff...
ops ++;
if(ops % 1000 == 0) {
c.commit();
}
}
}
c.commit();
If your table has indexes, it might be faster to delete them, insert the data, and recreate the indexes later.
Setting autocommit off, and doing a manual commit every 10 000 records or so (look into the documentation for a reasonable value - there is some limit) could speed up as well.
Generating the index/foreign key yourself, and keeping track of it should be faster than wordInsert.getGeneratedKeys(); but I'm not sure, whether it is possible from your content.
There is an approach called 'bulk insert'. I don't remember the details, but its a starting point for a search.
Write it to do threading, running 4 threads at the same time, or split it up in sections (read from config file) and distribute it to X machines and have them get the data togeather.
Use batch statements to execute multiple inserts at the same time, rather than one INSERT at a time.
In addition I would remove the part of your algorithm which updates the word count after each insert into the words table, instead just calculate all of the word counts once inserting the words is complete.
Another approach would be to do bulk inserts rather than single inserts. See this question Whats the fastest way to do a bulk insert into Postgres? for more information.
Create threads
String lastWord = "";
Long lastId = -1L;
PreparedStatement wordInsert;
PreparedStatement countInsert ;
public class ToPostgres {
public void main(String[] args) throws Exception {
String filePath = "./";
List<String> files = new ArrayList<String>();
for (int i =0; i < 10; i++) {
files.add(filePath+"googlebooks-eng-all-1gram-20090715-"+i+".csv");
}
Connection c = null;
try {
c = DriverManager.getConnection("jdbc:postgresql://localhost/googlebooks",
"postgres", "xxxxxx");
} catch (SQLException e) {
e.printStackTrace();
}
if (c != null) {
try {
wordInsert = c.prepareStatement(
"INSERT INTO words (word) VALUES (?)", Statement.RETURN_GENERATED_KEYS
);
countInsert = c.prepareStatement(
"INSERT INTO wordcounts (word_id, \"year\", total_count, total_pages, total_books) " +
"VALUES (?,?,?,?,?)"
);
for (String filename: files) {
new MyThread(filename). start();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class MyThread extends Thread{
String file;
public MyThread(String file) {
this.file = file;
}
#Override
public void run() {
try {
super.run();
BufferedReader input = new BufferedReader(new FileReader(new File(file)));
String line = "";
while ((line = input.readLine()) != null) {
String[] data = line.split("\t");
Long id = -1L;
if (lastWord.equals(data[0])) {
id = lastId;
} else {
wordInsert.setString(1, data[0]);
wordInsert.executeUpdate();
ResultSet resultSet = wordInsert.getGeneratedKeys();
if (resultSet != null && resultSet.next())
{
id = resultSet.getLong(1);
}
}
countInsert.setLong(1, id);
countInsert.setInt(2, Integer.parseInt(data[1]));
countInsert.setInt(3, Integer.parseInt(data[2]));
countInsert.setInt(4, Integer.parseInt(data[3]));
countInsert.setInt(5, Integer.parseInt(data[4]));
countInsert.executeUpdate();
lastWord = data[0];
lastId = id;
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

Categories

Resources