OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / main / java / org / xerial / silk / impl / SilkNode.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2009 Taro L. Saito\r
3  *\r
4  *  Licensed under the Apache License, Version 2.0 (the "License");\r
5  *  you may not use this file except in compliance with the License.\r
6  *  You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  *  Unless required by applicable law or agreed to in writing, software\r
11  *  distributed under the License is distributed on an "AS IS" BASIS,\r
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  *  See the License for the specific language governing permissions and\r
14  *  limitations under the License.\r
15  *--------------------------------------------------------------------------*/\r
16 //--------------------------------------\r
17 // XerialJ\r
18 //\r
19 // SilkNode.java\r
20 // Since: Jan 30, 2009 6:58:59 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/impl/SilkNode.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.silk.impl;\r
26 \r
27 import java.util.ArrayList;\r
28 \r
29 import org.xerial.util.StringUtil;\r
30 \r
31 /**\r
32  * SilkNode begins with '-' character, follwed by object notation of the form:\r
33  * \r
34  * <pre>\r
35  *  object_name (: nodeValue)? '(' attribute, ... ')' ([format_name])? ('*' | '&gt;' | '|')\r
36  * </pre>\r
37  * \r
38  * @author leo\r
39  * \r
40  */\r
41 public class SilkNode implements SilkElement\r
42 {\r
43     private String indent = null;\r
44     private String name;\r
45     private SilkValue value = null;\r
46 \r
47     private String dataType = null;\r
48     private SilkNodeOccurrence occurrence = SilkNodeOccurrence.ONE;\r
49     private ArrayList<SilkNode> childNodeList = new ArrayList<SilkNode>();\r
50 \r
51     public String getNodeIndent()\r
52     {\r
53         return indent;\r
54     }\r
55 \r
56     public boolean hasManyOccurrences()\r
57     {\r
58         return occurrence == SilkNodeOccurrence.ZERO_OR_MORE || occurrence == SilkNodeOccurrence.ONE_OR_MORE;\r
59     }\r
60 \r
61     public boolean hasChildren()\r
62     {\r
63         return childNodeList.size() > 0;\r
64     }\r
65 \r
66     public final static int NO_INDENT = Integer.MAX_VALUE; // must be higher than other indent levels\r
67     public final static int INTERMIDIATE_NODE_INDENT = Integer.MAX_VALUE - 1; // \r
68 \r
69     /**\r
70      * Return the indent level (the length of the leadning white spaces) of this\r
71      * node. If no indent is specified, return {@link SilkNode#NO_INDENT}.\r
72      * \r
73      * @return the indent level of the node, or {@link SilkNode#NO_INDENT} if no\r
74      *         indent is specified.\r
75      */\r
76     public int getIndentLevel()\r
77     {\r
78         if (indent == null)\r
79             return NO_INDENT;\r
80         else\r
81             return indent.length() - 1;\r
82     }\r
83 \r
84     public void setNodeIndent(String indent)\r
85     {\r
86         this.indent = indent;\r
87     }\r
88 \r
89     public String getName()\r
90     {\r
91         return name;\r
92     }\r
93 \r
94     public void setName(String name)\r
95     {\r
96         this.name = name;\r
97     }\r
98 \r
99     public boolean hasDataType()\r
100     {\r
101         return dataType != null;\r
102     }\r
103 \r
104     public String getDataType()\r
105     {\r
106         return dataType;\r
107     }\r
108 \r
109     public void setDataType(String dataType)\r
110     {\r
111         this.dataType = dataType;\r
112     }\r
113 \r
114     public SilkNodeOccurrence getOccurrence()\r
115     {\r
116         return occurrence;\r
117     }\r
118 \r
119     public void setOccurrence(SilkNodeOccurrence occurrence)\r
120     {\r
121         this.occurrence = occurrence;\r
122     }\r
123 \r
124     public ArrayList<SilkNode> getChildNodes()\r
125     {\r
126         return childNodeList;\r
127     }\r
128 \r
129     public void addSilkNode(SilkNode childNode)\r
130     {\r
131         this.childNodeList.add(childNode);\r
132     }\r
133 \r
134     public void setValue(String text)\r
135     {\r
136         this.value = new SilkTextValue(text);\r
137     }\r
138 \r
139     public void setJSON(String jsonText)\r
140     {\r
141         this.value = new SilkJSONValue(jsonText);\r
142     }\r
143 \r
144     public void setFunction(SilkFunction func)\r
145     {\r
146         this.value = func;\r
147     }\r
148 \r
149     public SilkValue getValue()\r
150     {\r
151         return value;\r
152     }\r
153 \r
154     public boolean hasValue()\r
155     {\r
156         return value != null;\r
157     }\r
158 \r
159     @Override\r
160     public String toString()\r
161     {\r
162         String childNodeString = childNodeList.isEmpty() ? "" : String.format("(%s)", StringUtil.join(childNodeList,\r
163                 ", "));\r
164         String nodeValue = value != null ? ":" + value.toString() : "";\r
165         if (hasDataType())\r
166             return String.format("%s[%s]<%s>%s%s", name, dataType, childNodeString, occurrence, nodeValue);\r
167         else\r
168             return String.format("%s%s<%s>%s", name, childNodeString, occurrence, nodeValue);\r
169     }\r
170 \r
171 }\r