001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.annotea;
021
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation;
028import org.nuxeo.ecm.platform.annotations.gwt.client.model.Container;
029import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointer;
030import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointerFactory;
031
032import com.google.gwt.xml.client.Document;
033import com.google.gwt.xml.client.Node;
034import com.google.gwt.xml.client.NodeList;
035import com.google.gwt.xml.client.XMLParser;
036
037/**
038 * @author Alexandre Russel
039 */
040public class RDFParser {
041
042    private static final String r = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
043
044    private static final String nx = "http://www.nuxeo.org/document/uid/";
045
046    public List<Annotation> getAnnotationList(String response) {
047        List<Annotation> result = new ArrayList<Annotation>();
048        Document document = XMLParser.parse(response);
049        Node root = document.getElementsByTagName("RDF").item(0);
050        NodeList nodeList = root.getChildNodes();
051        for (int x = 0; x < nodeList.getLength(); x++) {
052            Annotation annotation = processAnnotation(nodeList.item(x));
053            if (annotation != null) {
054                result.add(annotation);
055            }
056        }
057        return result;
058    }
059
060    public Annotation processAnnotation(Node item) {
061        if (item == null || item.getNamespaceURI() == null || item.getNodeName() == null) {
062            return null;
063        }
064        if (item.getNamespaceURI().equals(r) && item.getNodeName().endsWith(":Description")) {
065            String about = item.getAttributes().item(0).getNodeValue();
066            String annotationUUID = about.substring(about.lastIndexOf(":") + 1);
067            return getAnnotation(annotationUUID, item.getChildNodes());
068        }
069        return null;
070    }
071
072    public Annotation getAnnotation(String annotationUUID, NodeList list) {
073        Annotation annotation = new Annotation(annotationUUID);
074        Map<String, Statement> map = new HashMap<String, Statement>();
075        Map<String, String> fields = new HashMap<String, String>();
076        for (int x = 0; x < list.getLength(); x++) {
077            Node node = list.item(x);
078            if (node.getNodeName().equals("#text") || node.getNodeType() != Node.ELEMENT_NODE) {
079                continue;
080            }
081            if (node.getNamespaceURI().equals(nx)) {
082                String nodeName = node.getNodeName();
083                String name = nodeName.substring(nodeName.indexOf(":") + 1);
084                if ("startContainer".equals(name)) {
085                    annotation.setStartContainer(Container.fromString(node.getFirstChild().getNodeValue()));
086                    continue;
087                }
088                if ("endContainer".equals(name)) {
089                    annotation.setEndContainer(Container.fromString(node.getFirstChild().getNodeValue()));
090                    continue;
091                }
092                fields.put(name, node.getFirstChild().getNodeValue());
093                continue;
094            }
095            Statement statement = new Statement(node);
096            if (statement.getObject().equals("http://www.w3.org/2000/10/annotation-ns#Annotation")) {
097                continue;
098            }
099            map.put(statement.getPredicate(), statement);
100        }
101        annotation.setType(map.get(RDFConstant.R_TYPE).getObject());
102        XPointer xpointer = XPointerFactory.getXPointer(map.get(RDFConstant.A_CONTEXT).getObject());
103
104        annotation.setXpointer(xpointer);
105        if (map.containsKey(RDFConstant.H_BODY)) {
106            Statement s = map.get(RDFConstant.H_BODY);
107            annotation.setBody(parseXMLLiteralForBody(s.getObject()));
108        } else {
109            Statement bodyStatement = map.get(RDFConstant.A_BODY);
110            annotation.setBody(bodyStatement.getObject());
111            if (bodyStatement.isResource()) {
112                annotation.setBodyUrl(true);
113            }
114        }
115
116        if (map.get(RDFConstant.D_CREATOR) != null && map.get(RDFConstant.D_CREATOR).getObject() != null) {
117            annotation.setAuthor(map.get(RDFConstant.D_CREATOR).getObject());
118        }
119        if (map.get(RDFConstant.D_DATE) != null && map.get(RDFConstant.D_DATE).getObject() != null) {
120            annotation.setStringDate(map.get(RDFConstant.D_DATE).getObject());
121        }
122
123        for (Map.Entry<String, Statement> entry : map.entrySet()) {
124            fields.put(entry.getKey(), entry.getValue().getObject());
125        }
126        annotation.setFields(fields);
127
128        return annotation;
129    }
130
131    private String parseXMLLiteralForBody(String html) {
132        if (html.contains("<body>")) {
133            String body = "<body>";
134            int beginIndex = html.indexOf(body) + body.length();
135            int endIndex = html.indexOf("</body>");
136            return html.substring(beginIndex, endIndex);
137        } else if (html.contains("&lt;body&gt;")) {
138            String body = "&lt;body&gt;";
139            int beginIndex = html.indexOf(body) + body.length();
140            int endIndex = html.indexOf("&lt;/body&gt;");
141            return html.substring(beginIndex, endIndex);
142        }
143        return "";
144    }
145
146}