KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > grammar > XSLGrammarQueryProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xsl.grammar;
20
21 import java.beans.FeatureDescriptor JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import org.netbeans.modules.xml.api.model.GrammarEnvironment;
24 import org.netbeans.modules.xml.api.model.GrammarQuery;
25 import org.netbeans.modules.xml.api.model.GrammarQueryManager;
26 import org.openide.filesystems.FileObject;
27 import org.w3c.dom.DocumentType JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31
32 import org.openide.filesystems.FileObject;
33
34 import org.netbeans.modules.xsl.XSLDataObject;
35 import org.openide.loaders.DataObject;
36 import org.openide.loaders.DataObjectNotFoundException;
37
38 /**
39  * Provide DTD grammar. It must be registered at layer.
40  *
41  * @author Petr Kuzel
42  */

43 public class XSLGrammarQueryProvider extends GrammarQueryManager {
44     
45     static final String JavaDoc PUBLIC = "!!! find it out"; // NOI18N
46
static final String JavaDoc SYSTEM = "!!! find it out"; // NOI18N
47
static final String JavaDoc NAMESPACE = XSLGrammarQuery.XSLT_NAMESPACE_URI;
48     
49     private String JavaDoc prefix = null;
50     
51     public Enumeration JavaDoc enabled(GrammarEnvironment ctx) {
52
53         if (ctx.getFileObject() == null) return null;
54         
55         prefix = null;
56         Enumeration JavaDoc en = ctx.getDocumentChildren();
57         while (en.hasMoreElements()) {
58             Node JavaDoc next = (Node JavaDoc) en.nextElement();
59             if (next.getNodeType() == next.DOCUMENT_TYPE_NODE) {
60 // DocumentType doctype = (DocumentType) next;
61
// if (PUBLIC.equals(doctype.getPublicId()) || SYSTEM.equals(doctype.getSystemId())) {
62
// return new SingletonEnumeration(next);
63
// }
64
} else if (next.getNodeType() == next.ELEMENT_NODE) {
65                 Element JavaDoc element = (Element JavaDoc) next;
66                 String JavaDoc tag = element.getTagName();
67                 if (tag.indexOf(":") == -1) { // NOI18N
68
if ("transformation".equals(tag) || "stylesheet".equals(tag)) { // NOI18N
69
String JavaDoc ns = element.getAttribute("xmlns"); // NOI18N
70
if (NAMESPACE.equals(ns)) {
71                             return org.openide.util.Enumerations.singleton (next);
72                         }
73                     }
74                 } else {
75                     prefix = tag.substring(0, tag.indexOf(":")); // NOI18N
76
String JavaDoc local = tag.substring(tag.indexOf(":") + 1); // NOI18N
77
if ("transformation".equals(local) || "stylesheet".equals(local)) { // NOI18N
78
String JavaDoc ns = element.getAttribute("xmlns:" + prefix); // NOI18N
79
if (NAMESPACE.equals(ns)) {
80                             return org.openide.util.Enumerations.singleton (next);
81                         }
82                     }
83                 }
84             }
85         }
86         
87         // try mime type
88
FileObject fo = ctx.getFileObject();
89         if (fo != null) {
90             if (XSLDataObject.MIME_TYPE.equals(fo.getMIMEType())) {
91                 // almost forever, until client uses its own invalidation
92
// rules based e.g. on new node detection at root level
93
// or MIME type listening
94
return org.openide.util.Enumerations.empty();
95             }
96         }
97         
98         return null;
99     }
100     
101     public FeatureDescriptor JavaDoc getDescriptor() {
102         return new FeatureDescriptor JavaDoc();
103     }
104     
105     public GrammarQuery getGrammar(GrammarEnvironment input) {
106         try {
107             FileObject fo = input.getFileObject();
108             if (fo == null) throw new IllegalStateException JavaDoc("GrammarEnvironment has changed between enabled() and getGrammar()!"); // NOI18N // NOI18N
109
DataObject dataObj = DataObject.find(fo);
110             return new XSLGrammarQuery(dataObj);
111             
112         } catch (DataObjectNotFoundException e) {
113             throw new IllegalStateException JavaDoc("Missing DataObject " + e.getFileObject() + "!"); // NOI18N
114
}
115     }
116     
117 }
118
Popular Tags