1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 // Since: Jan 30, 2009 6:58:59 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/silk/impl/SilkNode.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.silk.impl;
\r
27 import java.util.ArrayList;
\r
29 import org.xerial.util.StringUtil;
\r
32 * SilkNode begins with '-' character, follwed by object notation of the form:
\r
35 * object_name (: nodeValue)? '(' attribute, ... ')' ([format_name])? ('*' | '>' | '|')
\r
41 public class SilkNode implements SilkElement
\r
43 private String indent = null;
\r
44 private String name;
\r
45 private SilkValue value = null;
\r
47 private String dataType = null;
\r
48 private SilkNodeOccurrence occurrence = SilkNodeOccurrence.ONE;
\r
49 private ArrayList<SilkNode> childNodeList = new ArrayList<SilkNode>();
\r
51 public String getNodeIndent()
\r
56 public boolean hasManyOccurrences()
\r
58 return occurrence == SilkNodeOccurrence.ZERO_OR_MORE || occurrence == SilkNodeOccurrence.ONE_OR_MORE;
\r
61 public boolean hasChildren()
\r
63 return childNodeList.size() > 0;
\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
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
73 * @return the indent level of the node, or {@link SilkNode#NO_INDENT} if no
\r
74 * indent is specified.
\r
76 public int getIndentLevel()
\r
81 return indent.length() - 1;
\r
84 public void setNodeIndent(String indent)
\r
86 this.indent = indent;
\r
89 public String getName()
\r
94 public void setName(String name)
\r
99 public boolean hasDataType()
\r
101 return dataType != null;
\r
104 public String getDataType()
\r
109 public void setDataType(String dataType)
\r
111 this.dataType = dataType;
\r
114 public SilkNodeOccurrence getOccurrence()
\r
119 public void setOccurrence(SilkNodeOccurrence occurrence)
\r
121 this.occurrence = occurrence;
\r
124 public ArrayList<SilkNode> getChildNodes()
\r
126 return childNodeList;
\r
129 public void addSilkNode(SilkNode childNode)
\r
131 this.childNodeList.add(childNode);
\r
134 public void setValue(String text)
\r
136 this.value = new SilkTextValue(text);
\r
139 public void setJSON(String jsonText)
\r
141 this.value = new SilkJSONValue(jsonText);
\r
144 public void setFunction(SilkFunction func)
\r
149 public SilkValue getValue()
\r
154 public boolean hasValue()
\r
156 return value != null;
\r
160 public String toString()
\r
162 String childNodeString = childNodeList.isEmpty() ? "" : String.format("(%s)", StringUtil.join(childNodeList,
\r
164 String nodeValue = value != null ? ":" + value.toString() : "";
\r
166 return String.format("%s[%s]<%s>%s%s", name, dataType, childNodeString, occurrence, nodeValue);
\r
168 return String.format("%s%s<%s>%s", name, childNodeString, occurrence, nodeValue);
\r