summaryrefslogtreecommitdiffstats
path: root/configgen/src/main/java/com/yahoo/config/codegen/CNode.java
diff options
context:
space:
mode:
Diffstat (limited to 'configgen/src/main/java/com/yahoo/config/codegen/CNode.java')
-rw-r--r--configgen/src/main/java/com/yahoo/config/codegen/CNode.java161
1 files changed, 161 insertions, 0 deletions
diff --git a/configgen/src/main/java/com/yahoo/config/codegen/CNode.java b/configgen/src/main/java/com/yahoo/config/codegen/CNode.java
new file mode 100644
index 00000000000..02fec548b40
--- /dev/null
+++ b/configgen/src/main/java/com/yahoo/config/codegen/CNode.java
@@ -0,0 +1,161 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.config.codegen;
+
+import java.util.StringTokenizer;
+
+/**
+ * Abstract superclass for all nodes representing a definition file.
+ *
+ * @author <a href="gv@yahoo-inc.com">G. Voldengen</a>
+ */
+public abstract class CNode {
+
+ public static final String DEFAULT_NAMESPACE = "config";
+
+ // TODO: replace by "type" enum
+ public final boolean isArray;
+ public final boolean isMap;
+ final String name;
+ final InnerCNode parent;
+
+ // TODO: remove! Only set for the root node, and root.getName() returns the same thing!
+ String defName = null;
+ String defVersion = "";
+ String defNamespace = null;
+ String defMd5 = "MISSING MD5";
+ String comment = "";
+
+
+ protected CNode(InnerCNode parent, String name) {
+ this.parent = parent;
+ int bracketIdx = name.indexOf('[');
+ int curlyIdx = name.indexOf('{');
+ if (bracketIdx != -1) {
+ this.name = name.substring(0, bracketIdx);
+ isArray = true;
+ isMap = false;
+ } else if (curlyIdx != -1) {
+ this.name = name.substring(0, curlyIdx);
+ isMap = true;
+ isArray = false;
+ } else {
+ this.name = name;
+ isMap = false;
+ isArray = false;
+ }
+ }
+
+ /**
+ * Returns the simple name of this node.
+ * @return the simple name of this node
+ */
+ public String getName() {
+ return name;
+ }
+
+ public InnerCNode getParent() {
+ return parent;
+ }
+
+ public abstract CNode[] getChildren();
+
+ public abstract CNode getChild(String name);
+
+ public String getMd5() {
+ return defMd5;
+ }
+
+ void setMd5(String md5) {
+ defMd5 = md5;
+ }
+
+ public String getVersion() {
+ return defVersion;
+ }
+
+ void setVersion(String version) {
+ defVersion = version;
+ }
+
+ public String getNamespace() {
+ return defNamespace;
+ }
+
+ void setNamespace(String namespace) {
+ defNamespace = namespace;
+ }
+
+ public String getComment() {
+ return comment;
+ }
+
+ void setComment(String comment) {
+ this.comment = comment;
+ }
+
+ protected abstract void setLeaf(String name, DefLine defLine, String comment)
+ throws IllegalArgumentException;
+
+ public abstract boolean needRestart();
+
+ protected void checkMyName(String myName) throws IllegalArgumentException {
+ if (isArray) {
+ int n1 = myName.indexOf('[');
+ int n2 = myName.indexOf(']');
+ if (n1 == -1 || n2 < n1)
+ throw new IllegalArgumentException("Invalid array syntax: " + myName);
+ myName = myName.substring(0, n1);
+ } else if (isMap) {
+ int n1 = myName.indexOf('{');
+ int n2 = myName.indexOf('}');
+ if (n1 == -1 || n2 < n1)
+ throw new IllegalArgumentException("Invalid map syntax: " + myName);
+ myName = myName.substring(0, n1);
+ } else if (myName.contains("[]")) {
+ throw new IllegalArgumentException("Parameter with name '" + getName() + "' has already been declared as a non-array type.");
+ } else if (myName.contains("{}")) {
+ throw new IllegalArgumentException("Parameter with name '" + getName() + "' has already been declared as a non-map type.");
+ }
+ if (!myName.equals(getName()))
+ throw new IllegalArgumentException(myName + " does not match " + getName() + ".");
+ }
+
+ /**
+ * @return the full name as a config path of this node.
+ */
+ public String getFullName() {
+ StringBuilder buf = new StringBuilder();
+ if (parent != null)
+ buf.append(parent.getFullName());
+ if (buf.length() > 0)
+ buf.append('.');
+ StringBuilder theName = new StringBuilder(this.getName());
+
+ if (isArray) theName.append("[]");
+ else if (isMap) theName.append("{}");
+
+ return buf.append(theName).toString();
+ }
+
+ /**
+ * @param prefix The prefix to use, usually an indent (spaces) followed by either '*' or "//"
+ * @return a comment block where each line is prefixed, but the caller must close it if using '*'.
+ */
+ public String getCommentBlock(String prefix) {
+ prefix = prefix + " ";
+ StringBuilder ret = new StringBuilder();
+ if (getComment().length() > 0) {
+ StringTokenizer st = new StringTokenizer(getComment(), "\n");
+ while (st.hasMoreTokens()) {
+ ret.append(prefix).append(st.nextToken()).append("\n");
+ }
+ }
+ return ret.toString();
+ }
+
+ @Override
+ public String toString() {
+ return getNamespace()+"."+getName()+","+getVersion();
+ }
+
+}