无法向ArrayList添加值,因为它在内部类中?

时间:2010-07-14 09:09:16

标签: java sax inner-classes

我正在编写一个可以解析rdf和owl文件的文件。我使用的是SAX和Java。

我的问题出在activeObject.add(file);

我收到错误“令牌上的语法错误,错位的构造” - 我不知道这意味着什么。它似乎没有意义,任何帮助都会非常感激。

PS:我可能完全错误导致错误的原因,它可能与内部类无关。

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

    // String file =
    // "http://www.srdc.metu.edu.tr/ubl/contextOntology/cpc.owl";
    // final String file = "http://onto.eva.mpg.de/obo/image.owl";
    // final String file =
    // "http://www.informatik.uni-ulm.de/ki/Liebig/owl/SUMO-LiteTB.rdf";
    // final String file =
    // "http://www.csd.abdn.ac.uk/~apreece/research/TowardsAnIntelligentWeb/PUB_findallbyKen_result.rdf";
    // final String file =
    // "http://www.srdc.metu.edu.tr/ubl/contextOntology/naics.owl";
    final String file = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

    URI uri = new URI(file);
    InputStream is = uri.toURL().openStream();

    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

            ArrayList<String> activeProperty = new ArrayList<String>();
            ArrayList<Triple> triplesList = new ArrayList<Triple>();
            ArrayList<String> activeObject = new ArrayList<String>();

            boolean name = false;
            activeObject.add(file);

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

                activeProperty.add(qName);
                int attrLength = attributes.getLength();

                for (int i = 0; i < attrLength; i++) {
                    String attrName = attributes.getQName(i).toLowerCase();
                    String attrValue = attributes.getValue(i).toLowerCase();

                    if (attrName.equals("rdf:about") || attrName.equals("rdf:resource") || attrName.equals("rdf:id")) {
                        activeObject.add(attrValue);
System.out.println(activeObject);
                        }

                    else{
                        String subject = activeObject.get(activeObject.size()-1);
                        String predicate = attrName;
                        String object = attrValue;
                        Triple newTriple = new Triple(subject, predicate, object);          
                        }
                    }
                }

            public void characters(char[] ch, int start, int length) throws SAXException {
//                      tempVal = new String(ch, start, length);
            }

            public void endElement(String uri, String localName, String rawName) {

                String subject = activeObject.get(activeObject.size()-2);
                String predicate = activeProperty.get(activeProperty.size()-1);
                String object = activeObject.get(activeObject.size()-1);

                Triple newTriple = new Triple(subject,predicate, object);

                if (rawName.equals(activeProperty.get(activeProperty.size() - 1))) {
                    activeProperty.remove(activeProperty.size() - 1);
                }
                else{
                    System.out.println("Something is seriosuly wrong ...sdf.sdf.we8ryw98fsydh");
                }
 //                 System.out.println(activeProperty);
            }

            // if (qName.equalsIgnoreCase("NAME")) {
            // name = true;
            // }

            // public void characters(char ch[], int start, int length)
            // throws SAXException {
            // if (name) {
            // System.out.println("Name: "
            // + new String(ch, start, length));
            // name = false;
            // }
            // }
        };

        saxParser.parse(is, handler);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:3)

您正试图在匿名类中编写正常语句,就像您尝试过这样:

class Foo extends DefaultHandler {
    ArrayList<String> activeProperty = new ArrayList<String>();
    ArrayList<Triple> triplesList = new ArrayList<Triple>();
    ArrayList<String> activeObject = new ArrayList<String>();

    boolean name = false;
    activeObject.add(file);
}

你做不到。如果您希望在构造上执行此操作,可以将其放在初始化程序块中,如下所示:

ArrayList<String> activeProperty = new ArrayList<String>();
ArrayList<Triple> triplesList = new ArrayList<Triple>();
ArrayList<String> activeObject = new ArrayList<String>();
boolean name = false;

{
    activeObject.add(file);
}

或者你可以用其他方式填充列表 - 例如你可以用Guava编写:

ArrayList<String> activeObject = Lists.newArrayList(file);