001/*
002 * (C) Copyright 2006-2016 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 *     bstefanescu
018 *     mguillaume
019 *     jcarsique
020 *     Yannis JULIENNE
021 */
022package org.nuxeo.connect.update.xml;
023
024import org.nuxeo.connect.update.PackageDependency;
025import org.nuxeo.connect.update.model.Field;
026import org.nuxeo.connect.update.model.Form;
027import org.nuxeo.connect.update.model.PackageDefinition;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class XmlSerializer extends XmlWriter {
033
034    public XmlSerializer() {
035    }
036
037    public XmlSerializer(String tab) {
038        super(tab);
039    }
040
041    public String toXML(PackageDefinition def) {
042        start("package");
043        if (def.getType() != null) {
044            attr("type", def.getType().getValue());
045        }
046        attr("name", def.getName());
047        if (def.getVersion() != null) {
048            attr("version", def.getVersion().toString());
049        }
050        startContent();
051
052        element("title", def.getTitle());
053        element("description", def.getDescription());
054        element("vendor", def.getVendor());
055        element("classifier", def.getClassifier());
056
057        element("hotreload-support", Boolean.valueOf(def.supportsHotReload()).toString());
058        element("require-terms-and-conditions-acceptance",
059                Boolean.valueOf(def.requireTermsAndConditionsAcceptance()).toString());
060
061        if (def.getInstaller() != null) {
062            start("installer");
063            attr("class", def.getInstaller().getType());
064            attr("restart", String.valueOf(def.getInstaller().getRequireRestart()));
065            end();
066        }
067        if (def.getUninstaller() != null) {
068            start("uninstaller");
069            attr("class", def.getUninstaller().getType());
070            attr("restart", String.valueOf(def.getUninstaller().getRequireRestart()));
071            end();
072        }
073        element("validator", def.getValidator());
074
075        if (def.getTargetPlatforms() != null && def.getTargetPlatforms().length > 0) {
076            start("platforms");
077            startContent();
078            for (String platform : def.getTargetPlatforms()) {
079                element("platform", platform);
080            }
081            end("platforms");
082        }
083
084        if (def.getTargetPlatformRange() != null || def.getTargetPlatformName() != null) {
085            start("target-platform");
086            startContent();
087            if (def.getTargetPlatformRange() != null) {
088                element("version", def.getTargetPlatformRange());
089            }
090            if (def.getTargetPlatformName() != null) {
091                element("name", def.getTargetPlatformName());
092            }
093            end("target-platform");
094        }
095
096        if (def.getDependencies() != null && def.getDependencies().length > 0) {
097            start("dependencies");
098            startContent();
099            for (PackageDependency dep : def.getDependencies()) {
100                element("package", dep.toString());
101            }
102            end("dependencies");
103        }
104
105        if (def.getOptionalDependencies() != null && def.getOptionalDependencies().length > 0) {
106            start("optional-dependencies");
107            startContent();
108            for (PackageDependency dep : def.getOptionalDependencies()) {
109                element("package", dep.toString());
110            }
111            end("optional-dependencies");
112        }
113
114        try {
115            def.getClass().getMethod("getConflicts");
116            if (def.getConflicts() != null && def.getConflicts().length > 0) {
117                start("conflicts");
118                startContent();
119                for (PackageDependency conflict : def.getConflicts()) {
120                    element("package", conflict.toString());
121                }
122                end("conflicts");
123            }
124        } catch (NoSuchMethodException e) {
125            // Ignore conflicts with old Connect Client versions
126        }
127
128        try {
129            def.getClass().getMethod("getProvides");
130            if (def.getProvides() != null && def.getProvides().length > 0) {
131                start("provides");
132                startContent();
133                for (PackageDependency provide : def.getProvides()) {
134                    element("package", provide.toString());
135                }
136                end("provides");
137            }
138        } catch (NoSuchMethodException e) {
139            // Ignore provides with old Connect Client versions
140        }
141
142        end("package");
143        return sb.toString();
144    }
145
146    public void buildXML(Form form) {
147        start("form");
148        startContent();
149        element("title", form.getTitle());
150        element("image", form.getImage());
151        element("description", form.getDescription());
152        if (form.getFields() != null && form.getFields().length > 0) {
153            start("fields");
154            startContent();
155            for (Field field : form.getFields()) {
156                start("field");
157                attr("name", field.getName());
158                attr("type", field.getType());
159                if (field.isRequired()) {
160                    attr("required", "true");
161                }
162                if (field.isReadOnly()) {
163                    attr("readonly", "true");
164                }
165                if (field.isVertical()) {
166                    attr("vertical", "true");
167                }
168                startContent();
169                element("label", field.getLabel());
170                element("value", field.getValue());
171                end("field");
172            }
173            end("fields");
174        }
175        end("form");
176    }
177
178    public String toXML(FormDefinition form) {
179        buildXML(form);
180        return sb.toString();
181    }
182
183    public String toXML(FormDefinition... forms) {
184        start("forms");
185        startContent();
186        for (FormDefinition form : forms) {
187            buildXML(form);
188        }
189        end("forms");
190        return sb.toString();
191    }
192
193    public String toXML(FormsDefinition forms) {
194        start("forms");
195        startContent();
196        for (Form form : forms.getForms()) {
197            buildXML(form);
198        }
199        end("forms");
200        return sb.toString();
201    }
202}