views:

50

answers:

1

I am writing a file that can parse rdf and owl files. I am using SAX and Java.

My problem is on the line activeObject.add(file);

I get the error "Syntax error on tokens, Misplaced construct(s)" - I don't know what this means. And it doesn't seem to make sense, any help would be much appreciated.

PS: I might be completely wrong about what is causing the error, it might have nothing to do with an inner class.

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  A: 

You're trying to write normal statements within an anonymous class, as if you'd tried this:

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

You can't do that. If you want this to be performed on construction, you can put it in an initializer block, like this:

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

{
    activeObject.add(file);
}

Or you could populate the list in some other way, perhaps - for instance with Guava you could write:

ArrayList<String> activeObject = Lists.newArrayList(file);
Jon Skeet
Thanks Jon, the initializer block worked. Now I just need to look up what it is and what it does. Thanks.
Ankur