Problema com java e couchdb

0 respostas
jeanlopes1

Oi gente,

me deparei com o problema mais insolúvel que já vi na vida até agora.

Eu criei um parser para transformar xml em json e exportar isso pro couchdb. O programa aparentemente funciona direitinho, mas quando vamos recuperar os dados do valor importado no couch ele retorna null. Sendo que no futon ele enxerga todos os dados importados!!!

@.@

Eis o código do parser xml to json
private Object parseIt(Object mOrL) {
        StringBuilder oldString = new StringBuilder("a");
        int i = 0;
        int j = 0;
        long countTags = 0;

        do {

            oldString.delete(0, oldString.length());
            oldString.append(this.s.toString());
            
            
            
            while (s.length() > 0  && s.charAt(0)=='\t') s = s.deleteCharAt(0);
            while (s.length() > 0  && s.charAt(0)==' ') s = s.deleteCharAt(0);
            
            parseamento:
            {

                Pattern endTag = Pattern.compile("</.+?>");
                Matcher et = endTag.matcher(s);

                boolean etFind = et.find();
                String etGroup0 = etFind ? et.group(0) : "";
                boolean ctEmpty = this.closeTag.empty();
                String closeTagPeek = ctEmpty ? "" : "</" + this.closeTag.peek().toString() + ">";
                closeTagPeek = closeTagPeek.replaceAll(" ", "");
                boolean equals = ctEmpty ? false : etGroup0.equals(closeTagPeek);
                boolean startsWith = s.indexOf("</") ==0;
                
                if (!ctEmpty && etFind && equals && startsWith) {
                    this.closeTag.pop();
                    s = s.replace(et.start(0), et.end(0), "");
                   
                    while (s.length() > 0  && s.charAt(0)=='\t') s = s.deleteCharAt(0);

                    if (textInContent == false) {
                        return mOrL instanceof Map ? (Map<String, Object>) mOrL : (ArrayList<Map<String, Object>>) mOrL;
                    }
                    textInContent = false;
                } else if (!ctEmpty && etFind && !equals && startsWith) {
                    controlador.printOnLog("Erro de sintaxe "
                            + "no xml. Por favor,\n passe-o em um validador"
                            + "\n a tag " + et.group(0) + " está fechando uma tag que não foi aberta"
                            + "\nSe o SAFEMODE estiver habilitado a compilação vai "
                            + "\nparar por causa desse erro"
                            + "\nArquivo:" + actualFile
                            + "\nTag nº:" + String.valueOf(countTags));

                    if (controlador.safeMode()) {
                        controlador.printOnLog("A compilação parou");
                        throw new IllegalArgumentException();
                    }

                    controlador.printOnLog("tag não compilada:" + et.group());
                    s = s.replace(et.start(0), et.end(0), "");
                }




                Pattern tagAberta = Pattern.compile("<.+?[^/]>");
                Pattern tagFechada = Pattern.compile("<.+?[^<]/>");
                Pattern notTag = Pattern.compile("<[A-Za-z0-9_]+>");

                Matcher tA = tagAberta.matcher(s);
                Matcher tF = tagFechada.matcher(s);
                Matcher nT = notTag.matcher(s);

                boolean tAfind = tA.find();
                boolean tFfind = tF.find();
                boolean nTfind = nT.find();
                int nTend = nTfind ? nT.end() : 0;
                int nTstart = nTfind ? nT.start() : 0;
                primeirasTags:
                if ((tAfind && tFfind && !nTfind)
                        || (tAfind && tFfind && nTfind && nTstart != 0)) {

                    countTags++;
                    String aux = "";

                    int tAEnd = tAfind ? tA.end() : 0;
                    int tFEnd = tFfind ? tF.end() : 0;
                    Pattern tag;
                    Matcher t;


                    if ((tAfind && !tFfind)
                            || (tAfind && tFfind && tAEnd < tFEnd)) {
                        if (nTfind && nTend < tAEnd) {
                            break primeirasTags;
                        }
                        aux = tA.group(0);
                        tag = Pattern.compile("<.+? ");
                        t = tag.matcher(aux);
                        if (!t.find()) {
                            break primeirasTags;
                        }
                        s = s.replace(tA.start(0), tA.end(0), "");
                    } else if ((!tAfind && tFfind)
                            || (tAfind && tFfind && tAEnd > tFEnd)) {
                        if (nTfind && nTend < tFEnd) {
                            break primeirasTags;
                        }
                        aux = tF.group(0);
                        tag = Pattern.compile("<.+? ");
                        t = tag.matcher(aux);
                        if (!t.find()) {
                            break primeirasTags;
                        }
                        s = s.replace(tF.start(0), tF.end(0), "");

                    } else {
                        controlador.printOnLog("Erro interno desconhecido. "
                                + "Xingue o programador"
                                + "\nArquivo:" + actualFile
                                + "Tag nº:" + String.valueOf(countTags));
                        throw new IllegalArgumentException();
                    }


                    aux = t.replaceFirst("");

                    Pattern pairs = Pattern.compile(".+?=\".*?\"");
                    Matcher p = pairs.matcher(aux);
                    Map<String, Object> allP = new HashMap<String, Object>();

                    while (p.find()) {

                        Pattern vv = Pattern.compile(".+?=\"\"");
                        Matcher m = vv.matcher(p.group());

                        Pattern k = Pattern.compile(".+?=");
                        Matcher n = k.matcher(p.group());
                        n.find();

                        if (m.find() && !n.find()) {
                            String key = m.group();
                            while (key.startsWith(" ")) key = key.replaceFirst(" ", "");
                            key = key.substring(0, key.length() - 3);

                            aux = p.replaceFirst("");
                            p = pairs.matcher(aux);
                            
                            while (key.startsWith(" ")) key = key.replaceFirst(" ", "");
                            
                            allP.put(key, "");
                            

                        } else {
                            String key = n.group();
                            while (key.startsWith(" ")) key = key.replaceFirst(" ", "");
                            key = key.substring(0, key.length() - 1);
                            aux = p.replaceFirst("");
                            p = pairs.matcher(aux);

                            String value = n.replaceFirst("");
                            value = value.substring(1, value.length() - 1);
                            while (key.startsWith(" ")) {
                                key = key.replaceFirst(" ", "");
                            }
                            allP.put(key, value);
                            
                        }

                    }

                    if (!aux.endsWith("/>")) {
                        closeTag.push(t.group().substring(1, t.group().length()));
                        this.key = t.group().substring(1, t.group().length());
                        while (key.endsWith(" ")) key = key.replaceFirst(" ", "");
                        allP.put(key,(HashMap<String, Object>) this.parseIt(new HashMap<String, Object>()));
                    }
                    if (mOrL instanceof Map) {
                        ((Map<String, Object>) mOrL).put(t.group().substring(1, t.group().length()), allP);
                    } else {
                        Map<String, Object> hm = new HashMap<String, Object>();
                        hm.put(t.group().substring(1, t.group().length()), allP);
                        ((ArrayList<Map<String, Object>>) mOrL).add(hm);
                    }
                    this.key = t.group().substring(1, t.group().length());
                    while (key.endsWith(" ")) key = key.replaceFirst(" ", "");
                    break parseamento;
                }


                nT = notTag.matcher(s);

                if (nT.find() && nT.start() == 0) {
                    countTags++;
                    Pattern tag = Pattern.compile("<.+?>");
                    Matcher t = tag.matcher(nT.group(0));
                    if (!t.find()) {
                        controlador.printOnLog("Erro de sintaxe "
                                + "no xml. Por favor,\n passe-o em um validador"
                                + "\nArquivo:" + actualFile
                                + "Tag nº:" + String.valueOf(countTags));
                        throw new IllegalArgumentException();
                    }

                    t = tag.matcher(s);
                    if (!t.find())break parseamento;

                    key = t.group(0).substring(1, t.group(0).length() - 1);
                    s = s.replace(t.start(0), t.end(0), "");
                    closeTag.push(key);
                
                    boolean vdd = true;
                    while (vdd) {
                    vdd = false;
                    while (s.length() > 0  && s.charAt(0)=='\n') { s = s.deleteCharAt(0); vdd = true; }
                    while (s.length() > 0  && s.charAt(0)==' ') { s = s.deleteCharAt(0); vdd = true; }
                    while (s.length() > 0  && s.charAt(0)=='\t') { s = s.deleteCharAt(0); vdd = true; }
                }

                    if (s.charAt(0)=='<') {

                        if (mOrL instanceof Map) {
                            ((Map<String, Object>) mOrL).put(key, this.parseIt(new ArrayList<Map<String, Object>>()));
                        } else {
                            Map<String, Object> hm = new HashMap<String, Object>();
                            hm.put(key, this.parseIt(new ArrayList<Map<String, Object>>()));
                            ((ArrayList<Map<String, Object>>) mOrL).add(hm);
                        }
                    } else {

                        Pattern textFirst = Pattern.compile(".*?</");
                        Matcher textf = textFirst.matcher(s);
                        if (!textf.find()) break parseamento;
                        String value = textf.group(0).substring(0, textf.group(0).length() - 2);
                        s = s.replace(textf.start(0), textf.end(0), "");
                        
                        s.insert(0, "</");

                        if (mOrL instanceof Map) {
                            ((Map<String, Object>) mOrL).put(key, value);
                        } else {
                            Map<String, Object> hm = new HashMap<String, Object>();
                            hm.put(key, value);
                            ((ArrayList<Map<String, Object>>) mOrL).add(hm);
                        }

                        textInContent = true;
                    }

                }

                if (i == 4 && s.length() > 3 && j < 5) {
                    i = 0;
                    j++;
                    while (s.length() > 0  && s.charAt(0)=='\t')  s = s.deleteCharAt(0);
                    while (s.length() > 0  && s.charAt(0)==' ')  s = s.deleteCharAt(0);

                    Pattern textFirst = Pattern.compile(".*?</");
                    Matcher textf = textFirst.matcher(s);
                    if (!textf.find()) break parseamento;
                    String value = textf.group(0).substring(0, textf.group(0).length() - 2);
                    s = s.replace(textf.start(0), textf.end(0), "");

                        s.insert(0, "</");
                    if (mOrL instanceof Map) {
                        ((Map<String, Object>) mOrL).put(key, value);
                    } else {
                        Map<String, Object> hm = new HashMap<String, Object>();
                        hm.put(key, value);
                        ((ArrayList<Map<String, Object>>) mOrL).add(hm);
                    }
                    textInContent = true;

                }


            }
            
            i = oldString.toString().equalsIgnoreCase(s.toString()) ? i + 1 : 0;

        } while (i < 5);
        
        if (this.controlador.isGc()) System.gc();

        return mOrL instanceof Map ? (Map<String, Object>) mOrL : (ArrayList<Map<String, Object>>) mOrL;
            }

