001/*
002 * (C) Copyright 2014 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.targetplatforms.core.descriptors;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.apache.xml.serialize.OutputFormat;
028import org.nuxeo.common.xmap.DOMSerializer;
029import org.nuxeo.common.xmap.annotation.XContent;
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XNodeList;
032import org.w3c.dom.DocumentFragment;
033
034/**
035 * Common descriptor for target packages/platforms.
036 *
037 * @since 5.7.1
038 */
039@SuppressWarnings("deprecation")
040public class TargetDescriptor {
041
042    private static final Log log = LogFactory.getLog(TargetDescriptor.class);
043
044    @XNode("@id")
045    String id;
046
047    @XNode("@enabled")
048    Boolean enabled;
049
050    @XNode("@restricted")
051    Boolean restricted;
052
053    @XNode("@deprecated")
054    Boolean deprecated;
055
056    @XNode("@parent")
057    String parent;
058
059    @XNode("name")
060    String name;
061
062    @XNode("version")
063    String version;
064
065    @XNode("refVersion")
066    String refVersion;
067
068    @XNode("label")
069    String label;
070
071    @XNode("status")
072    String status;
073
074    @XNode("releaseDate")
075    String releaseDate;
076
077    @XNode("endOfAvailability")
078    String endOfAvailability;
079
080    @XNode("downloadLink")
081    String downloadLink;
082
083    // retrieve HTML tags => introspect DOM on setter
084    String description;
085
086    @XContent("description")
087    public void setDescription(DocumentFragment descriptionDOM) {
088        try {
089            OutputFormat of = new OutputFormat();
090            of.setOmitXMLDeclaration(true);
091            description = DOMSerializer.toString(descriptionDOM, of).trim();
092        } catch (IOException e) {
093            log.error(e, e);
094        }
095    }
096
097    @XNodeList(value = "types/type", type = ArrayList.class, componentType = String.class)
098    List<String> types;
099
100    public boolean isEnableSet() {
101        return enabled != null;
102    }
103
104    public boolean isEnabled() {
105        return enabled == null || Boolean.TRUE.equals(enabled);
106    }
107
108    // needed for contributions merge
109    public void setEnabled(boolean enabled) {
110        this.enabled = Boolean.valueOf(enabled);
111    }
112
113    public String getId() {
114        return id;
115    }
116
117    public boolean isRestricted() {
118        return restricted != null && Boolean.TRUE.equals(restricted);
119    }
120
121    public boolean isDeprecated() {
122        return deprecated != null && Boolean.TRUE.equals(deprecated);
123    }
124
125    public String getParent() {
126        return parent;
127    }
128
129    public String getName() {
130        return name;
131    }
132
133    public String getVersion() {
134        return version;
135    }
136
137    public String getRefVersion() {
138        return refVersion;
139    }
140
141    public String getLabel() {
142        return label;
143    }
144
145    public String getStatus() {
146        return status;
147    }
148
149    public String getReleaseDate() {
150        return releaseDate;
151    }
152
153    public String getEndOfAvailability() {
154        return endOfAvailability;
155    }
156
157    public String getDownloadLink() {
158        return downloadLink;
159    }
160
161    public String getDescription() {
162        return description;
163    }
164
165    public List<String> getTypes() {
166        return types;
167    }
168
169    public boolean matchesType(String type) {
170        if (types == null) {
171            return false;
172        }
173        return types.contains(type);
174    }
175
176    @Override
177    public TargetDescriptor clone() {
178        TargetDescriptor clone = new TargetDescriptor();
179        doClone(clone);
180        return clone();
181    }
182
183    protected void doClone(TargetDescriptor clone) {
184        clone.id = id;
185        clone.enabled = enabled;
186        clone.restricted = restricted;
187        clone.deprecated = deprecated;
188        clone.parent = parent;
189        clone.name = name;
190        clone.version = version;
191        clone.refVersion = refVersion;
192        clone.label = label;
193        clone.status = status;
194        clone.releaseDate = releaseDate;
195        clone.endOfAvailability = endOfAvailability;
196        clone.downloadLink = downloadLink;
197        clone.description = description;
198        if (types != null) {
199            clone.types = new ArrayList<>(types);
200        }
201    }
202}