configcontext.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,406 行 · 第 1/3 页

JAVA
1,406
字号
    else if (node instanceof Element) {      NamedNodeMap attrList = node.getAttributes();      if (attrList != null) {        for (int i = 0; i < attrList.getLength(); i++) {          if (! attrList.item(i).getNodeName().startsWith("xml"))            return true;        }      }    }    for (ptr = node.getFirstChild(); ptr != null; ptr = ptr.getNextSibling()) {      if (ptr instanceof Element)	return true;    }    return false;  }  static String getValue(QName name, Node node, String defaultValue)  {    /*    NamedNodeMap attrList = node.getAttributes();    if (attrList != null) {      for (int i = 0; i < attrList.getLength(); i++) {	if (attrList.item(i).getNodeName().equals(name.getName()))	  return attrList.item(i).getNodeValue();      }    }    */    if (node instanceof Element) {      String value = ((Element) node).getAttribute(name.getName());      if (! "".equals(value))        return value;    }    Node ptr;    for (ptr = node.getFirstChild(); ptr != null; ptr = ptr.getNextSibling()) {      QName qName = ((QAbstractNode) ptr).getQName();      if (name.equals(qName))	return textValue(ptr);    }    return defaultValue;  }  /**   * Returns the text value of the node.   */  Object getELValue(Attribute attr, Node node)  {    if (node instanceof Attr) {      Attr attrNode = (Attr) node;      String data = attrNode.getNodeValue();      // server/12h6      if (data != null && isEL() && attr.isEL()	  && (data.indexOf("#{") >= 0 || data.indexOf("${") >= 0)) {	return eval(attr.getConfigType(), data);      }      return null;    }    if (! (node instanceof Element))      return null;    Element elt = (Element) node;    Element childElt = null;    for (Node child = elt.getFirstChild();	 child != null;	 child = child.getNextSibling()) {      if (child instanceof Element) {	if (childElt != null)	  return null;		childElt = (Element) child;      }            else if (child instanceof CharacterData	       && ! XmlUtil.isWhitespace(((CharacterData) child).getData())) {	String data = ((CharacterData) child).getData();	if (isEL() && attr.isEL() && childElt == null	    && child.getNextSibling() == null	    && (data.indexOf("#{") >= 0 || data.indexOf("${") >= 0)) {	  String exprString = data.trim();	  ELContext elContext = getELContext();	  ELParser parser = new ELParser(elContext, exprString);	  Expr expr = parser.parse();	  Object value = expr.getValue(elContext);	  // ioc/2403	  return attr.getConfigType().valueOf(value);	}		return null;      }    }    return null;  }  /**   * Returns the text value of the node.   */  String getTextValue(Node node)  {    if (node instanceof Attr) {      Attr attrNode = (Attr) node;      String data = attrNode.getNodeValue();      return data;    }    if (! (node instanceof Element))      return null;    Element elt = (Element) node;    // ioc/2235    if (elt.getAttributes().getLength() > 0)      return null;    for (Node child = elt.getFirstChild();	 child != null;	 child = child.getNextSibling()) {      if (child instanceof Element) {	return null;      }            else if (child instanceof CharacterData) {	String data = ((CharacterData) child).getData();	if (child.getNextSibling() == null && ! XmlUtil.isWhitespace(data)) {	  return data;	}	else	  return null;      }    }    return null;  }  private Object eval(ConfigType type, String data)  {    ELContext elContext = getELContext();        ELParser parser = new ELParser(elContext, data);        Expr expr = parser.parse();    Object value = type.valueOf(elContext, expr);    if (value != null)      return value;    else      return NULL;  }  /**   * Returns the text value of the node.   */  Object getElementValue(Attribute attr, Node node)  {    if (! (node instanceof Element))      return null;    Element elt = (Element) node;    Element childElt = null;    for (Node child = elt.getFirstChild();	 child != null;	 child = child.getNextSibling()) {      if (child instanceof Element) {	if (childElt != null)	  return null;		childElt = (Element) child;      }            else if (child instanceof CharacterData	       && ! XmlUtil.isWhitespace(((CharacterData) child).getData())) {	String data = ((CharacterData) child).getData();	if (isEL() && attr.isEL() && childElt == null	    && child.getNextSibling() == null	    && (data.indexOf("#{") >= 0 || data.indexOf("${") >= 0)) {	  ELContext elContext = getELContext();	  ELParser parser = new ELParser(elContext, data.trim());    	  Expr expr = parser.parse();	  Object value = attr.getConfigType().valueOf(elContext, expr);	  if (value != null)	    return value;	  else	    return NULL;	}		return null;      }    }    if (childElt == null)      return null;    TypeFactory factory = TypeFactory.getFactory();    ConfigType childType      = factory.getEnvironmentType(((QElement) childElt).getQName());    if (childType != null) {      Object childBean = childType.create(null);            configureNode(childElt, childBean, childType);            childType.init(childBean);      Object value = childType.replaceObject(childBean);      if (value != null)	return value;      else	return NULL;    }    return null;  }  /**   * Returns the text value of the node.   */  static String textValue(Node node)  {    if (node instanceof Attr)      return node.getNodeValue();    else {      String value = XmlUtil.textValue(node);      if (value == null || value.equals(""))	return "";      else if (node instanceof Element) {	String space = ((Element) node).getAttribute("xml:space");	if (! space.equals(""))	  return value;      }      return value.trim();    }  }  /**   * Returns the text value of the node.   */  static String textValueNoTrim(Node node)  {    if (node instanceof Attr)      return node.getNodeValue();    else {      String value = XmlUtil.textValue(node);      if (value == null)	return "";      return value;    }  }  /**   * Evaluate as an object   */  public Object evalObject(String exprString)    throws ELException  {    if (exprString.indexOf("${") >= 0 && isEL()) {      ELParser parser = new ELParser(getELContext(), exprString);      parser.setCheckEscape(true);      Expr expr = parser.parse();      return expr.getValue(getELContext());    }    else      return exprString;  }  public static RuntimeException error(String msg, Node node)  {    String systemId = null;    String filename = null;    int line = 0;    if (node instanceof QAbstractNode) {      QAbstractNode qnode = (QAbstractNode) node;            systemId = qnode.getBaseURI();      filename = qnode.getFilename();      line = qnode.getLine();    }    if (systemId != null) {      String sourceLines = getSourceLines(systemId, line);            msg = msg + sourceLines;    }          if (filename != null)      return new LineConfigException(filename, line, msg);    else      return new LineConfigException(msg);  }    public static RuntimeException error(Throwable e, Node node)  {    String systemId = null;    String filename = null;    int line = 0;    if (e instanceof RuntimeException	&& e instanceof DisplayableException	&& ! ConfigException.class.equals(e.getClass())) {      return (RuntimeException) e;    }    if (node instanceof QAbstractNode) {      QAbstractNode qnode = (QAbstractNode) node;            systemId = qnode.getBaseURI();      filename = qnode.getFilename();      line = qnode.getLine();    }    for (; e.getCause() != null; e = e.getCause()) {      if (e instanceof LineCompileException)        break;      else if (e instanceof LineConfigException)        break;      else if (e instanceof CompileException)        break;    }    if (e instanceof LineConfigException)      return (LineConfigException) e;    else if (e instanceof LineCompileException) {      return new LineConfigException(e.getMessage(), e);    }    else if (e instanceof ConfigException	     && e.getMessage() != null	     && filename != null) {      String sourceLines = getSourceLines(systemId, line);      return new LineConfigException(filename, line,				     e.getMessage() + sourceLines,				     e);    }    else if (e instanceof CompileException && e.getMessage() != null) {      return new LineConfigException(filename, line, e);    }    else {      String sourceLines = getSourceLines(systemId, line);            String msg = filename + ":" + line + ": " + e + sourceLines;      if (e instanceof RuntimeException) {	throw new LineConfigException(msg, e);      }      else if (e instanceof Error) {	// server/1711	throw new LineConfigException(msg, e);	// throw (Error) e;      }      else	return new LineConfigException(msg, e);    }  }  private static String getSourceLines(String systemId, int errorLine)  {    if (systemId == null)      return "";        ReadStream is = null;    try {      is = Vfs.lookup().lookup(systemId).openRead();      int line = 0;      StringBuilder sb = new StringBuilder("\n\n");      String text;      while ((text = is.readLine()) != null) {	line++;	if (errorLine - 2 <= line && line <= errorLine + 2) {	  sb.append(line);	  sb.append(": ");	  sb.append(text);	  sb.append("\n");	}      }      return sb.toString();    } catch (IOException e) {      log.log(Level.FINEST, e.toString(), e);      return "";    } finally {      if (is != null)	is.close();    }  }  public String toString()  {    return getClass().getSimpleName() + "[" + _dependentScope + "]";  }  static class ValidatorEntry {    private Validator _validator;    private ClassLoader _loader;    ValidatorEntry(Validator validator)    {      _validator = validator;      _loader = Thread.currentThread().getContextClassLoader();    }    void validate()      throws ConfigException    {      Thread thread = Thread.currentThread();      ClassLoader oldLoader = thread.getContextClassLoader();      try {	thread.setContextClassLoader(_loader);	_validator.validate();      } finally {	thread.setContextClassLoader(oldLoader);      }    }  }  static {    _resinClassSet.add(RESIN_CLASS_NS);    _resinClassSet.add(RESIN_CLASS);    _resinClassSet.add(RESIN_TYPE_NS);    _resinClassSet.add(RESIN_TYPE);    _resinClassSet.add(RESIN_PARAM_NS);    _resinClassSet.add(RESIN_PARAM);    /*    _resinClassSet.add(new QName("resin:type", "http://caucho.com/ns/resin"));    _resinClassSet.add(new QName("resin:class", "http://caucho.com/ns/resin"));    */  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?