Anonymous inner class not getting called when executed via jar file

119 views Asked by At

I have written a program that reads an XML file using java SAX parser, modifies the tags and writes it to another XML file.

The program works fine when i run the class using java convertToXYZ, but when I build a jar and try to run it using the java -jar XMLGenerator.jar, it doesn't enter the startDocument() or startElement() method of the DefaultHandler anonymous inner class.I get an XML file with few tags that are not in the inner class (I have hardcoded them outside). Is it because the anonymous inner class was not built?

The anonymous inner class file convertToXYZ$1.class is present in the jar file.

I even tried using a named inner class for DefaultHandler... but that too didn't work out.

Here is the snip of the anonymous inner class :

DefaultHandler handler = new DefaultHandler() {

    NodeList nodes = null;
    NodeList parentnodes;
    int number_of_nodes;
    int number_of_parents;
    boolean tag_status[];
    boolean parent_status[];
    String store;

    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.startDocument();

        try{
            File prop = new File("ABCTags.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(prop);
            doc.getDocumentElement().normalize();

            nodes= doc.getElementsByTagName("Tag");
            parentnodes =doc.getElementsByTagName("ParentTag");
            number_of_nodes = nodes.getLength();
            number_of_parents = parentnodes.getLength();
            tag_status = new boolean[number_of_nodes];
            parent_status = new boolean[number_of_parents];

            Arrays.fill(tag_status, Boolean.FALSE);
            Arrays.fill(parent_status, Boolean.FALSE);
        } catch(Exception e){ }
    }

    public void startElement(String uri, String localName,String qName, 
                    Attributes attributes) throws SAXException {

        for(int i=0;i<number_of_nodes;i++) {
            Node node= nodes.item(i);

            if(node instanceof Element) {
                Element child = (Element) node;
                if(child.getAttribute("store").equalsIgnoreCase(qName) || child.getAttribute("abcName")!= null && qName.equalsIgnoreCase(child.getAttribute("abcName"))) {
                    tag_status[i] = true;
                }

            }
        }

        for(int j=0; j<number_of_parents; j++) {
            Node node= parentnodes.item(j);

            if(node instanceof Element) {
                Element child = (Element) node;

                if(child.getAttribute("abcName")!= null && qName.equalsIgnoreCase(child.getAttribute("abcName")) && !child.getAttribute("display").equalsIgnoreCase("nil")) {
                    if(child.getAttribute("type").equalsIgnoreCase("closed")) {
                        if(child.getAttribute("true").length()>0) {
                            int num = Integer.parseInt(child.getAttribute("true"));
                            if(parent_status[num]) {
                                parent_status[j] = true;
                                convertToXYZ.buffer+= "<"+child.getTextContent()+" SEGMENT=\"1\"/>\n";
                            }
                        } else {
                            parent_status[j] = true;
                            convertToXYZ.buffer+= "<"+child.getTextContent()+" SEGMENT=\"1\"/>\n";
                        }
                    } else if(child.getAttribute("open").equalsIgnoreCase(qName)) {
                        if(child.getAttribute("true").length()>0  && parent_status[j]){
                            int num = Integer.parseInt(child.getAttribute("true"));
                            if(parent_status[num] && parent_status[j]) {
                                parent_status[j] = false;
                                if(child.getAttribute("next").length()>0) {
                                    if(child.getAttribute("split").length()>0){
                                        String [] str = store.split(" ");
                                        if(str.length > 1) {
                                            convertToXYZ.buffer+= "<"+child.getAttribute("next")+">"+str[0]+"</"+child.getAttribute("next")+">\n";
                                        }
                                    }
                                }
                                convertToXYZ.buffer+= "</"+child.getTextContent()+">\n";            
                            }
                        }
                    } else if(child.getAttribute("true").length()>0 && !(child.getAttribute("open").length()>0)) {
                        int num = Integer.parseInt(child.getAttribute("true"));
                        if(parent_status[num]) {
                            parent_status[j] = true;
                            convertToXYZ.buffer+= "<"+child.getTextContent()+" SEGMENT=\"1\">\n";
                        }
                    } else {
                        parent_status[j] = true;
                        convertToXYZ.buffer+= "<"+child.getTextContent()+" SEGMENT=\"1\">\n";
                    }
                } else if(child.getAttribute("display").equalsIgnoreCase("nil") && qName.equalsIgnoreCase(child.getAttribute("abcName"))) {
                    parent_status[j] = true;
                }
            }
        }
    }

    public void endElement(String uri, String localName,
                    String qName) throws SAXException {
        for(int i=0;i<number_of_nodes;i++) {
            Node node= nodes.item(i);

            if(node instanceof Element) {
                Element child = (Element) node;
                if(child.getAttribute("store").equalsIgnoreCase(qName) || child.getAttribute("abcName")!= null && qName.equalsIgnoreCase(child.getAttribute("abcName"))) {  
                    tag_status[i] = false;
                }
            }
        }

        for(int j=0;j<number_of_parents;j++) {
            Node node= parentnodes.item(j);

            if(node instanceof Element) {
                Element child = (Element) node;
                if(!child.getAttribute("type").equalsIgnoreCase("closed") && child.getAttribute("abcName")!= null && qName.equalsIgnoreCase(child.getAttribute("abcName")) && !child.getAttribute("display").equalsIgnoreCase("nil") && !(child.getAttribute("closed").length()>0)) {
                    if(child.getAttribute("true").length()>0) {
                        int num = Integer.parseInt(child.getAttribute("true"));
                        if(parent_status[num]) {
                            parent_status[j] = false;
                            convertToXYZ.buffer+= "</"+child.getTextContent()+">\n";
                        }
                    } else if(!(child.getAttribute("closed").length()>0)) {
                        parent_status[j] = false;
                        convertToXYZ.buffer+= "</"+child.getTextContent()+">\n";
                    }
                } else if(child.getAttribute("closed").equalsIgnoreCase(qName) && !child.getAttribute("display").equalsIgnoreCase("nil")) {
                    if(child.getAttribute("true").length()>0){
                        int num = Integer.parseInt(child.getAttribute("true"));
                        if(parent_status[num]) {
                            parent_status[j] = true;
                            convertToXYZ.buffer+= "<"+child.getTextContent()+" SEGMENT=\"1\">\n";
                        }
                    }
                } else if(child.getAttribute("display").equalsIgnoreCase("nil") && qName.equalsIgnoreCase(child.getAttribute("abcName"))) {
                    parent_status[j] = false;
                }
            }
        }
    }

    public void characters(char ch[], int start, int length) throws SAXException {
        for(int i=0;i<number_of_nodes;i++) {
            if(tag_status[i]) {
                Node node = nodes.item(i);

                if(node instanceof Element) {
                    Element child = (Element)node;

                    if(child.getAttribute("overflow").length()>0){
                        int overflow = Integer.parseInt(child.getAttribute("overflow"));
                        if(length < overflow) {
                            convertToXYZ.buffer+="<"+child.getTextContent()+"1"+">"+new String(ch,start,length)+"</"+child.getTextContent()+"1"+">\n";
                        } else if(length >= overflow) {
                            for(int x=0;x<3;x++) {
                                if(length>=overflow) {
                                    convertToXYZ.buffer+="<"+child.getTextContent()+(x+1)+">"+new String(ch,start,overflow)+"</"+child.getTextContent()+(x+1)+">\n";
                                    length-= overflow;
                                    start+=overflow;
                                } else if(length < overflow && length > 0) {
                                    convertToXYZ.buffer+="<"+child.getTextContent()+(x+1)+">"+new String(ch,start,length)+"</"+child.getTextContent()+(x+1)+">\n";
                                    length = 0;
                                }
                            }
                        }
                    } 
                    else if(child.getAttribute("swap").length()>0 && length>0) {
                        String rep[] = child.getAttribute("swap").split(",");
                        String prts[] = new String [rep.length];
                        boolean other = false;
                        for(int y=0;y<rep.length;y++) {
                            String a[] = rep[y].split(":");
                            prts[y] = a[0];
                        }
                        for(int p=0;p<rep.length;p++) {
                            String a[] = rep[p].split(":");
                            if(a[0].equals(new String(ch,start,length))) {
                                        convertToXYZ.buffer+="<"+child.getTextContent()+">"+a[1]+"</"+child.getTextContent()+">\n";
                            } else if(a[0].equals("Others")) {
                                for(int z = 0 ;z< prts.length; z++) {
                                    if(!prts[z].equalsIgnoreCase(new String(ch,start,length))) {
                                        if(z== prts.length-1)
                                            other = true;

                                        continue;
                                    } else
                                        break;
                                }
                                if(other) {
                                    String b[] = a[1].split(";");
                                    int l= Integer.parseInt(b[0]);
                                    int m= Integer.parseInt(b[1]);

                                    if(l<0)
                                        l+=length;
                                    if(m<0)
                                        m+=length;

                                    start = (start+length)-(m-l)-1;
                                    length = m-l;

                                    convertToXYZ.buffer+="<"+child.getTextContent()+">"+new String(ch,start,length)+"</"+child.getTextContent()+">\n";
                                }
                            }
                        }
                    } 
                    else if(child.getAttribute("notTrue").length()>0 && length>0) {
                        int num = Integer.parseInt(child.getAttribute("notTrue"));

                        if(!parent_status[num] && child.getAttribute("characters").length()>0) {
                            String len[] = child.getAttribute("characters").split(",");
                            start += Integer.parseInt(len[0]);
                            length = Integer.parseInt(len[1]) -Integer.parseInt(len[0]);
                            convertToXYZ.buffer+="<"+child.getTextContent()+">"+new String(ch,start,length)+"</"+child.getTextContent()+">\n";
                        } else if(!parent_status[num])
                            convertToXYZ.buffer+="<"+child.getTextContent()+">"+new String(ch,start,length)+"</"+child.getTextContent()+">\n";
                    } 
                    else if(child.getAttribute("true").length()>0 && length>0) {
                        int num = Integer.parseInt(child.getAttribute("true"));

                        if(parent_status[num]) {
                            convertToXYZ.buffer+="<"+child.getTextContent()+">"+new String(ch,start,length)+"</"+child.getTextContent()+">\n";
                        }
                    } 
                    else if(child.getAttribute("constant").length()>0 && length>0) {
                        convertToXYZ.buffer+="<"+child.getTextContent()+">"+child.getAttribute("constant")+"</"+child.getTextContent()+">\n";
                    } 
                    else if(child.getAttribute("store").length()>0 && length>0) {
                        store = new String(ch,start,length);
                    } 
                    else if(length>0)
                        convertToXYZ.buffer+="<"+child.getTextContent()+">"+new String(ch,start,length)+"</"+child.getTextContent()+">\n";
                }
            }
        }
    }
};
saxParser.parse(InputFilePath, handler);

Please help me with this. I am unable to understand why the execution differs when I run the jar instead of the class.

0

There are 0 answers