001/*
002 * (C) Copyright 2006-2008 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 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XObject;
026import org.nuxeo.ecm.webengine.loader.ClassProxy;
027import org.nuxeo.ecm.webengine.loader.StaticClassProxy;
028import org.nuxeo.ecm.webengine.model.Private;
029import org.nuxeo.ecm.webengine.model.Protected;
030import org.nuxeo.ecm.webengine.model.Public;
031import org.nuxeo.ecm.webengine.model.ResourceType;
032import org.nuxeo.ecm.webengine.model.TypeVisibility;
033import org.nuxeo.ecm.webengine.model.WebObject;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@XObject("web-object")
039public class TypeDescriptor implements Cloneable {
040
041    @XNode("@class")
042    void setClassProxy(Class<?> clazz) {
043        this.clazz = new StaticClassProxy(clazz);
044    }
045
046    public ClassProxy clazz;
047
048    @XNode("@name")
049    public String type;
050
051    @XNode("@fragment")
052    public String fragment;
053
054    @XNode("@superType")
055    public String superType = ResourceType.ROOT_TYPE_NAME;
056
057    @XNode("@visibility")
058    public void setVisibility(String v) {
059        if (v.equals("public")) {
060            visibility = TypeVisibility.PUBLIC;
061        } else if (v.equals("protected")) {
062            visibility = TypeVisibility.PROTECTED;
063        } else if (v.equals("private")) {
064            visibility = TypeVisibility.PRIVATE;
065        } else {
066            visibility = TypeVisibility.DEFAULT;
067        }
068    }
069
070    public int visibility = TypeVisibility.DEFAULT;
071
072    public TypeDescriptor() {
073    }
074
075    public TypeDescriptor(ClassProxy clazz, String type, String superType) {
076        this.clazz = clazz;
077        this.type = type;
078        this.superType = superType;
079        Class<?> k = clazz.get();
080        if (k.isAnnotationPresent(Public.class)) {
081            visibility = TypeVisibility.PUBLIC;
082        } else if (k.isAnnotationPresent(Protected.class)) {
083            visibility = TypeVisibility.PROTECTED;
084        } else if (k.isAnnotationPresent(Private.class)) {
085            visibility = TypeVisibility.PRIVATE;
086        }
087    }
088
089    public int getVisibility() {
090        return visibility;
091    }
092
093
094    @Override
095    public int hashCode() {
096        final int prime = 31;
097        int result = 1;
098        result = prime * result + ((type == null) ? 0 : type.hashCode());
099        result = prime * result + ((fragment == null) ? 0 : fragment.hashCode());
100        return result;
101    }
102
103    @Override
104    public boolean equals(Object obj) {
105        if (this == obj) {
106            return true;
107        }
108        if (obj == null) {
109            return false;
110        }
111        if (!(obj instanceof TypeDescriptor)) {
112            return false;
113        }
114        TypeDescriptor other = (TypeDescriptor) obj;
115        if (type == null) {
116            if (other.type != null) {
117                return false;
118            }
119        } else if (!type.equals(other.type)) {
120            return false;
121        }
122        if (fragment == null) {
123            if (other.fragment != null) {
124                return false;
125            }
126        } else if (!fragment.equals(other.fragment)) {
127            return false;
128        }
129        return true;
130    }
131
132    @Override
133    public TypeDescriptor clone() {
134        try {
135            return (TypeDescriptor) super.clone();
136        } catch (CloneNotSupportedException e) {
137            throw new RuntimeException("Canot happen");
138        }
139    }
140
141    public String getId() {
142        return type;
143    }
144
145    public String getFragment() {
146        return fragment;
147    }
148
149    public boolean isMainFragment() {
150        return fragment == null;
151    }
152
153    public boolean isAdapter() {
154        return false;
155    }
156
157    public AdapterDescriptor asAdapterDescriptor() {
158        return null;
159    }
160
161    public TypeDescriptor asTypeDescriptor() {
162        return this;
163    }
164
165    public static TypeDescriptor fromAnnotation(ClassProxy clazz, WebObject type) {
166        return new TypeDescriptor(clazz, type.type(), type.superType());
167    }
168
169    @Override
170    public String toString() {
171        return type + " extends " + superType + " [" + clazz.getClassName() + "]";
172    }
173
174}