001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.tmap.model.validation;
020:
021: import java.io.File;
022: import org.netbeans.api.project.Project;
023: import org.netbeans.modules.xml.axi.AXIComponent;
024: import org.netbeans.modules.xslt.model.XslModel;
025: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
026: import org.netbeans.modules.xslt.tmap.model.api.TMapModel;
027: import org.netbeans.modules.xslt.tmap.model.api.Transform;
028: import org.netbeans.modules.xslt.tmap.model.xsltmap.XmlUtil;
029: import org.netbeans.modules.xslt.tmap.model.xsltmap.XsltMapConst;
030: import org.netbeans.modules.xslt.tmap.util.TMapUtil;
031: import org.netbeans.modules.xslt.tmap.util.Util;
032: import org.openide.filesystems.FileObject;
033: import org.openide.filesystems.FileUtil;
034: import org.openide.util.NbBundle;
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037:
038: /**
039: *
040: * @author Vitaly Bychkov
041: * @version 1.0
042: */
043: public class TransformmapValidatorImpl implements TransformmapValidator {
044:
045: private static TransformmapValidator INSTANCE = new TransformmapValidatorImpl();
046:
047: private ValidationRule[] myValidationRules;
048:
049: private ComplexValidationRule[] myComplexValidationRules;
050:
051: private TransformmapValidatorImpl() {
052: myValidationRules = new ValidationRule[] {
053: new NoTransformationDescriptor(), new IsXsltMap(),
054: new IsCorrectNs(), new IsOldTransformmap()
055:
056: };
057:
058: myComplexValidationRules = new ComplexValidationRule[] {
059: new CantResolveTransform(), new CantResolveInputType(),
060: new CantResolveOutputType() };
061: }
062:
063: public static TransformmapValidator getInstance() {
064: return INSTANCE;
065: }
066:
067: private String validate(Object valObject) {
068: String valResult = null;
069: for (TransformmapValidatorImpl.ValidationRule rule : myValidationRules) {
070: valResult = rule.validate(valObject);
071: if (valResult != null) {
072: break;
073: }
074: }
075: return valResult;
076: }
077:
078: private String validate(Object obj1, Object obj2) {
079: String valResult = null;
080: for (TransformmapValidatorImpl.ComplexValidationRule rule : myComplexValidationRules) {
081: valResult = rule.validate(obj1, obj2);
082: if (valResult != null) {
083: break;
084: }
085: }
086: return valResult;
087: }
088:
089: public String validate(File transformDescriptor) {
090: return validate((Object) transformDescriptor);
091: }
092:
093: public String validate(Project project) {
094: return validate((Object) project);
095: }
096:
097: public String validate(TMapModel model, FileObject xsltFo) {
098: return validate((Object) model, (Object) xsltFo);
099: }
100:
101: private interface ValidationRule {
102: boolean accept(Object valObject);
103:
104: String validate(Object valObject);
105: }
106:
107: private interface ComplexValidationRule {
108: boolean accept(Object obj1, Object obj2);
109:
110: String validate(Object obj1, Object obj2);
111: }
112:
113: private class NoTransformationDescriptor implements ValidationRule {
114: private String validate(Project project) {
115: if (project == null) {
116: return null;
117: }
118:
119: File tDescriptor = Util
120: .getTransformationDescriptor(project);
121: if (tDescriptor == null) {
122: return NbBundle.getMessage(
123: TransformmapValidatorImpl.class,
124: "Msg_NoTransformationDescriptor");
125: }
126:
127: return null;
128: }
129:
130: public boolean accept(Object valObject) {
131: return valObject instanceof Project;
132: }
133:
134: public String validate(Object valObject) {
135: if (!accept(valObject)) {
136: return null;
137: }
138: return validate((Project) valObject);
139: }
140: }
141:
142: private class IsXsltMap implements ValidationRule {
143: private String validate(File transformDescriptor) {
144: if (transformDescriptor == null) {
145: return null;
146: }
147:
148: String xsltMap = XsltMapConst.XSLTMAP + "."
149: + XsltMapConst.XML;
150: if (xsltMap.equals(transformDescriptor.getName())) {
151: return NbBundle.getMessage(
152: TransformmapValidatorImpl.class,
153: "Msg_OldXsltmap");
154: }
155: return null;
156: }
157:
158: private String validate(Project project) {
159: if (project == null) {
160: return null;
161: }
162: return validate(Util.getTransformationDescriptor(project));
163: }
164:
165: public boolean accept(Object valObject) {
166: return valObject instanceof File
167: || valObject instanceof Project;
168: }
169:
170: public String validate(Object valObject) {
171: if (!accept(valObject)) {
172: return null;
173: }
174: return valObject instanceof File ? validate((File) valObject)
175: : validate((Project) valObject);
176: }
177: }
178:
179: private class IsCorrectNs implements ValidationRule {
180: private String validate(File transformDescriptor) {
181: if (transformDescriptor == null) {
182: return null;
183: }
184:
185: Document descriptorDocument = XmlUtil.getDocument(
186: transformDescriptor, true);
187: if (descriptorDocument == null) {
188: return NbBundle.getMessage(
189: TransformmapValidatorImpl.class,
190: "Msg_CantGetDescriptorDocument");
191: }
192:
193: Element rootElement = descriptorDocument
194: .getDocumentElement();
195: if (rootElement == null) {
196: return NbBundle.getMessage(
197: TransformmapValidatorImpl.class,
198: "Msg_CantGetRootElement");
199: }
200:
201: String ns = rootElement.getNamespaceURI();
202: ns = ns == null ? "" : ns;
203: if (!TMapComponent.TRANSFORM_MAP_NS_URI.equals(ns)) {
204: return NbBundle.getMessage(
205: TransformmapValidatorImpl.class,
206: "Msg_IncorrectNamespace", ns);
207: }
208: return null;
209: }
210:
211: public boolean accept(Object valObject) {
212: return valObject instanceof File;
213: }
214:
215: public String validate(Object valObject) {
216: if (!accept(valObject)) {
217: return null;
218: }
219: return validate((File) valObject);
220: }
221: }
222:
223: private class IsOldTransformmap implements ValidationRule {
224: private String validate(File transformDescriptor) {
225: // TODO a
226: return null;
227: }
228:
229: private String validate(Project project) {
230: return validate(Util.getTransformationDescriptor(project));
231: }
232:
233: public boolean accept(Object valObject) {
234: return valObject instanceof File
235: || valObject instanceof Project;
236: }
237:
238: public String validate(Object valObject) {
239: if (!accept(valObject)) {
240: return null;
241: }
242: return valObject instanceof File ? validate((File) valObject)
243: : validate((Project) valObject);
244: }
245: }
246:
247: private class CantResolveTransform implements ComplexValidationRule {
248: private String validate(TMapModel model, FileObject xsltFo) {
249: if (model == null || xsltFo == null) {
250: return null;
251: }
252: if (TMapUtil.getTransform(model, xsltFo) == null) {
253: return NbBundle.getMessage(
254: TransformmapValidatorImpl.class,
255: "Msg_CantResolveTransformation", xsltFo
256: .getNameExt()); // NOI18N
257: }
258: return null;
259: }
260:
261: public boolean accept(Object obj1, Object obj2) {
262: return obj1 instanceof TMapModel
263: && obj2 instanceof FileObject;
264: }
265:
266: public String validate(Object obj1, Object obj2) {
267: if (!accept(obj1, obj2)) {
268: return null;
269: }
270: return validate((TMapModel) obj1, (FileObject) obj2);
271: }
272: }
273:
274: private class CantResolveInputType implements ComplexValidationRule {
275: private String validate(TMapModel model, FileObject xsltFo) {
276: if (model == null || xsltFo == null) {
277: return null;
278: }
279:
280: Transform transform = TMapUtil.getTransform(model, xsltFo);
281: if (transform == null) {
282: return null;
283: }
284:
285: if (TMapUtil.getSourceComponent(transform) == null) {
286: return NbBundle.getMessage(
287: TransformmapValidatorImpl.class,
288: "Msg_CantResolveSourceType", xsltFo
289: .getNameExt()); // NOI18N
290: }
291: return null;
292: }
293:
294: public boolean accept(Object obj1, Object obj2) {
295: return obj1 instanceof TMapModel
296: && obj2 instanceof FileObject;
297: }
298:
299: public String validate(Object obj1, Object obj2) {
300: if (!accept(obj1, obj2)) {
301: return null;
302: }
303: return validate((TMapModel) obj1, (FileObject) obj2);
304: }
305: }
306:
307: private class CantResolveOutputType implements
308: ComplexValidationRule {
309: private String validate(TMapModel model, FileObject xsltFo) {
310: if (model == null || xsltFo == null) {
311: return null;
312: }
313:
314: Transform transform = TMapUtil.getTransform(model, xsltFo);
315: if (transform == null) {
316: return null;
317: }
318:
319: if (TMapUtil.getTargetComponent(transform) == null) {
320: return NbBundle.getMessage(
321: TransformmapValidatorImpl.class,
322: "Msg_CantResolveTargetType", xsltFo
323: .getNameExt()); // NOI18N
324: }
325: return null;
326: }
327:
328: public boolean accept(Object obj1, Object obj2) {
329: return obj1 instanceof TMapModel
330: && obj2 instanceof FileObject;
331: }
332:
333: public String validate(Object obj1, Object obj2) {
334: if (!accept(obj1, obj2)) {
335: return null;
336: }
337: return validate((TMapModel) obj1, (FileObject) obj2);
338: }
339: }
340:
341: public String validate(AXIComponent axiComp, String typeParam) {
342: if (axiComp == null
343: || axiComp.getModel() == null
344: || axiComp.getModel().getState() != XslModel.State.VALID) {
345: return NbBundle.getMessage(TransformmapValidatorImpl.class,
346: "MSG_Error_BadSchema", typeParam); // NOI18N
347: }
348: return null;
349: }
350:
351: }
|