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.Utils; 034import org.nuxeo.ecm.webengine.model.WebObject; 035 036/** 037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 038 */ 039@XObject("web-object") 040public class TypeDescriptor implements Cloneable { 041 042 @XNode("@class") 043 void setClassProxy(Class<?> clazz) { 044 this.clazz = new StaticClassProxy(clazz); 045 } 046 047 public ClassProxy clazz; 048 049 @XNode("@name") 050 public String type; 051 052 @XNode("@fragment") 053 public String fragment; 054 055 @XNode("@superType") 056 public String superType = ResourceType.ROOT_TYPE_NAME; 057 058 @XNode("@visibility") 059 public void setVisibility(String v) { 060 if (v.equals("public")) { 061 visibility = TypeVisibility.PUBLIC; 062 } else if (v.equals("protected")) { 063 visibility = TypeVisibility.PROTECTED; 064 } else if (v.equals("private")) { 065 visibility = TypeVisibility.PRIVATE; 066 } else { 067 visibility = TypeVisibility.DEFAULT; 068 } 069 } 070 071 public int visibility = TypeVisibility.DEFAULT; 072 073 public TypeDescriptor() { 074 } 075 076 public TypeDescriptor(ClassProxy clazz, String type, String superType) { 077 this.clazz = clazz; 078 this.type = type; 079 this.superType = superType; 080 Class<?> k = clazz.get(); 081 if (k.isAnnotationPresent(Public.class)) { 082 visibility = TypeVisibility.PUBLIC; 083 } else if (k.isAnnotationPresent(Protected.class)) { 084 visibility = TypeVisibility.PROTECTED; 085 } else if (k.isAnnotationPresent(Private.class)) { 086 visibility = TypeVisibility.PRIVATE; 087 } 088 } 089 090 public int getVisibility() { 091 return visibility; 092 } 093 094 @Override 095 public boolean equals(Object obj) { 096 if (obj == this) { 097 return true; 098 } 099 if (obj == null) { 100 return false; 101 } 102 if (obj instanceof TypeDescriptor) { 103 TypeDescriptor td = (TypeDescriptor) obj; 104 return type.equals(td.type) && Utils.streq(fragment, td.fragment); 105 } 106 return false; 107 } 108 109 @Override 110 public TypeDescriptor clone() { 111 try { 112 return (TypeDescriptor) super.clone(); 113 } catch (CloneNotSupportedException e) { 114 throw new RuntimeException("Canot happen"); 115 } 116 } 117 118 public String getId() { 119 return type; 120 } 121 122 public String getFragment() { 123 return fragment; 124 } 125 126 public boolean isMainFragment() { 127 return fragment == null; 128 } 129 130 public boolean isAdapter() { 131 return false; 132 } 133 134 public AdapterDescriptor asAdapterDescriptor() { 135 return null; 136 } 137 138 public TypeDescriptor asTypeDescriptor() { 139 return this; 140 } 141 142 public static TypeDescriptor fromAnnotation(ClassProxy clazz, WebObject type) { 143 return new TypeDescriptor(clazz, type.type(), type.superType()); 144 } 145 146 @Override 147 public String toString() { 148 return type + " extends " + superType + " [" + clazz.getClassName() + "]"; 149 } 150 151}