KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > pattern > RefPattern


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.relaxng.pattern;
30
31 import com.caucho.relaxng.RelaxException;
32 import com.caucho.relaxng.program.Item;
33
34 /**
35  * Relax reference pattern
36  */

37 public class RefPattern extends Pattern {
38   private GrammarPattern _grammar;
39   
40   private String JavaDoc _refName;
41
42   /**
43    * Creates a new element pattern.
44    */

45   public RefPattern(GrammarPattern grammar, String JavaDoc refName)
46   {
47     _grammar = grammar;
48     
49     _refName = refName;
50   }
51
52   /**
53    * Returns the Relax schema name.
54    */

55   public String JavaDoc getTagName()
56   {
57     return "ref";
58   }
59
60   /**
61    * Returns the definition name.
62    */

63   public String JavaDoc getRefName()
64   {
65     return _refName;
66   }
67
68   /**
69    * Creates the item.
70    */

71   public Item createItem(GrammarPattern grammar)
72     throws RelaxException
73   {
74     Pattern pattern = grammar.getDefinition(_refName);
75
76     if (pattern == null) {
77       // XXX: line #
78
throw error(L.l("<ref name=\"{0}\"/> is an unknown reference.",
79               _refName));
80     }
81
82     for (Pattern ptr = this;
83          ptr != null && ! (ptr instanceof ElementPattern);
84          ptr = ptr.getParent()) {
85       if (ptr == pattern) {
86         throw error(L.l("<define name=\"{0}\"/> calls itself recursively in a <ref/>.",
87             _refName));
88       }
89     }
90
91     return pattern.createItem(grammar);
92   }
93
94   /**
95    * Returns a string for the production.
96    */

97   public String JavaDoc toProduction()
98   {
99     Pattern pattern = _grammar.getDefinition(_refName);
100
101     return pattern.toProduction();
102   }
103
104   /**
105    * Returns true if the pattern equals.
106    */

107   public boolean equals(Object JavaDoc o)
108   {
109     if (this == o)
110       return true;
111
112     if (! (o instanceof Pattern))
113       return false;
114
115     return o.equals(_grammar.getDefinition(_refName));
116   }
117
118   /**
119    * Debugging.
120    */

121   public String JavaDoc toString()
122   {
123     return "RefPattern[" + _refName + "]";
124   }
125 }
126
127
Popular Tags