2010-07-14 87 views
1

我正在写一个可以解析rdf和owl文件的文件。我正在使用SAX和Java。无法向ArrayList添加值,因为它位于内部类中?

我的问题是上线activeObject.add(file);

我得到的错误“语法错误上令牌,错位结构(S)” - 我不知道这意味着什么。它似乎没有道理,任何帮助将不胜感激。 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(); 
    } 
} 

回答

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

感谢Jon,初始化块工作。现在我只需要查看它是什么以及它做了什么。谢谢。 – Ankur 2010-07-14 09:21:31

相关问题