001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model.impl;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.ecm.webengine.loader.ClassProxy;
025import org.nuxeo.ecm.webengine.loader.StaticClassProxy;
026import org.nuxeo.ecm.webengine.model.Private;
027import org.nuxeo.ecm.webengine.model.Protected;
028import org.nuxeo.ecm.webengine.model.Public;
029import org.nuxeo.ecm.webengine.model.ResourceType;
030import org.nuxeo.ecm.webengine.model.TypeVisibility;
031import org.nuxeo.ecm.webengine.model.Utils;
032import org.nuxeo.ecm.webengine.model.WebObject;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037@XObject("web-object")
038public class TypeDescriptor implements Cloneable {
039
040    @XNode("@class")
041    void setClassProxy(Class<?> clazz) {
042        this.clazz = new StaticClassProxy(clazz);
043    }
044
045    public ClassProxy clazz;
046
047    @XNode("@name")
048    public String type;
049
050    @XNode("@fragment")
051    public String fragment;
052
053    @XNode("@superType")
054    public String superType = ResourceType.ROOT_TYPE_NAME;
055
056    @XNode("@visibility")
057    public void setVisibility(String v) {
058        if (v.equals("public")) {
059            visibility = TypeVisibility.PUBLIC;
060        } else if (v.equals("protected")) {
061            visibility = TypeVisibility.PROTECTED;
062        } else if (v.equals("private")) {
063            visibility = TypeVisibility.PRIVATE;
064        } else {
065            visibility = TypeVisibility.DEFAULT;
066        }
067    }
068
069    public int visibility = TypeVisibility.DEFAULT;
070
071    public TypeDescriptor() {
072    }
073
074    public TypeDescriptor(ClassProxy clazz, String type, String superType) {
075        this.clazz = clazz;
076        this.type = type;
077        this.superType = superType;
078        Class<?> k = clazz.get();
079        if (k.isAnnotationPresent(Public.class)) {
080            visibility = TypeVisibility.PUBLIC;
081        } else if (k.isAnnotationPresent(Protected.class)) {
082            visibility = TypeVisibility.PROTECTED;
083        } else if (k.isAnnotationPresent(Private.class)) {
084            visibility = TypeVisibility.PRIVATE;
085        }
086    }
087
088    public int getVisibility() {
089        return visibility;
090    }
091
092    @Override
093    public boolean equals(Object obj) {
094        if (obj == this) {
095            return true;
096        }
097        if (obj == null) {
098            return false;
099        }
100        if (obj instanceof TypeDescriptor) {
101            TypeDescriptor td = (TypeDescriptor) obj;
102            return type.equals(td.type) && Utils.streq(fragment, td.fragment);
103        }
104        return false;
105    }
106
107    @Override
108    public TypeDescriptor clone() {
109        try {
110            return (TypeDescriptor) super.clone();
111        } catch (CloneNotSupportedException e) {
112            throw new RuntimeException("Canot happen");
113        }
114    }
115
116    public String getId() {
117        return type;
118    }
119
120    public String getFragment() {
121        return fragment;
122    }
123
124    public boolean isMainFragment() {
125        return fragment == null;
126    }
127
128    public boolean isAdapter() {
129        return false;
130    }
131
132    public AdapterDescriptor asAdapterDescriptor() {
133        return null;
134    }
135
136    public TypeDescriptor asTypeDescriptor() {
137        return this;
138    }
139
140    public static TypeDescriptor fromAnnotation(ClassProxy clazz, WebObject type) {
141        return new TypeDescriptor(clazz, type.type(), type.superType());
142    }
143
144    @Override
145    public String toString() {
146        return type + " extends " + superType + " [" + clazz.getClassName() + "]";
147    }
148
149}