001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Alexandre Russel 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.gwt.client.annotea; 023 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation; 030import org.nuxeo.ecm.platform.annotations.gwt.client.model.Container; 031import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointer; 032import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointerFactory; 033 034import com.google.gwt.xml.client.Document; 035import com.google.gwt.xml.client.Node; 036import com.google.gwt.xml.client.NodeList; 037import com.google.gwt.xml.client.XMLParser; 038 039/** 040 * @author Alexandre Russel 041 */ 042public class RDFParser { 043 044 private static final String r = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 045 046 private static final String nx = "http://www.nuxeo.org/document/uid/"; 047 048 public List<Annotation> getAnnotationList(String response) { 049 List<Annotation> result = new ArrayList<Annotation>(); 050 Document document = XMLParser.parse(response); 051 Node root = document.getElementsByTagName("RDF").item(0); 052 NodeList nodeList = root.getChildNodes(); 053 for (int x = 0; x < nodeList.getLength(); x++) { 054 Annotation annotation = processAnnotation(nodeList.item(x)); 055 if (annotation != null) { 056 result.add(annotation); 057 } 058 } 059 return result; 060 } 061 062 public Annotation processAnnotation(Node item) { 063 if (item == null || item.getNamespaceURI() == null || item.getNodeName() == null) { 064 return null; 065 } 066 if (item.getNamespaceURI().equals(r) && item.getNodeName().endsWith(":Description")) { 067 String about = item.getAttributes().item(0).getNodeValue(); 068 String annotationUUID = about.substring(about.lastIndexOf(":") + 1); 069 return getAnnotation(annotationUUID, item.getChildNodes()); 070 } 071 return null; 072 } 073 074 public Annotation getAnnotation(String annotationUUID, NodeList list) { 075 Annotation annotation = new Annotation(annotationUUID); 076 Map<String, Statement> map = new HashMap<String, Statement>(); 077 Map<String, String> fields = new HashMap<String, String>(); 078 for (int x = 0; x < list.getLength(); x++) { 079 Node node = list.item(x); 080 if (node.getNodeName().equals("#text") || node.getNodeType() != Node.ELEMENT_NODE) { 081 continue; 082 } 083 if (node.getNamespaceURI().equals(nx)) { 084 String nodeName = node.getNodeName(); 085 String name = nodeName.substring(nodeName.indexOf(":") + 1); 086 if ("startContainer".equals(name)) { 087 annotation.setStartContainer(Container.fromString(node.getFirstChild().getNodeValue())); 088 continue; 089 } 090 if ("endContainer".equals(name)) { 091 annotation.setEndContainer(Container.fromString(node.getFirstChild().getNodeValue())); 092 continue; 093 } 094 fields.put(name, node.getFirstChild().getNodeValue()); 095 continue; 096 } 097 Statement statement = new Statement(node); 098 if (statement.getObject().equals("http://www.w3.org/2000/10/annotation-ns#Annotation")) { 099 continue; 100 } 101 map.put(statement.getPredicate(), statement); 102 } 103 annotation.setType(map.get(RDFConstant.R_TYPE).getObject()); 104 XPointer xpointer = XPointerFactory.getXPointer(map.get(RDFConstant.A_CONTEXT).getObject()); 105 106 annotation.setXpointer(xpointer); 107 if (map.containsKey(RDFConstant.H_BODY)) { 108 Statement s = map.get(RDFConstant.H_BODY); 109 annotation.setBody(parseXMLLiteralForBody(s.getObject())); 110 } else { 111 Statement bodyStatement = map.get(RDFConstant.A_BODY); 112 annotation.setBody(bodyStatement.getObject()); 113 if (bodyStatement.isResource()) { 114 annotation.setBodyUrl(true); 115 } 116 } 117 118 if (map.get(RDFConstant.D_CREATOR) != null && map.get(RDFConstant.D_CREATOR).getObject() != null) { 119 annotation.setAuthor(map.get(RDFConstant.D_CREATOR).getObject()); 120 } 121 if (map.get(RDFConstant.D_DATE) != null && map.get(RDFConstant.D_DATE).getObject() != null) { 122 annotation.setStringDate(map.get(RDFConstant.D_DATE).getObject()); 123 } 124 125 for (Map.Entry<String, Statement> entry : map.entrySet()) { 126 fields.put(entry.getKey(), entry.getValue().getObject()); 127 } 128 annotation.setFields(fields); 129 130 return annotation; 131 } 132 133 private String parseXMLLiteralForBody(String html) { 134 if (html.contains("<body>")) { 135 String body = "<body>"; 136 int beginIndex = html.indexOf(body) + body.length(); 137 int endIndex = html.indexOf("</body>"); 138 return html.substring(beginIndex, endIndex); 139 } else if (html.contains("<body>")) { 140 String body = "<body>"; 141 int beginIndex = html.indexOf(body) + body.length(); 142 int endIndex = html.indexOf("</body>"); 143 return html.substring(beginIndex, endIndex); 144 } 145 return ""; 146 } 147 148}