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 * troger 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.platform.annotations.gwt.client.annotea; 023 024import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.AnnotationDefinition; 025import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.WebConfiguration; 026import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation; 027import org.nuxeo.ecm.platform.annotations.gwt.client.util.AnnotationUtils; 028import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPointer; 029 030import com.google.gwt.user.client.Window; 031 032/** 033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 034 */ 035public class AnnotationXmlGenerator { 036 037 private final WebConfiguration webConfiguration; 038 039 private final Annotation annotation; 040 041 private String annotationXml = "<?xml version=\"1.0\"?>" 042 + "<r:RDF xmlns:a=\"http://www.w3.org/2000/10/annotation-ns#\" xmlns:r=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" 043 + " xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:h=\"http://www.w3.org/1999/xx/http#\"" 044 + " xmlns:nx=\"http://www.nuxeo.org/document/uid/\">" + " <r:Description>" 045 + " <r:type r:resource=\"http://www.w3.org/2000/10/annotation-ns#Annotation\" />" 046 + " <r:type r:resource=\"${uri}\" />" + " <a:annotates r:resource=\"${annotates}\" /> " 047 + " <a:context>${context}</a:context> " + " <a:body r:parseType=\"Literal\">${body}</a:body>" 048 + " ${fields}" + " ${startContainer}" + " ${endContainer}" + " </r:Description></r:RDF> "; 049 050 public AnnotationXmlGenerator(WebConfiguration webConfiguration, Annotation annotation) { 051 this.webConfiguration = webConfiguration; 052 this.annotation = annotation; 053 } 054 055 public String generateXml() { 056 replaceURI(); 057 replaceXPointer(); 058 replaceAnnotate(); 059 replaceBody(); 060 replaceFields(); 061 replaceStartContainer(); 062 replaceEndContainer(); 063 return annotationXml; 064 } 065 066 private void replaceURI() { 067 AnnotationDefinition annotationDefinition = webConfiguration.getAnnotationDefinition(annotation.getShortType()); 068 annotationXml = annotationXml.replace("${uri}", annotationDefinition.getUri()); 069 070 } 071 072 private void replaceXPointer() { 073 XPointer xpointer = annotation.getXpointer(); 074 annotationXml = annotationXml.replace("${context}", xpointer.getXpointerString()); 075 } 076 077 private void replaceAnnotate() { 078 String href = Window.Location.getHref(); 079 if (href.contains("?")) { 080 annotationXml = annotationXml.replace("${annotates}", href.substring(0, href.indexOf('?'))); 081 } else { 082 annotationXml = annotationXml.replace("${annotates}", href); 083 } 084 } 085 086 private void replaceBody() { 087 String encodedBody = AnnotationUtils.escapeHtml(annotation.getBody()); 088 annotationXml = annotationXml.replace("${body}", encodedBody); 089 } 090 091 private void replaceFields() { 092 String fields = ""; 093 for (String fieldName : annotation.getFields().keySet()) { 094 fields += "<nx:" + fieldName + " r:parseType=\"Literal\">" + annotation.getFields().get(fieldName) 095 + "</nx:" + fieldName + ">"; 096 } 097 annotationXml = annotationXml.replace("${fields}", fields); 098 } 099 100 private void replaceStartContainer() { 101 String replacement = ""; 102 if (annotation.hasStartContainer()) { 103 replacement = "<nx:startContainer r:parseType=\"Literal\">" 104 + annotation.getStartContainer().generateString() + "</nx:startContainer>"; 105 106 } 107 annotationXml = annotationXml.replace("${startContainer}", replacement); 108 109 } 110 111 private void replaceEndContainer() { 112 String replacement = ""; 113 if (annotation.hasEndContainer()) { 114 replacement = "<nx:endContainer r:parseType=\"Literal\">" + annotation.getEndContainer().generateString() 115 + "</nx:endContainer>"; 116 } 117 annotationXml = annotationXml.replace("${endContainer}", replacement); 118 } 119 120}