2010-07-14 6 views
1

rdf 및 owl 파일을 구문 분석 할 수있는 파일을 작성 중입니다. 나는 SAX와 자바를 사용하고있다. 나는 이것이 무엇을 의미하는지 모르는 -내부 클래스이므로 ArrayList에 값을 추가 할 수 없습니까?

내 문제는 "잘못 배치 된 구조 (들) 토큰에 구문 오류"나는 오류 라인 activeObject.add(file);

에 있습니다. 그리고 그것은 이해가되지 않는 것 같습니다, 어떤 도움을 많이 주시면 감사하겠습니다.

추신 : 나는 오류의 원인에 대해 완전히 틀릴 수도 있으며, 내부 클래스와 아무 관련이 없을 수도 있습니다.

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

감사합니다. 이니셜 라이저 블록이 작동했습니다. 이제 나는 그것이 무엇인지, 그것이 무엇인지 찾아 볼 필요가 있습니다. 감사. – Ankur

관련 문제