Eis o código do método que envia ao couchdb

public void toCouch() {
        if (!this.xmlToolJsonView.getBusyInconTimer().isRunning()) {
            this.xmlToolJsonView.getBusyInconTimer().start();
        }
        Database db;

        if (!this.xmlToolJsonView.getPortjTextField1().getText().isEmpty()) {
            db = new Database(this.xmlToolJsonView.getHostjTextField3().getText(), Integer.parseInt(this.xmlToolJsonView.getPortjTextField1().getText()), this.xmlToolJsonView.getDatabasejTextField2().getText());
        } else {
            db = new Database(this.xmlToolJsonView.getHostjTextField3().getText(),
                    this.xmlToolJsonView.getDatabasejTextField2().getText());
        }
        if (!this.xmlToolJsonView.getUserjTextField4().getText().isEmpty()) {

            db.getServer().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(splitTag, splitTag));
        }

        if (map instanceof ArrayList) {
                        
            for (int i = 0; i < map.size(); i++) {
                
                db.createDocument(map.get(i));
            }
        } else {
            map = this.runParser();
        }
        if (!this.xmlToolJsonView.getBusyInconTimer().isRunning()) {
            this.xmlToolJsonView.getBusyInconTimer().start();
        }

        if (map == null) {
            this.printOnLog("Erros ocorreram, o xml não foi parseado."
                    + "Verifique seus passos. Verifique também a sintaxe do xml");
        } else {


            for (int i = 0; i < map.size(); i++) {

                db.createDocument(map.get(i));
            }
        }
        if (this.xmlToolJsonView.getBusyInconTimer().isRunning()) {
            this.xmlToolJsonView.getBusyInconTimer().stop();
        }
    }

Eis as fotos de como os dados aparecem no couch

Isso é muito estranho... olhando as duas figuras em anexo, sabemos que ele deveria retornar o valor de "unidade" com os dados que estão contidos nela, e não um campo null....

Isso é extremamente difícil de resovler...
se alguém puder me ajudar, ou ao menos indicar alguma referência onde eu possa me esclarecer sobre este tipo de fenômeno, fico eternamente agradecido!!



Criado 10 de janeiro de 2011
Respostas 0
Participantes 1