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    
021    package org.granite.builder.properties;
022    
023    import org.granite.builder.util.StringUtil;
024    import org.granite.generator.TemplateUri;
025    import org.granite.generator.as3.reflect.JavaType.Kind;
026    
027    import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
028    
029    /**
030     * @author Franck WOLFF
031     */
032    public 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                            String[] uriArray = StringUtil.split(uris, ';');
075                            templateUris = new TemplateUri[uriArray.length];
076                            for (int i = uriArray.length - 1; i >= 0; i--)
077                                    templateUris[i] = new TemplateUri(uriArray[i], i > 0);
078                    }
079                    return templateUris;
080            }
081            
082            public void validate(ValidationResults results) {
083                    if (kind == null || uris == null)
084                            results.getErrors().add("templates: kind and uris cannot be null");
085            }
086    
087            @Override
088            public boolean equals(Object o) {
089                    return (o instanceof Gas3Template && kind.equals(((Gas3Template)o).kind));
090            }
091    
092            @Override
093            public int hashCode() {
094                    return kind.hashCode();
095            }
096    }