Mostrando postagens com marcador XML. Mostrar todas as postagens
Mostrando postagens com marcador XML. Mostrar todas as postagens

quarta-feira, 31 de julho de 2013

Import xml do excel em java com Xstream


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package teste.xml.excel;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.List;

/**
 *
 * @author smart
 */
public class Teste {

    public static void main(String[] args) throws IOException, Exception {

        Teste t = new Teste();
        XStream xstream = t.getXStream();
        xstream.setMode(XStream.NO_REFERENCES);

        xstream.alias("Workbook", Teste.Workbook.class);
        xstream.addImplicitArray(Teste.Workbook.class, "Worksheet", Teste.Worksheet.class);
        xstream.alias("Table", Teste.Table.class);
        xstream.addImplicitArray(Teste.Table.class, "Row", Teste.Row.class);
        xstream.addImplicitArray(Teste.Row.class, "Cell", Teste.Cell.class);
        xstream.alias("Cell", Teste.Cell.class);
        xstream.alias("Data", String.class);

        Teste.Workbook o = (Teste.Workbook) xstream.fromXML(new File("/home/smart/NetBeansProjects/Teste Rest/build/classes/teste/xml/excel/excel.xml"), new Teste.Workbook());
        System.out.println(o);
    }

    public XStream getXStream() {
        XStream xstream = new XStream() {

            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {

                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

        return xstream;
    }
 
    private String getURIArquivo(String arquivo) throws Exception {
       URL is = this.getClass().getResource(arquivo);
       if (is == null) {
           is = this.getClass().getClassLoader().getResource(arquivo);
       }
       return URLDecoder.decode(is.toString(), "UTF-8");
   }

    static class Workbook {

        private List<Worksheet> Worksheet;

        public List getWorksheet() {
            return Worksheet;
        }

        public void setWorksheet(List Worksheet) {
            this.Worksheet = Worksheet;
        }
    }

    static class Worksheet {

        private Teste.Table Table;

        public Teste.Table getTable() {
            return Table;
        }

        public void setTable(Teste.Table Table) {
            this.Table = Table;
        }
    }

    static class Table {

        private List<Row> Row;

        public List getRow() {
            return Row;
        }

        public void setRow(List Row) {
            this.Row = Row;
        }
    }

    static class Row {

        private List<Cell> Cell;

        public List getCell() {
            return Cell;
        }

        public void setCell(List Cell) {
            this.Cell = Cell;
        }
    }

    static class Cell {

        private String Data;

        public String getData() {
            return Data;
        }

        public void setData(String Data) {
            this.Data = Data;
        }
    }
}

quarta-feira, 12 de junho de 2013

Desabilitar a validação do JSF2 para partial submit

<f:metadata>
     <f:event listener="#{control.bindValue}" type="preValidate"/>
<f:metadata>


@Component
public class FormularioControl {
        
    public void bindValue(ComponentSystemEvent evento) {
        evento.getComponent().processUpdates(FacesContext.getCurrentInstance());
        validaCampos();
    }

}

quarta-feira, 8 de maio de 2013

Converter HTML em PDF com iText


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package teste.pdf;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;

/**
 *
 * @author smart
 */
public class ConverterHtmlEmPdf {

    public static OutputStream convertHTMLtoPDF(BufferedReader html, OutputStream pdf) throws IOException, DocumentException {
        String htmlContent = "";
        try {
            StringBuilder sb = new StringBuilder();
            String line = html.readLine();
            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = html.readLine();
            }
            htmlContent = sb.toString();
        } finally {
            html.close();
        }
        Document doc = new Document(PageSize.A4);
        PdfWriter.getInstance(doc, pdf);
        doc.open();
        HTMLWorker hw = new HTMLWorker(doc);
        hw.parse(new StringReader(htmlContent));
        doc.close();
        return pdf;
    }

    public static void main(String[] args) throws IOException, DocumentException {
//        String File_To_Convert = "/tmp/teste2.html";
        String File_To_Convert = "/tmp/modelo3.html";

        BufferedReader br = new BufferedReader(new FileReader(File_To_Convert));

        String HTML_TO_PDF = "/tmp/MODELOTESTE.pdf";
        OutputStream os = new FileOutputStream(HTML_TO_PDF);

        convertHTMLtoPDF(br, os);
    }
}

quinta-feira, 8 de novembro de 2012

Criar um Xstream que ignora atributos desconhecidos

public XStream getXStream() {
  XStream xstream = new XStream() {
    @Override
    protected MapperWrapper wrapMapper(MapperWrapper next) {
      return new MapperWrapper(next) {
        @Override
        public boolean shouldSerializeMember(Class definedIn, String fieldName) {
          if (definedIn == Object.class) {
            return false;
          }
          return super.shouldSerializeMember(definedIn, fieldName);
        }
      };
    }
  };
  return xstream;
}