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