首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 XML 与 Java NIO 的较量 如何转移话题至缓冲区和通道(2)

使用 XML 与 Java NIO 的较量 如何转移话题至缓冲区和通道(2)

工作原理我已经做了许多无用功;现在让我们看一下我        完成的工作!在分析过程中,我定义了一个简单的数据结构(以及相应的 XML 词汇表)用于文件的描述。为了用 XI 处理新文件,我编写了另一个描述。      
我通过使用下列文件实现数据结构着手:
  • Ruleset (请参阅 )表示一组正则表达式。
  • Match (请参阅 )表示一个正则表达式。该类封装了           java.regex 。
  • Group (请参阅 )是来自正则表达式的组(加圆括号的表达式)。
清单 3. Ruleset.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.ananas.xi;
import java.util.*;
public class Ruleset
   extends QName
{
   private List matches = new ArrayList();
   private String error = null;
   public Ruleset(String namespaceURI,
                  String localName,
                  String qualifiedName)
   {
      super(namespaceURI,localName,qualifiedName);
   }
   public void setError(String error)
   {
      this.error = error;
   }
   public String getError()
   {
      return error;
   }
   public synchronized void addMatch(Match match)
   {
      matches.add(match);
   }
   public synchronized Match getMatchAt(int index)
   {
      return (Match)matches.get(index);
   }
   public synchronized int getMatchCount()
   {
      return matches.size();
   }
}




Ruleset 实质上是一个用于         Match 对象列表的容器。      
清单 4. Match.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.ananas.xi;
import java.util.*;
import java.util.regex.*;
public class Match
   extends QName
{
   private Pattern pattern;
   private Matcher matcher = null;
   private String input = null;
   private List groups = new ArrayList();
   public Match(String namespaceURI,
                String localName,
                String qualifiedName,
                String pattern)
   {
      super(namespaceURI,localName,qualifiedName);
      this.pattern = Pattern.compile(pattern);
   }
   public synchronized void addGroup(Group group)
   {
      groups.add(group);
   }
   public synchronized Group getGroupNameAt(int index)
   {
      if(index < 1 || index > groups.size())
         throw new IndexOutOfBoundsException("index out of bounds");
      return (Group)groups.get(index - 1);
   }
   public synchronized String getGroupValueAt(int index)
      throws IllegalStateException, IllegalArgumentException
   {
      if(matcher == null)
         throw new IllegalStateException("Call matches() first");
      return getGroupNameAt(index).isText() ?
             matcher.group(0) : matcher.group(index);
   }
   public synchronized int getGroupCount()
   {
      return groups.size();
   }
   public boolean matches(String st)
   {
      input = st;
      if(matcher == null)
         matcher = pattern.matcher(st);
      else
         matcher.reset(st);
      return matcher.lookingAt();
   }
   public String rest()
   {
      if(matcher == null)
         throw new IllegalStateException("Call matches() first");
      int end = matcher.end(),
          length = input.length();
      if(end < length)
         return input.substring(end,length);
      else
         return null;
   }
}




Match 是这个数据结构中最重要的类。它表示一个正则表达式并提供了使正则表达式与字符串相匹配的逻辑。注:它使用         lookingAt() 来应用正则表达式。因为         lookingAt() 能与部分字符串相匹配,所以将字符串分解成子串是可能的。      
清单 5. Group.java
1
2
3
4
5
6
7
8
9
10
11
package org.ananas.xi;
public class Group
   extends QName
{
   public Group(String namespaceURI,
                String localName,
                String qualifiedName)
   {
      super(namespaceURI,localName,qualifiedName);
   }
}




所有类均由         QName (请参阅 )派生而来,         QName 将 XML 元素名称表示成名称空间 URI 和本地名的组合。      
清单 6. QName.java
1
2
3
4
5
6
7
8
9
10
11
package org.ananas.xi;
public class Group
   extends QName
{
   public Group(String namespaceURI,
                String localName,
                String qualifiedName)
   {
      super(namespaceURI,localName,qualifiedName);
   }
}

返回列表