[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

UTF-8 Charset & Java Plugin Development - Problems



Dear Friends,

I'm writing a kind of a Java module that generates Dia diagrams, but I'm
facing some problems related to the UTF-8 File format. My Java class
generates
a file but when I try to open it using Dia comes a message saying the file
format is unknown.
Altough it is in the very beginning I'm appending the java code hoping it
may help.

Thanks in advance.

Joel Oliveira
Systems Engineer

-------------------------------------------------------------------------
/*
 * DiaMaker.java
 * This class drills into a directory structure and parses files named
 * *.php and *.inc writing the result into a file, supposed to be in UTF-8
 * charset to be read in Dia
 *
 * My environment is:
 *
 * java version "1.4.2_04"
 * Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
 * Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
 *
 * Linux slivre4 2.6.3-4mdk #1 Tue Mar 2 07:26:13 CET 2004 i686 unknown
unknown GNU/Linux
 *
 * Created on 28 de Outubro de 2004, 10:40
 */
import java.io.*;
import java.util.*;
/**
 *
 * @author  root
 */
public class DiaMaker {
    PrintStream os ; // used into display method
    DataOutputStream resFile;
    OutputStreamWriter osw;
    String rootDir ="/home/teleduc/public_html";
    String resultDiaFile ="/home/teleduc/TeleducParsing.txt";
    String format1 = ".php";
    String format2 = ".inc";


    /** Creates a new instance of DiaMaker */
    public DiaMaker() {
        try {
            os = System.out;
            resFile = new DataOutputStream(new FileOutputStream( new
File(resultDiaFile)));
            osw = new
OutputStreamWriter(resFile,java.nio.charset.Charset.forName("UTF-8"));
        }catch (Exception e){

        }


    }
    public static void main(String s[]){
        DiaMaker dm = new DiaMaker();
        dm.start();

    }
    public void start(){
       this.beginDiagram();
       this.findfiles(rootDir);
       this.endDiagram();

    }
    public void findfiles(String pathname){
        File dirRaiz = new File(pathname);
        if (dirRaiz.exists() && dirRaiz.isDirectory()){
            File[] conteudo = dirRaiz.listFiles();

            for (int i=0 ; i < conteudo.length; i++ ){
                File arquivo = conteudo[i];
                if (arquivo.isFile()){
                    display("examinando conteudo do arquivo:
"+arquivo.getAbsolutePath());
                    parseFile(arquivo);
                } else {
                    display("entrando no diretorio:
"+arquivo.getAbsolutePath());
                    findfiles(arquivo.getAbsolutePath());
                }
            }
        }
    }

    public void display(String msg){
        os.println(msg);

    }

    public void parseFile(File f){
        try {
            if (
            (f.getName().indexOf(format1)>-1)||
            (f.getName().indexOf(format2)>-1) ) {
                FileReader fr = new FileReader(f);
                BufferedReader br2 = new BufferedReader(fr);
                String linha = null;
                while ((linha = br2.readLine())!=null){
                    linha = linha.trim();
                    if (linha.indexOf("function ")==0){
                        display(extractName(linha));
                        createBox(linha);
                    }
                    if (linha.indexOf("select ")>-1){
                        display(linha);
                    }
                    if (linha.indexOf("update ")>-1){
                        display(linha);
                    }
                    if (linha.indexOf("insert ")>-1){
                        display(linha);
                    }
                    if (linha.indexOf("delete ")>-1){
                        display(linha);
                    }
                    //createBox(out


                }
                br2.close();
            }
        } catch  (Exception e){
        }
    }

    public String extractName(String functionName){
        String name = "";
        StringTokenizer st = new StringTokenizer(functionName, " (");
        st.nextToken();
        name = st.nextToken();
        return name;
    }
    public void beginDiagram(){
        try {
        writeToFileUTF("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

        writeToFileUTF("<dia:diagram
xmlns:dia=\"http://www.lysator.liu.se/~alla/dia/\";>");
        writeToFileUTF("<dia:diagramdata>");
        writeToFileUTF("<dia:attribute name=\"background\">");
        writeToFileUTF("<dia:color val=\"#ffffff\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"pagebreak\">");
        writeToFileUTF("<dia:color val=\"#000099\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"paper\">");
        writeToFileUTF("<dia:composite type=\"paper\">");
        writeToFileUTF("<dia:attribute name=\"name\">");
        writeToFileUTF("<dia:string>#A4#</dia:string>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"tmargin\">");
        writeToFileUTF("<dia:real val=\"2.8222\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"bmargin\">");
        writeToFileUTF("<dia:real val=\"2.8222\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"lmargin\">");
        writeToFileUTF("<dia:real val=\"2.8222\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"rmargin\">");
        writeToFileUTF("<dia:real val=\"2.8222\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"is_portrait\">");
        writeToFileUTF("<dia:boolean val=\"true\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"scaling\">");
        writeToFileUTF("<dia:real val=\"1\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"fitto\">");
        writeToFileUTF("<dia:boolean val=\"false\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("</dia:composite>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"grid\">");
        writeToFileUTF("<dia:composite type=\"grid\">");
        writeToFileUTF("<dia:attribute name=\"width_x\">");
        writeToFileUTF("<dia:real val=\"1\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"width_y\">");
        writeToFileUTF("<dia:real val=\"1\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"visible_x\">");
        writeToFileUTF("<dia:int val=\"1\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"visible_y\">");
        writeToFileUTF("<dia:int val=\"1\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:composite type=\"color\"/>");
        writeToFileUTF("</dia:composite>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"color\">");
        writeToFileUTF("<dia:color val=\"#d8e5e5\"/>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("<dia:attribute name=\"guides\">");
        writeToFileUTF("<dia:composite type=\"guides\">");
        writeToFileUTF("<dia:attribute name=\"hguides\"/>");
        writeToFileUTF("<dia:attribute name=\"vguides\"/>");
        writeToFileUTF("</dia:composite>");
        writeToFileUTF("</dia:attribute>");
        writeToFileUTF("</dia:diagramdata>");
        writeToFileUTF("<dia:layer name=\"Segundo Plano\"
visible=\"true\">");
         } catch (Exception e){}
    }
    public void endDiagram(){
        try {
        writeToFileUTF(" </dia:layer>");
        writeToFileUTF(" </dia:diagram>");
        } catch (Exception e){}

        try {resFile.close(); } catch (Exception e){}
    }

    public void createBox(String msg){
        try {
        writeToFileUTF(" <dia:object type=\"Flowchart - Box\"
version=\"0\" id=\"O2\">");
        writeToFileUTF(" <dia:attribute name=\"obj_pos\">");
        writeToFileUTF(" <dia:point val=\"10,9.88794\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"obj_bb\">");
        writeToFileUTF(" <dia:rectangle val=\"10,9.88794;14.55,11.7879\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_corner\">");
        writeToFileUTF(" <dia:point val=\"10,9.88794\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_width\">");
        writeToFileUTF(" <dia:real val=\"4.55\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_height\">");
        writeToFileUTF(" <dia:real val=\"1.9\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"border_width\">");
        writeToFileUTF(" <dia:real val=\"0\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"show_background\">");
        writeToFileUTF(" <dia:boolean val=\"true\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"padding\">");
        writeToFileUTF(" <dia:real val=\"0.5\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"text\">");
        writeToFileUTF(" <dia:composite type=\"text\">");
        writeToFileUTF(" <dia:attribute name=\"string\">");
        writeToFileUTF(" <dia:string>#"+msg+"#</dia:string>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"font\">");
        writeToFileUTF(" <dia:font family=\"sans\" style=\"0\"
name=\"Helvetica\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"height\">");
        writeToFileUTF(" <dia:real val=\"0.8\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"pos\">");
        writeToFileUTF(" <dia:point val=\"12.275,11.0879\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"color\">");
        writeToFileUTF(" <dia:color val=\"#000000\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"alignment\">");
        writeToFileUTF(" <dia:enum val=\"1\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" </dia:composite>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" </dia:object>");
        } catch (Exception e){}
    }

    public void createLine(String msg){
        try {
        writeToFileUTF(" <dia:object type=\"Standard - Line\"
version=\"0\" id=\"O3\">");
        writeToFileUTF(" <dia:attribute name=\"obj_pos\">");
        writeToFileUTF(" <dia:point val=\"12.3,7.5\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"obj_bb\">");
        writeToFileUTF(" <dia:rectangle val=\"12.275,7.5;12.3,9.88794\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"conn_endpoints\">");
        writeToFileUTF(" <dia:point val=\"12.3,7.5\"/>");
        writeToFileUTF(" <dia:point val=\"12.275,9.88794\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"numcp\">");
        writeToFileUTF(" <dia:int val=\"1\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"line_width\">");
        writeToFileUTF(" <dia:real val=\"0\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:connections>");
        writeToFileUTF(" <dia:connection handle=\"0\" to=\"O1\"
connection=\"13\"/>");
        writeToFileUTF(" <dia:connection handle=\"1\" to=\"O2\"
connection=\"2\"/>");
        writeToFileUTF(" </dia:connections>");
        writeToFileUTF(" </dia:object>");
         } catch (Exception e){}
    }

    public void createDb(String msg){
        try {

        writeToFileUTF(" <dia:object type=\"Network - Storage\"
version=\"0\" id=\"O0\">");
        writeToFileUTF(" <dia:attribute name=\"obj_pos\">");
        writeToFileUTF(" <dia:point val=\"5.1,5.9\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"obj_bb\">");
        writeToFileUTF(" <dia:rectangle val=\"5.1,5.9;6.75949,8.61897\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_corner\">");
        writeToFileUTF(" <dia:point val=\"5.1,5.9\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_width\">");
        writeToFileUTF(" <dia:real val=\"1.425\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"elem_height\">");
        writeToFileUTF(" <dia:real val=\"1.67647\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"border_width\">");
        writeToFileUTF(" <dia:real val=\"0\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"show_background\">");
        writeToFileUTF(" <dia:boolean val=\"true\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"flip_horizontal\">");
        writeToFileUTF(" <dia:boolean val=\"false\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"flip_vertical\">");
        writeToFileUTF(" <dia:boolean val=\"false\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"padding\">");
        writeToFileUTF(" <dia:real val=\"0.353553\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"text\">");
        writeToFileUTF(" <dia:composite type=\"text\">");
        writeToFileUTF(" <dia:attribute name=\"string\">");
        writeToFileUTF(" <dia:string>#"+msg+"#</dia:string>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"font\">");
        writeToFileUTF(" <dia:font family=\"sans\" style=\"0\"
name=\"Helvetica\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"height\">");
        writeToFileUTF(" <dia:real val=\"0.8\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"pos\">");
        writeToFileUTF(" <dia:point val=\"5.93824,8.22647\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"color\">");
        writeToFileUTF(" <dia:color val=\"#000000\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" <dia:attribute name=\"alignment\">");
        writeToFileUTF(" <dia:enum val=\"1\"/>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" </dia:composite>");
        writeToFileUTF(" </dia:attribute>");
        writeToFileUTF(" </dia:object>");
        } catch (Exception e){}
    }

    public void writeToFileUTF(String msg){
        try{
            System.out.println(osw.getEncoding());
            osw.write(msg+"\n");
        } catch (Exception e){
        }
    }
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] Mail converted by Mofo Magic and the Flying D

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

GuideSMACK