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