001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.builder.properties;
022
023import org.granite.builder.util.StringUtil;
024import org.granite.generator.TemplateUri;
025import org.granite.generator.as3.reflect.JavaType.Kind;
026
027import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
028
029/**
030 * @author Franck WOLFF
031 */
032public class Gas3Template implements Validable {
033        
034        @XStreamAsAttribute
035        private Kind kind;
036
037        @XStreamAsAttribute
038        private String uris;
039        
040        private transient TemplateUri[] templateUris;
041
042        public Gas3Template(Kind kind, String uris) {
043                this.kind = kind;
044                this.uris = uris;
045        }
046
047        public Kind getKind() {
048                return kind;
049        }
050        public void setKind(Kind kind) {
051                this.kind = kind;
052        }
053
054        public String getUris() {
055                return uris;
056        }
057        public void setUris(String uris) {
058                this.uris = uris;
059                this.templateUris = null;
060        }
061        public void setUri(String uri, boolean base) {
062                String[] uriArray = StringUtil.split(uris, ';');
063                if (!base)
064                        uriArray[0] = uri;
065                else if (uriArray.length > 1)
066                        uriArray[1] = uri;
067                else
068                        uriArray = new String[]{uriArray[0], uri};
069                setUris(StringUtil.join(uriArray, ';'));
070        }
071
072        public TemplateUri[] getTemplateUris() {
073                if (templateUris == null) {
074                        if (uris.length() == 0 || uris.charAt(0) == ';')
075                                templateUris = new TemplateUri[0];
076                        else {
077                                String[] uriArray = StringUtil.split(uris, ';');
078                                templateUris = new TemplateUri[uriArray.length];
079                                for (int i = uriArray.length - 1; i >= 0; i--)
080                                        templateUris[i] = new TemplateUri(uriArray[i], i > 0);
081                        }
082                }
083                return templateUris;
084        }
085        
086        @Override
087        public void validate(ValidationResults results) {
088                if (kind == null || uris == null)
089                        results.getErrors().add("templates: kind and uris cannot be null");
090        }
091
092        @Override
093        public boolean equals(Object o) {
094                return (o instanceof Gas3Template && kind.equals(((Gas3Template)o).kind));
095        }
096
097        @Override
098        public int hashCode() {
099                return kind.hashCode();
100        }
101}