org/codehaus/griffon/runtime/core/ArtifactHandlerAdapter.java

`ArtifactHandlerAdapter.java`
`001 /* 002 * Copyright 2009-2012 the original author or authors. 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 017 package org.codehaus.griffon.runtime.core; 018 019 import griffon.core.ArtifactHandler; 020 import griffon.core.ArtifactInfo; 021 import griffon.core.GriffonApplication; 022 import griffon.core.GriffonClass; 023 import griffon.util.GriffonNameUtils; 024 025 import java.util.Collections; 026 import java.util.Map; 027 import java.util.TreeMap; 028 029 /** 030 * Base implementation of the ArtifactHandler interface. 031 * 032 * @author Andres Almiray 033 * @since 0.9.1 034 */ 035 public abstract class ArtifactHandlerAdapter implements ArtifactHandler { 036 private final String type; 037 private final String trailing; 038 private final GriffonApplication app; 039 040 private ArtifactInfo[] artifacts = new ArtifactInfo[0]; 041 private GriffonClass[] classes = new GriffonClass[0]; 042 private Map<String, GriffonClass> classesByName = new TreeMap<String, GriffonClass>(); 043 044 public ArtifactHandlerAdapter(GriffonApplication app, String type, String trailing) { 045 this.app = app; 046 this.type = type; 047 this.trailing = trailing == null ? "" : trailing; 048 } 049 050 public String getType() { 051 return type; 052 } 053 054 public String getTrailing() { 055 return trailing; 056 } 057 058 public void initialize(ArtifactInfo[] artifacts) { 059 this.artifacts = new ArtifactInfo[artifacts.length]; 060 System.arraycopy(artifacts, 0, this.artifacts, 0, artifacts.length); 061 classes = new GriffonClass[artifacts.length]; 062 for (int i = 0; i < artifacts.length; i++) { 063 Class clazz = artifacts[i].getClazz(); 064 classes[i] = newGriffonClassInstance(clazz); 065 classesByName.put(clazz.getName(), classes[i]); 066 } 067 } 068 069 protected abstract GriffonClass newGriffonClassInstance(Class clazz); 070 071 public Map<String, GriffonClass> getClassesByName() { 072 return Collections.<String, GriffonClass>unmodifiableMap(classesByName); 073 } 074 075 /** 076 * Returns true if the target Class is a class artifact 077 * handled by this object.<p> 078 * This implementation performs an equality check on class.name 079 */ 080 public boolean isArtifact(Class clazz) { 081 if (clazz == null) return false; 082 return classesByName.get(clazz.getName()) != null; 083 } 084 085 public boolean isArtifact(GriffonClass clazz) { 086 for (GriffonClass griffonClass : classes) { 087 if (griffonClass.equals(clazz)) return true; 088 } 089 return false; 090 } 091 092 public GriffonClass[] getClasses() { 093 return classes; 094 } 095 096 public ArtifactInfo[] getArtifacts() { 097 return artifacts; 098 } 099 100 public GriffonClass getClassFor(Class clazz) { 101 if (clazz == null) return null; 102 return getClassFor(clazz.getName()); 103 } 104 105 public GriffonClass getClassFor(String fqnClassName) { 106 if (GriffonNameUtils.isBlank(fqnClassName)) return null; 107 return classesByName.get(fqnClassName); 108 } 109 110 public GriffonClass findClassFor(String propertyName) { 111 if (GriffonNameUtils.isBlank(propertyName)) return null; 112 113 String simpleName = propertyName; 114 115 int lastDot = propertyName.lastIndexOf("."); 116 if (lastDot > -1) { 117 simpleName = simpleName.substring(lastDot + 1); 118 } 119 120 if (simpleName.length() == 1) { 121 simpleName = simpleName.toUpperCase(); 122 } else { 123 simpleName = simpleName.substring(0, 1).toUpperCase() + simpleName.substring(1); 124 } 125 126 if (!simpleName.endsWith(trailing)) { 127 simpleName += trailing; 128 } 129 130 for (GriffonClass griffonClass : classes) { 131 if (griffonClass.getClazz().getSimpleName().equals(simpleName)) return griffonClass; 132 } 133 134 return null; 135 } 136 137 public GriffonApplication getApp() { 138 return app; 139 } 140 }`