通过jar文件执行时不会调用匿名内部类

时间:2015-06-18 08:59:36

标签: java jar xml-parsing sax anonymous-inner-class

我编写了一个程序,使用java SAX解析器读取XML文件,修改标记并将其写入另一个XML文件。

当我使用java convertToXYZ运行类时,程序运行正常,但是当我构建一个jar并尝试使用java -jar XMLGenerator.jar运行它时,它不会进入startDocument() } {或startElement() DefaultHandler匿名内部类的方法。我得到一个XML文件,其中包含很少的标记,这些标记不在内部类中(我在外部硬编码)。是因为没有建立匿名内部类吗?

jar文件中存在匿名内部类文件convertToXYZ$1.class

我甚至尝试为DefaultHandler使用命名的内部类......但这也没有成功。

以下是匿名内部类的片段:

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);

请帮我解决这个问题。当我运行jar而不是类时,我无法理解为什么执行会有所不同。

0 个答案:

没有答案