jspparser.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,239 行 · 第 1/4 页
JAVA
2,239 行
} addText(); setLocation(); _jspBuilder.endElement(name); return read(); } private void processTaglibDirective(ArrayList<QName> keys, ArrayList<String> values) throws IOException, JspParseException { int p = keys.indexOf(PREFIX); if (p < 0) throw error(L.l("The taglib directive requires a 'prefix' attribute. 'prefix' is the XML prefix for all tags in the taglib.")); String prefix = values.get(p); if (_prefixes.contains(prefix) && _parseState.getQName(prefix) == null) { throw error(L.l("The taglib prefix '{0}' must be defined before it is used.", prefix)); } String uri = null; p = keys.indexOf(URI); if (p >= 0) uri = values.get(p); String tagdir = null; p = keys.indexOf(TAGDIR); if (p >= 0) tagdir = values.get(p); if (uri != null) processTaglib(prefix, uri); else if (tagdir != null) processTaglibDir(prefix, tagdir); } /** * Adds a new known taglib prefix to the current namespace. */ private void processTaglib(String prefix, String uri) throws JspParseException { Taglib taglib = null; int colon = uri.indexOf(':'); int slash = uri.indexOf('/'); String location = null; if (colon > 0 && colon < slash) location = uri; else if (slash == 0) location = uri; else location = _uriPwd + uri; try { taglib = _tagManager.addTaglib(prefix, uri, location); String tldURI = "urn:jsptld:" + uri; _parseState.pushNamespace(prefix, tldURI); _namespaces = new Namespace(_namespaces, prefix, tldURI); return; } catch (JspParseException e) { throw error(e); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } if (colon > 0 && colon < slash) throw error(L.l("Unknown taglib '{0}'. Taglibs specified with an absolute URI must either be:\n1) specified in the web.xml\n2) defined in a jar's .tld in META-INF\n3) defined in a .tld in WEB-INF\n4) predefined by Resin", uri)); } /** * Adds a new known tag dir to the current namespace. */ private void processTaglibDir(String prefix, String tagDir) throws JspParseException { Taglib taglib = null; try { taglib = _tagManager.addTaglibDir(prefix, tagDir); String tagURI = "urn:jsptagdir:" + tagDir; _parseState.pushNamespace(prefix, tagURI); _namespaces = new Namespace(_namespaces, prefix, tagURI); return; } catch (JspParseException e) { throw error(e); } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } private void processIncludeDirective(ArrayList keys, ArrayList values) throws IOException, JspParseException { int p = keys.indexOf("file"); if (p < 0) throw error(L.l("The include directive requires a 'file' attribute.")); String file = (String) values.get(p); pushInclude(file); } public void pushInclude(String value) throws IOException, JspParseException { pushInclude(value, false); } public void pushInclude(String value, boolean allowDuplicate) throws IOException, JspParseException { if (value.equals("")) throw error("include directive needs 'file' attribute. Use either <%@ include file='myfile.jsp' %> or <jsp:directive.include file='myfile.jsp'/>"); Path include; if (value.length() > 0 && value.charAt(0) == '/') include = _parseState.resolvePath(value); else include = _parseState.resolvePath(_uriPwd + value); String newUrl = _uriPwd; if (value.startsWith("/")) newUrl = value; else newUrl = _uriPwd + value; include.setUserPath(newUrl); String newUrlPwd; int p = newUrl.lastIndexOf('/'); newUrlPwd = newUrl.substring(0, p + 1); if (_jspPath != null && _jspPath.equals(include) && ! allowDuplicate) throw error(L.l("circular include of '{0}' forbidden. A JSP file may not include itself.", include)); for (int i = 0; i < _includes.size(); i++) { Include inc = _includes.get(i); if (inc._stream.getPath().equals(include) && ! allowDuplicate) throw error(L.l("circular include of '{0}'. A JSP file may not include itself.", include)); } try { addInclude(include.openRead(), newUrlPwd); } catch (IOException e) { log.log(Level.WARNING, e.toString(), e); if (include.exists()) throw error(L.l("can't open include of '{0}'. '{1}' exists but it's not readable.", value, include.getNativePath())); else throw error(L.l("can't open include of '{0}'. '{1}' does not exist.", value, include.getNativePath())); } } private void addInclude(ReadStream stream, String newUrlPwd) throws IOException, JspParseException { addText(); readLf(); Include inc = null; if (_stream != null) { inc = new Include(_stream, _line, _uriPwd); _includes.add(inc); } _parseState.addDepend(stream.getPath()); try { String encoding = _stream.getEncoding(); if (encoding != null) stream.setEncoding(encoding); } catch (Exception e) { } _stream = stream; _filename = stream.getUserPath(); _jspPath = stream.getPath(); _line = 1; _lineStart = _line; _uriPwd = newUrlPwd; _parseState.setUriPwd(_uriPwd); } /** * Skips whitespace characters. * * @param ch the current character * * @return the first non-whitespace character */ private int skipWhitespace(int ch) throws IOException, JspParseException { for (; XmlChar.isWhitespace(ch); ch = read()) { } return ch; } /** * Skips whitespace to end of line * * @param ch the current character * * @return the first non-whitespace character */ private int skipWhitespaceToEndOfLine(int ch) throws IOException, JspParseException { for (; XmlChar.isWhitespace(ch); ch = read()) { if (ch == '\n') return read(); else if (ch == '\r') { ch = read(); if (ch == '\n') return read(); else return ch; } } return ch; } private void addText(char ch) { _text.append(ch); } private void addText(String s) { _text.append(s); } private void addText() throws JspParseException { if (_text.length() > 0) createText(); _startText = _charCount; _lineStart = _line; } private void createText() throws JspParseException { String string = _text.toString(); setLocation(_jspPath, _filename, _lineStart); if (_parseState.isTrimWhitespace() && isWhitespace(string)) { } else _jspBuilder.text(string, _filename, _lineStart, _line); _lineStart = _line; _text.clear(); _startText = _charCount; } private boolean isWhitespace(String s) { int length = s.length(); for (int i = 0; i < length; i++) { if (! Character.isWhitespace(s.charAt(i))) return false; } return true; } /** * Checks to see if the element name represents a tag. */ private int getTag(String name) throws JspParseException { int p = name.indexOf(':'); if (p < 0) return TAG_UNKNOWN; String prefix = name.substring(0, p); String local = name.substring(p + 1); _prefixes.add(prefix); String url = Namespace.find(_namespaces, prefix); if (url != null) return TAG_JSP; else return TAG_UNKNOWN; /* QName qname; if (url != null) qname = new QName(prefix, local, url); else qname = new QName(prefix, local, null); TagInfo tag = _tagManager.getTag(qname); if (tag != null) return TAG_JSP; else return TAG_UNKNOWN; */ } private void unread(int ch) { _peek = ch; } /** * Reads the next character we're in the middle of cr/lf. */ private void readLf() throws IOException, JspParseException { if (_seenCr) { int ch = read(); if (ch != '\n') _peek = ch; } } /** * Reads the next character. */ private int read() throws IOException, JspParseException { int ch; if (_peek >= 0) { ch = _peek; _peek = -1; return ch; } try { if ((ch = _stream.readChar()) >= 0) { _charCount++; if (ch == '\r') { _line++; _charCount = 0; _seenCr = true; } else if (ch == '\n' && _seenCr) { _seenCr = false; _charCount = 0; } else if (ch == '\n') { _line++; _charCount = 0; } else { _seenCr = false; } return ch; } } catch (IOException e) { throw error(e.toString()); } _stream.close(); _seenCr = false; if (_includes.size() > 0) { setLocation(_jspPath, _filename, _line); Include include = _includes.get(_includes.size() - 1); _includes.remove(_includes.size() - 1); _stream = include._stream; _filename = _stream.getUserPath(); _jspPath = _stream.getPath(); _line = include._line; _lineStart = _line; _uriPwd = include._uriPwd; _parseState.setUriPwd(_uriPwd); setLocation(_jspPath, _filename, _line); return read(); } return -1; } void clear(Path appDir, String errorPage) { } /** * Creates an error message adding the filename and line. * * @param message the error message */ public JspParseException error(Exception e) { String message = e.getMessage(); if (e instanceof JspParseException) { log.log(Level.FINE, e.toString(), e); } if (e instanceof JspLineParseException) return (JspLineParseException) e; else if (e instanceof LineCompileException) return new JspLineParseException(e); if (_lineMap == null) return new JspLineParseException(_filename + ":" + _line + ": " + message, e); else { LineMap.Line line = _lineMap.getLine(_line); return new JspLineParseException(line.getSourceFilename() + ":" + line.getSourceLine(_line) + ": " + message, e); } } /** * Creates an error message adding the filename and line. * * @param message the error message */ public JspParseException error(String message) { JspGenerator gen = _jspBuilder.getGenerator(); if (_lineMap == null) { if (gen != null) return new JspLineParseException(_filename + ":" + _line + ": " + message + gen.getSourceLines(_jspPath, _line)); else return new JspLineParseException(_filename + ":" + _line + ": " + message); } else { LineMap.Line line = _lineMap.getLine(_line); return new JspLineParseException(line.getSourceFilename() + ":" + line.getSourceLine(_line) + ": " + message); } } /** * Creates an error message adding the filename and line. * * @param message the error message */ public JspParseException error(String message, Throwable e) { if (_lineMap == null) return new JspLineParseException(_filename + ":" + _line + ": " + message, e); else { LineMap.Line line = _lineMap.getLine(_line); return new JspLineParseException(line.getSourceFilename() + ":" + line.getSourceLine(_line) + ": " + message, e); } } /** * Sets the current location in the original file */ private void setLocation() { setLocation(_jspPath, _filename, _line); } /** * Sets the current location in the original file * * @param filename the filename * @param line the line in the source file */ private void setLocation(Path jspPath, String filename, int line) { if (_lineMap == null) { _jspBuilder.setLocation(jspPath, filename, line); } else { LineMap.Line srcLine = _lineMap.getLine(line); if (srcLine != null) { _jspBuilder.setLocation(jspPath, srcLine.getSourceFilename(), srcLine.getSourceLine(line)); } } } private String badChar(int ch) { if (ch < 0) return "end of file"; else if (ch == '\n' || ch == '\r') return "end of line"; else if (ch >= 0x20 && ch <= 0x7f) return "'" + (char) ch + "'"; else return "'" + (char) ch + "' (\\u" + hex(ch) + ")"; } private String hex(int value) { CharBuffer cb = new CharBuffer(); for (int b = 3; b >= 0; b--) { int v = (value >> (4 * b)) & 0xf; if (v < 10) cb.append((char) (v + '0')); else cb.append((char) (v - 10 + 'a')); } return cb.toString(); } class Include { ReadStream _stream; int _line; String _uriPwd; Include(ReadStream stream, int line, String uriPwd) { _stream = stream; _line = line; _uriPwd = uriPwd; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?