bodong_chen  
Java Space
日历
<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678
统计
  • 随笔 - 11
  • 文章 - 3
  • 评论 - 2
  • 引用 - 0

导航

常用链接

留言簿(2)

随笔分类

随笔档案

文章档案

Log4j

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

2006年3月15日

以前经常会在List的index处理上犯微小的错误,或大一,或小一,但结果都是一样:wrong!所以在这里简要总结一下最容易出错的两个方法。
1. List的indexOf(Object obj)方法:
这个方法返回的是List中一个节点的序号。例如:第一个节点返回0,最后一个节点返回list.size()-1。
2. List的subList(int begin, int end)方法:
这个方法是取出一个List的一个子List,在两个参数上很容易犯错误。
以一个size为5的List为例:
subList(0, 2);取出的是第一个到第二个节点;
subList(0, 1);取出第一个节点;
subList(1, 1);不取出任何节点;
subList(2, 1);则会出参数错误的异常。
可以看出,begin为起始节点的序号,end为所要取的最后一个节点的序号+1。

jdk中的这些类有很多细节都需要非常注意,不然实际的coding中会遇到很多麻烦。
posted @ 2006-04-10 16:37 bodong_chen 阅读(1361) | 评论 (0)编辑 收藏
 
在一个有关Jdom的讨论中提到:

Elliotte Rusty Harold: XML documents must be well-formed. There are, depending on how you count, anywhere from a hundred to several thousand different rules. These "well-formedness" rules are the minimum requirements for an XML document. The rules cover things like what characters are allowed in element names: The letter 'a' is OK. The letter omega is OK. The asterisk character is not OK. White space is not OK. The rules say that every start-tag has to have a matching end-tag. Elements can nest, but they cannot overlap. Processing instructions have the form <, ?, a target, white space, the data, ?, and a >. Comments cannot contain a double hyphen. There are many such rules governing well-formedness of XML documents.

Validity talks about which elements and attributes are allowed where. Well-formedness only talks about the structure of any XML document, irrespective of what the names are. Validity says, we're only going to allow these elements with these names in these positions. Validity is not required. Well-formedness is.

JDOM, and for that matter DOM, allows you to create malformed documents. They do not check everything they can possibly check. For instance, they do not currently check that the text content of a text node does not contain the null character, which is completely illegal in an XML document.

这是发生在2003年的讨论:http://www.artima.com/intv/jdom2.html。

现在,使用JDOM可以完成这两件事:
1. 验证XML文件的Wellformedness:
The build() method of SAXBuilder throws an IOException if an I/O error such as a broken socket prevents the document from being completely read. It throws a JDOMException if the document is malformed. This is the generic superclass for most things that can go wrong while working with JDOM other than I/O errors.
Example:
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMChecker {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMChecker URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder();
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness errors, 
      
// then no exception is thrown
      System.out.println(args[0+ " is well-formed.");
    }
    
// indicates a well-formedness error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not well-formed.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    } 
  }
}
2. 验证XML文件的Validity:
SAXBuilder only checks documents for well-formedness, not validity. If you want to validate as well, then pass the boolean true to the SAXBuilder() constructor. Then any validity errors will also cause JDOMExceptions.
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;


public class JDOMValidator {

  
public static void main(String[] args) {
  
    
if (args.length == 0) {
      System.out.println(
"Usage: java JDOMValidator URL"); 
      
return;
    } 
      
    SAXBuilder builder 
= new SAXBuilder(true);
                                    
//  ^^^^
                                    
// Turn on validation
     
    
// command line should offer URIs or file names
    try {
      builder.build(args[
0]);
      
// If there are no well-formedness or validity errors, 
      
// then no exception is thrown.
      System.out.println(args[0+ " is valid.");
    }
    
// indicates a well-formedness or validity error
    catch (JDOMException e) { 
      System.out.println(args[
0+ " is not valid.");
      System.out.println(e.getMessage());
    }  
    
catch (IOException e) { 
      System.out.println(
"Could not check " + args[0]);
      System.out.println(
" because " + e.getMessage());
    }  
  }
}

参考文献:http://www.ibiblio.org/xml/books/xmljava/chapters/ch14s07.html
posted @ 2006-04-03 17:41 bodong_chen 阅读(331) | 评论 (0)编辑 收藏
 
MSSQLServer2000

<data-sources>
                <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
                        <set-property property="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
                        <set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;databaseName=ClassManageSys" />
                        <set-property property="username" value="sa" />
                        <set-property property="password" value="" />
                        <set-property property="maxActive" value="10" />
                        <set-property property="maxWait" value="5000" />
                        <set-property property="defaultAutoCommit" value="false" />
                        <set-property property="defaultReadOnly" value="false" />
                </data-source>
</data-sources>

MySQL

<struts-config>
        <data-sources>
                <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
                        <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
                        <set-property property="url" value="jdbc:mysql://localhost/test" />
                        <set-property property="username" value="root" />
                        <set-property property="password" value="" />
                        <set-property property="maxActive" value="10" />
                        <set-property property="maxWait" value="5000" />
                        <set-property property="defaultAutoCommit" value="false" />
                        <set-property property="defaultReadOnly" value="false" />
        </data-source>
</data-sources>

posted @ 2006-03-17 15:42 bodong_chen 阅读(968) | 评论 (1)编辑 收藏
 
花了一天时间学习Log4j,现在差不多已经达到了熟练运用的程度了。
关于Log4j的文章很多,而且已经写得很好了,我在这里写也只能是copy & paste那些大牛们的作品。在此推荐我觉得讲得最清楚的一篇文章:
https://www.qos.ch/ac2001/F11-10.html
希望能对Log4j的学习者有所帮助。我看了几篇中文的文章,其实都是对这篇文章进行的翻译,其中还有一些不当的地方,建议还是学习英文原文为上。
此外,Log4j的官方主页http://logging.apache.org/log4j/docs/index.html上可以下载到相关的资源。

最后谈一下个人心得。对于一个web项目来说,可以构建一个Debugger类,专门服务于其他的类和JSP,这样就免去了在每一需要进行Debuging的地方都定义一个Logger实例,使用起来很方便。下面提供参考代码:
 1 /*
 2  * Created on 2006-3-14
 3  *
 4  * TODO To change the template for this generated file go to
 5  * Window - Preferences - Java - Code Style - Code Templates
 6  */
 7 package util;
 8 
 9 import org.apache.log4j.Logger;
10 
11 /**
12  * @author dirkchen
13  *
14  * TODO To change the template for this generated type comment go to
15  * Window - Preferences - Java - Code Style - Code Templates
16  */
17 public class Debugger 
18 {
19     protected static Logger logger = Logger.getLogger("ChildrenProject");
20     
21     static
22     {
23         logger.setAdditivity( false );
24     }
25     
26     public static void error( Throwable e ) 
27     {
28         logger.error("", e);
29     }
30     
31     public static void debug( Object obj )
32     {
33         logger.debug( obj );
34     }
35 }
在其他地方只需要编写如下代码就可以进行debuging或者其他操作:
util.Debugger.debug(...);
当然,这时针对debug级的log信息。
posted @ 2006-03-15 15:52 bodong_chen 阅读(303) | 评论 (0)编辑 收藏
 
Copyright © bodong_chen Powered by: 博客园 模板提供:沪江博客