jaxp/src/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Import.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 6 7f561c08de6b
child 2669 15024792697e
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: Import.java,v 1.2.4.1 2005/09/12 10:32:33 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.net.MalformedURLException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.xml.sax.InputSource;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import org.xml.sax.XMLReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * @author Erwin Bolwidt <ejb@klomp.org>
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * @author Gunnlaugur Briem <gthb@dimon.is>
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
final class Import extends TopLevelElement {
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    private Stylesheet _imported = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    public Stylesheet getImportedStylesheet() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
        return _imported;
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    public void parseContents(final Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        final XSLTC xsltc = parser.getXSLTC();
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        final Stylesheet context = parser.getCurrentStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
            String docToLoad = getAttribute("href");
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
            if (context.checkForLoop(docToLoad)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
                final ErrorMsg msg = new ErrorMsg(ErrorMsg.CIRCULAR_INCLUDE_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
                                                  docToLoad, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
                parser.reportError(Constants.FATAL, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
            InputSource input = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            XMLReader reader = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
            String currLoadedDoc = context.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
            SourceLoader loader = context.getSourceLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            // Use SourceLoader if available
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            if (loader != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
                input = loader.loadSource(docToLoad, currLoadedDoc, xsltc);
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
                if (input != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
                    docToLoad = input.getSystemId();
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
                    reader = xsltc.getXMLReader();
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
                } else if (parser.errorsFound()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
                    return;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
            // No SourceLoader or not resolved by SourceLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
            if (input == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
                docToLoad = SystemIDResolver.getAbsoluteURI(docToLoad, currLoadedDoc);
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                input = new InputSource(docToLoad);
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            // Return if we could not resolve the URL
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            if (input == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                final ErrorMsg msg =
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                    new ErrorMsg(ErrorMsg.FILE_NOT_FOUND_ERR, docToLoad, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                parser.reportError(Constants.FATAL, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            final SyntaxTreeNode root;
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
            if (reader != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
                root = parser.parse(reader,input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                root = parser.parse(input);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
            if (root == null) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            _imported = parser.makeStylesheet(root);
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
            if (_imported == null) return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
            _imported.setSourceLoader(loader);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
            _imported.setSystemId(docToLoad);
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
            _imported.setParentStylesheet(context);
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            _imported.setImportingStylesheet(context);
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        _imported.setTemplateInlining(context.getTemplateInlining());
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
            // precedence for the including stylesheet
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
            final int currPrecedence = parser.getCurrentImportPrecedence();
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            final int nextPrecedence = parser.getNextImportPrecedence();
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            _imported.setImportPrecedence(currPrecedence);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
            context.setImportPrecedence(nextPrecedence);
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
            parser.setCurrentStylesheet(_imported);
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            _imported.parseContents(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
            final Enumeration elements = _imported.elements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            final Stylesheet topStylesheet = parser.getTopLevelStylesheet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
            while (elements.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
                final Object element = elements.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                if (element instanceof TopLevelElement) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                    if (element instanceof Variable) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                        topStylesheet.addVariable((Variable) element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                    else if (element instanceof Param) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
                        topStylesheet.addParam((Param) element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
                    else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
                        topStylesheet.addElement((TopLevelElement) element);
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        catch (Exception e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        finally {
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            parser.setCurrentStylesheet(context);
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        return Type.Void;
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        // do nothing
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
}