001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
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: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.mivisitor.jdo;
018:
019: import org.objectweb.speedo.generation.mivisitor.AbstractMetaInfoVisitor;
020: import org.objectweb.speedo.lib.Personality;
021: import org.objectweb.speedo.metadata.SpeedoPackage;
022: import org.objectweb.speedo.metadata.SpeedoExtension;
023: import org.objectweb.speedo.metadata.SpeedoElement;
024: import org.objectweb.speedo.metadata.SpeedoClass;
025: import org.objectweb.speedo.metadata.SpeedoField;
026: import org.objectweb.speedo.metadata.SpeedoMap;
027: import org.objectweb.speedo.metadata.SpeedoCollection;
028: import org.objectweb.speedo.api.SpeedoException;
029: import org.objectweb.speedo.api.SpeedoProperties;
030: import org.objectweb.util.monolog.api.BasicLevel;
031:
032: import java.util.List;
033: import java.util.Collections;
034: import java.util.ArrayList;
035:
036: /**
037: * This class checks if the specified speedo extensions in .jdo file are supported.
038: * A warning (logging) is print for the unknown extensions.
039: *
040: * @author S.Chassande-Barrioz
041: */
042: public class ExtensionVerifier extends AbstractMetaInfoVisitor
043: implements SpeedoProperties {
044:
045: public ExtensionVerifier(Personality p) {
046: super (p);
047: }
048:
049: protected String getLoggerName() {
050: return super .getLoggerName() + ".extensionverifier";
051: }
052:
053: public void visitPackage(SpeedoPackage sp) throws SpeedoException {
054: super .visitPackage(sp);
055: StringBuffer sb = new StringBuffer();
056: sb.append("package ");
057: sb.append(sp.name);
058: sb.append(" in the .jdo file ");
059: sb.append(sp.xmlDescriptor.xmlFile);
060: checkSpeedoElement(sp, EXTENSION_PACKAGE_IDX, sb.toString());
061: }
062:
063: public void visitClass(SpeedoClass sc) throws SpeedoException {
064: super .visitClass(sc);
065: StringBuffer sb = new StringBuffer();
066: sb.append("class ");
067: sb.append(sc.getFQName());
068: sb.append(" in the .jdo file ");
069: sb.append(sc.getXMLFileName());
070: checkSpeedoElement(sc, EXTENSION_CLASS_IDX, sb.toString());
071: }
072:
073: public void visitField(SpeedoField sf) throws SpeedoException {
074: super .visitField(sf);
075: StringBuffer sb = new StringBuffer();
076: sb.append("field ");
077: sb.append(sf.name);
078: sb.append("of class ");
079: sb.append(sf.moClass.getFQName());
080: sb.append(" in the .jdo file ");
081: sb.append(sf.moClass.getXMLFileName());
082: checkSpeedoElement(sf, EXTENSION_FIELD_IDX, sb.toString());
083: if (sf.jdoTuple != null) {
084: if (sf.jdoTuple instanceof SpeedoMap) {
085: sb.insert(0, "map ");
086: checkSpeedoElement(sf.jdoTuple, EXTENSION_MAP_IDX, sb
087: .toString());
088: } else if (sf.jdoTuple instanceof SpeedoCollection) {
089: sb.insert(0, "collection ");
090: checkSpeedoElement(sf.jdoTuple,
091: EXTENSION_COLLECTION_IDX, sb.toString());
092: }
093: }
094: }
095:
096: private void checkSpeedoElement(SpeedoElement element, int nodeIdx,
097: String nodeName) {
098: List exts = getSpeedoExtension(element);
099: String unexpected = getUnexpectedExtensionStr(exts, nodeIdx);
100: if (unexpected != null) {
101: logger.log(BasicLevel.WARN,
102: "Unsupported speedo extension found for the "
103: + nodeName + unexpected);
104: }
105: }
106:
107: private String getUnexpectedExtensionStr(List exts, int node) {
108: StringBuffer sb = new StringBuffer();
109: String sep = "";
110: boolean unexpectedFound = false;
111: List supported = (List) SUPPORTED_EXTENSION.get(node);
112: for (int i = 0; i < exts.size(); i++) {
113: SpeedoExtension se = (SpeedoExtension) exts.get(i);
114: boolean uf = supported.isEmpty()
115: || !supported.contains(se.key);
116: if (uf) {
117: sb.append(sep);
118: sep = ", ";
119: unexpectedFound = true;
120: sb.append("(");
121: sb.append(se.key);
122: sb.append("=>");
123: sb.append(se.value);
124: sb.append(")");
125: }
126: }
127: if (unexpectedFound) {
128: sb.insert(0, "{");
129: sb.append("}");
130: return sb.toString();
131: } else {
132: return null;
133: }
134: }
135:
136: private List getSpeedoExtension(SpeedoElement element) {
137: List exts = element.jdoExtension;
138: if (exts.isEmpty()) {
139: return Collections.EMPTY_LIST;
140: }
141: ArrayList result = new ArrayList(exts.size());
142: for (int i = 0; i < exts.size(); i++) {
143: SpeedoExtension se = (SpeedoExtension) exts.get(i);
144: if (VENDOR_NAME.equals(se.vendorName)) {
145: result.add(se);
146: }
147: }
148: return result;
149: }
150: }
|