001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xsl.grammar;
042:
043: import java.beans.FeatureDescriptor;
044: import java.util.Enumeration;
045: import org.netbeans.modules.xml.api.model.GrammarEnvironment;
046: import org.netbeans.modules.xml.api.model.GrammarQuery;
047: import org.netbeans.modules.xml.api.model.GrammarQueryManager;
048: import org.openide.filesystems.FileObject;
049: import org.w3c.dom.DocumentType;
050: import org.w3c.dom.Element;
051: import org.w3c.dom.Node;
052: import org.xml.sax.InputSource;
053:
054: import org.openide.filesystems.FileObject;
055:
056: import org.netbeans.modules.xsl.XSLDataObject;
057: import org.openide.loaders.DataObject;
058: import org.openide.loaders.DataObjectNotFoundException;
059:
060: /**
061: * Provide DTD grammar. It must be registered at layer.
062: *
063: * @author Petr Kuzel
064: */
065: public class XSLGrammarQueryProvider extends GrammarQueryManager {
066:
067: static final String PUBLIC = "!!! find it out"; // NOI18N
068: static final String SYSTEM = "!!! find it out"; // NOI18N
069: static final String NAMESPACE = XSLGrammarQuery.XSLT_NAMESPACE_URI;
070:
071: private String prefix = null;
072:
073: public Enumeration enabled(GrammarEnvironment ctx) {
074:
075: if (ctx.getFileObject() == null)
076: return null;
077:
078: prefix = null;
079: Enumeration en = ctx.getDocumentChildren();
080: while (en.hasMoreElements()) {
081: Node next = (Node) en.nextElement();
082: if (next.getNodeType() == next.DOCUMENT_TYPE_NODE) {
083: // DocumentType doctype = (DocumentType) next;
084: // if (PUBLIC.equals(doctype.getPublicId()) || SYSTEM.equals(doctype.getSystemId())) {
085: // return new SingletonEnumeration(next);
086: // }
087: } else if (next.getNodeType() == next.ELEMENT_NODE) {
088: Element element = (Element) next;
089: String tag = element.getTagName();
090: if (tag.indexOf(":") == -1) { // NOI18N
091: if ("transformation".equals(tag)
092: || "stylesheet".equals(tag)) { // NOI18N
093: String ns = element.getAttribute("xmlns"); // NOI18N
094: if (NAMESPACE.equals(ns)) {
095: return org.openide.util.Enumerations
096: .singleton(next);
097: }
098: }
099: } else {
100: prefix = tag.substring(0, tag.indexOf(":")); // NOI18N
101: String local = tag.substring(tag.indexOf(":") + 1); // NOI18N
102: if ("transformation".equals(local)
103: || "stylesheet".equals(local)) { // NOI18N
104: String ns = element.getAttribute("xmlns:"
105: + prefix); // NOI18N
106: if (NAMESPACE.equals(ns)) {
107: return org.openide.util.Enumerations
108: .singleton(next);
109: }
110: }
111: }
112: }
113: }
114:
115: // try mime type
116: FileObject fo = ctx.getFileObject();
117: if (fo != null) {
118: if (XSLDataObject.MIME_TYPE.equals(fo.getMIMEType())) {
119: // almost forever, until client uses its own invalidation
120: // rules based e.g. on new node detection at root level
121: // or MIME type listening
122: return org.openide.util.Enumerations.empty();
123: }
124: }
125:
126: return null;
127: }
128:
129: public FeatureDescriptor getDescriptor() {
130: return new FeatureDescriptor();
131: }
132:
133: public GrammarQuery getGrammar(GrammarEnvironment input) {
134: try {
135: FileObject fo = input.getFileObject();
136: if (fo == null)
137: throw new IllegalStateException(
138: "GrammarEnvironment has changed between enabled() and getGrammar()!"); // NOI18N // NOI18N
139: DataObject dataObj = DataObject.find(fo);
140: return new XSLGrammarQuery(dataObj);
141:
142: } catch (DataObjectNotFoundException e) {
143: throw new IllegalStateException("Missing DataObject "
144: + e.getFileObject() + "!"); // NOI18N
145: }
146: }
147:
148: }
|