KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > mivisitor > ExtensionVerifier


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.generation.mivisitor;
19
20 import org.objectweb.speedo.metadata.SpeedoPackage;
21 import org.objectweb.speedo.metadata.SpeedoExtension;
22 import org.objectweb.speedo.metadata.SpeedoElement;
23 import org.objectweb.speedo.metadata.SpeedoClass;
24 import org.objectweb.speedo.metadata.SpeedoField;
25 import org.objectweb.speedo.metadata.SpeedoMap;
26 import org.objectweb.speedo.metadata.SpeedoCollection;
27 import org.objectweb.speedo.api.SpeedoException;
28 import org.objectweb.speedo.api.SpeedoProperties;
29 import org.objectweb.util.monolog.api.BasicLevel;
30
31 import java.util.List JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 /**
36  * This class checks if the specified speedo extensions in .jdo file are supported.
37  * A warning (logging) is print for the unknown extensions.
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public class ExtensionVerifier
42     extends AbstractMetaInfoVisitor
43     implements SpeedoProperties {
44
45     protected String JavaDoc getLoggerName() {
46         return super.getLoggerName() + ".extensionverifier";
47     }
48
49     public void visitPackage(SpeedoPackage sp) throws SpeedoException {
50         super.visitPackage(sp);
51         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
52         sb.append("package ");
53         sb.append(sp.name);
54         sb.append(" in the .jdo file ");
55         sb.append(sp.jdoXMLDescriptor.xmlFile);
56         checkSpeedoElement(sp, EXTENSION_PACKAGE_IDX, sb.toString());
57     }
58
59     public void visitClass(SpeedoClass sc) throws SpeedoException {
60         super.visitClass(sc);
61         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
62         sb.append("class ");
63         sb.append(sc.getFQName());
64         sb.append(" in the .jdo file ");
65         sb.append(sc.getJDOFileName());
66         checkSpeedoElement(sc, EXTENSION_CLASS_IDX, sb.toString());
67     }
68
69     public void visitField(SpeedoField sf) throws SpeedoException {
70         super.visitField(sf);
71         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
72         sb.append("field ");
73         sb.append(sf.name);
74         sb.append("of class ");
75         sb.append(sf.jdoClass.getFQName());
76         sb.append(" in the .jdo file ");
77         sb.append(sf.jdoClass.getJDOFileName());
78         checkSpeedoElement(sf, EXTENSION_FIELD_IDX, sb.toString());
79         if (sf.jdoTuple != null) {
80             if (sf.jdoTuple instanceof SpeedoMap) {
81                 sb.insert(0, "map ");
82                 checkSpeedoElement(sf.jdoTuple, EXTENSION_MAP_IDX, sb.toString());
83             } else if (sf.jdoTuple instanceof SpeedoCollection) {
84                 sb.insert(0, "collection ");
85                 checkSpeedoElement(sf.jdoTuple, EXTENSION_COLLECTION_IDX, sb.toString());
86             }
87         }
88     }
89
90     private void checkSpeedoElement(SpeedoElement element,
91                                     int nodeIdx,
92                                     String JavaDoc nodeName) {
93         List JavaDoc exts = getSpeedoExtension(element);
94         String JavaDoc unexpected = getUnexpectedExtensionStr(exts, nodeIdx);
95         if (unexpected != null) {
96             logger.log(BasicLevel.WARN,
97                 "Unsupported speedo extension found for the "
98                 + nodeName + unexpected);
99         }
100     }
101
102     private String JavaDoc getUnexpectedExtensionStr(List JavaDoc exts, int node) {
103         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
104         String JavaDoc sep = "";
105         boolean unexpectedFound = false;
106         List JavaDoc supported = (List JavaDoc) SUPPORTED_EXTENSION.get(node);
107         for(int i=0; i<exts.size(); i++) {
108             SpeedoExtension se = (SpeedoExtension) exts.get(i);
109             boolean uf = supported.isEmpty() || !supported.contains(se.key);
110             if (uf) {
111                 sb.append(sep);
112                 sep = ", ";
113                 unexpectedFound = true;
114                 sb.append("(");
115                 sb.append(se.key);
116                 sb.append("=>");
117                 sb.append(se.value);
118                 sb.append(")");
119             }
120         }
121         if (unexpectedFound) {
122             sb.insert(0, "{");
123             sb.append("}");
124             return sb.toString();
125         } else {
126             return null;
127         }
128     }
129
130
131     private List JavaDoc getSpeedoExtension(SpeedoElement element) {
132         List JavaDoc exts = element.jdoExtension;
133         if(exts.isEmpty()) {
134             return Collections.EMPTY_LIST;
135         }
136         ArrayList JavaDoc result = new ArrayList JavaDoc(exts.size());
137         for(int i=0; i<exts.size(); i++) {
138             SpeedoExtension se = (SpeedoExtension) exts.get(i);
139             if (VENDOR_NAME.equals(se.vendorName)) {
140                 result.add(se);
141             }
142         }
143         return result;
144     }
145 }
146
Popular Tags