Beispiel #1
0
        public void Test1_Values1()
        {
            StructuredText r = CreateRoot();

            r.Set("A", "B");
            Assert.AreEqual("Poderosa {\r\n  A=B\r\n}\r\n", Dump(r));
        }
        private void WriteNode(StructuredText node)
        {
            WriteIndent();
            _writer.Write(node.Name);
            _writer.WriteLine(" {");
            _indent += INDENT_UNIT;

            foreach (object ch in node.Children)
            {
                StructuredText.Entry e = ch as StructuredText.Entry;
                if (e != null)                //entry
                {
                    WriteIndent();
                    _writer.Write(e.name);
                    _writer.Write('=');
                    _writer.WriteLine(e.value);
                }
                else                   //child node
                {
                    WriteNode((StructuredText)ch);
                }
            }

            _indent -= INDENT_UNIT;
            WriteIndent();
            _writer.WriteLine("}");
        }
        /// <summary>
        /// ディープコピーするが、親からは切り離される
        /// </summary>
        public object Clone()
        {
            StructuredText np = new StructuredText(null, _name);

            np._children = CollectionUtil.DeepCopyArrayList(_children);
            return(np);
        }
 public StructuredText AddChild(StructuredText child)
 {
     _dirtyFlag    = true;
     child._parent = this;
     _children.Add(child);
     return(this);
 }
        private void ProcessLine(string line)
        {
            if (line.Length == 0 || line[0] == '#')
            {
                return;                                            //comment line
            }
            int e = line.IndexOf('=');

            if (e != -1 && _current != null)
            {
                string name0 = CutPrecedingSpace(line.Substring(0, e));
                string value = e == line.Length - 1? "" : CutPrecedingSpace(line.Substring(e + 1));
                _current.Set(name0, value);
            }
            else if (line[line.Length - 1] == '{')
            {
                string name = line.Substring(0, line.IndexOf(' '));
                _current = _current == null? new StructuredText(null, name) : _current.AddChild(name);
                if (_root == null)
                {
                    _root = _current;                             //最初のNodeをルートとする
                }
            }
            else if (line[line.Length - 1] == '}')
            {
                _current = _current.Parent;
            }
        }
        private void WriteNode(StructuredText node)
        {
            _writer.WriteStartElement(node.Name);

            // first, output StructuredText.Entry as a XML attribute.
            List <StructuredText> childNodes = new List <StructuredText>();

            foreach (object ch in node.Children)
            {
                StructuredText.Entry e = ch as StructuredText.Entry;
                if (e != null)   //entry
                {
                    _writer.WriteAttributeString(e.name, e.value);
                }
                else   //child node
                {
                    childNodes.Add((StructuredText)ch);
                }
            }

            // second, output StructuredText as a XML tag.
            foreach (StructuredText ch in childNodes)
            {
                WriteNode(ch);
            }

            _writer.WriteEndElement();
        }
Beispiel #7
0
        public static string DumpStructuredText(StructuredText st)
        {
            StringWriter wr = new StringWriter();

            new TextStructuredTextWriter(wr).Write(st);
            wr.Close();
            return(wr.ToString());
        }
Beispiel #8
0
        private string Dump(StructuredText node)
        {
            StringWriter w = new StringWriter();

            new TextStructuredTextWriter(w).Write(node);
            w.Close();
            return(w.ToString());
        }
        public StructuredText AddChild(string name)
        {
            _dirtyFlag = true;

            StructuredText n = new StructuredText(this, name);

            _children.Add(n);
            return(n);
        }
Beispiel #10
0
        public void Test1_Values4()
        {
            StructuredText r = CreateRoot();

            r.Set("A", "P");
            r.Set("B", "Q");
            r.Set("C", "R");
            r.ClearValue("B");
            Assert.AreEqual("Poderosa {\r\n  A=P\r\n  C=R\r\n}\r\n", Dump(r));
        }
 private StructuredText Read(XmlElement elem) {
     StructuredText node = new StructuredText(elem.LocalName);
     foreach (XmlAttribute attr in elem.Attributes)
         node.Set(attr.LocalName, attr.Value);
     foreach (XmlNode ch in elem.ChildNodes) {
         XmlElement ce = ch as XmlElement;
         if (ce != null)
             node.AddChild(Read(ce));
     }
     return node;
 }
Beispiel #12
0
        public void Test2_Nodes2()
        {
            StructuredText r = CreateRoot();
            StructuredText c = r.GetOrCreateChild("XXX.YYY.ZZZ");

            Assert.AreEqual("ZZZ", c.Name);
            Assert.AreEqual("YYY", c.Parent.Name);
            Assert.AreEqual("XXX", c.Parent.Parent.Name);
            Assert.AreSame(r, c.Parent.Parent.Parent);
            Assert.AreSame(c, r.GetOrCreateChild("XXX.YYY.ZZZ"));
        }
Beispiel #13
0
        public void Test1_Values3()
        {
            StructuredText r = CreateRoot();

            r.Set("A", "B");
            Assert.AreEqual("B", r.Get("A"));
            Assert.IsNull(r.Get("Q"));
            Assert.AreEqual("V", r.Get("Q", "V"));
            r.SetOrReplace("A", "C");

            Assert.AreEqual("Poderosa {\r\n  A=C\r\n}\r\n", Dump(r));
        }
 public StructuredText FindChild(string name)
 {
     foreach (object o in _children)
     {
         StructuredText n = o as StructuredText;
         if (n != null && n.Name == name)
         {
             return(n);
         }
     }
     return(null);
 }
        public override StructuredText Read()
        {
            _current = null;
            _root    = null;
            string line = _reader.ReadLine();

            while (line != null)
            {
                ProcessLine(CutPrecedingSpace(line));
                line = _reader.ReadLine();
            }
            return(_root);
        }
        //TODO 実はこの戻りをまたコレクションにコピーすることがよくある。むだだ
        public IList FindMultipleNote(string name)
        {
            ArrayList r = new ArrayList();

            foreach (object o in _children)
            {
                StructuredText n = o as StructuredText;
                if (n != null && n.Name == name)
                {
                    r.Add(n);
                }
            }
            return(r);
        }
Beispiel #17
0
        public void Test4_Clone()
        {
            StructuredText r = CreateRoot();
            StructuredText y = r.GetOrCreateChild("XXX.YYY");

            y.Set("A", "B");

            StructuredText nx = (StructuredText)y.Parent.Clone();

            nx.FindChild("YYY").SetOrReplace("A", "C");
            Assert.IsNull(nx.Parent);
            Assert.AreEqual("XXX {\r\n  YYY {\r\n    A=C\r\n  }\r\n}\r\n", Dump(nx));
            Assert.AreEqual("YYY {\r\n  A=B\r\n}\r\n", Dump(y));
        }
        // child nodes
        //子孫まで一気に作成もOK
        public StructuredText GetOrCreateChild(string name)
        {
            _dirtyFlag = true;

            int            comma = name.IndexOf('.');
            string         local = comma == -1? name : name.Substring(0, comma);
            StructuredText n     = FindChild(local);

            if (n == null)
            {
                n = new StructuredText(this, local);
                _children.Add(n);
            }
            return(comma == -1? n : n.GetOrCreateChild(name.Substring(comma + 1)));
        }
Beispiel #19
0
        public void Test3_Parse1()
        {
            StructuredText r = CreateRoot();
            StructuredText z = r.GetOrCreateChild("XXX.YYY.ZZZ");

            z.Set("A", "B");
            z.Set("C", "D");
            StructuredText z2 = z.Parent.AddChild("ZZZ");

            z2.Set("E", "F");

            //一回変換してOK?
            StructuredText r2 = new TextStructuredTextReader(new StringReader(Dump(r))).Read();

            Assert.AreEqual(Dump(r), Dump(r2));
        }
Beispiel #20
0
        public void Test2_Nodes1()
        {
            StructuredText r  = CreateRoot();
            StructuredText c1 = r.GetOrCreateChild("C");

            Assert.AreEqual("C", c1.Name);
            Assert.AreSame(r, c1.Parent);

            StructuredText c2 = r.AddChild("C");

            Assert.AreSame(r, c2.Parent);

            Assert.AreSame(c1, r.FindChild("C")); //must be the first child
            IList il = r.FindMultipleNote("C");

            Assert.AreEqual(2, il.Count);
            Assert.AreSame(c2, il[1]);
        }
        private StructuredText Read(XmlElement elem)
        {
            StructuredText node = new StructuredText(elem.LocalName);

            foreach (XmlAttribute attr in elem.Attributes)
            {
                node.Set(attr.LocalName, attr.Value);
            }
            foreach (XmlNode ch in elem.ChildNodes)
            {
                XmlElement ce = ch as XmlElement;
                if (ce != null)
                {
                    node.AddChild(Read(ce));
                }
            }
            return(node);
        }
        private void WriteNode(StructuredText node)
        {
            _writer.WriteStartElement(node.Name);

            foreach (object ch in node.Children)
            {
                StructuredText.Entry e = ch as StructuredText.Entry;
                if (e != null) //entry
                {
                    _writer.WriteAttributeString(e.name, e.value);
                }
                else   //child node
                {
                    WriteNode((StructuredText)ch);
                }
            }

            _writer.WriteEndElement();
        }
        private void WriteNode(StructuredText node) {
            _writer.WriteStartElement(node.Name);

            // first, output StructuredText.Entry as a XML attribute.
            List<StructuredText> childNodes = new List<StructuredText>();
            foreach (object ch in node.Children) {
                StructuredText.Entry e = ch as StructuredText.Entry;
                if (e != null) { //entry
                    _writer.WriteAttributeString(e.name, e.value);
                }
                else { //child node
                    childNodes.Add((StructuredText)ch);
                }
            }

            // second, output StructuredText as a XML tag.
            foreach (StructuredText ch in childNodes) {
                WriteNode(ch);
            }

            _writer.WriteEndElement();
        }
Beispiel #24
0
 public abstract void Write(StructuredText node);
Beispiel #25
0
 public override StructuredText Read()
 {
     _current = null;
     _root = null;
     string line = _reader.ReadLine();
     while (line != null) {
         ProcessLine(CutPrecedingSpace(line));
         line = _reader.ReadLine();
     }
     return _root;
 }
Beispiel #26
0
 private string Dump(StructuredText node)
 {
     StringWriter w = new StringWriter();
     new TextStructuredTextWriter(w).Write(node);
     w.Close();
     return w.ToString();
 }
Beispiel #27
0
        private void ProcessLine(string line)
        {
            if (line.Length == 0 || line[0] == '#')
                return; //comment line

            int e = line.IndexOf('=');
            if (e != -1 && _current != null) {
                string name0 = CutPrecedingSpace(line.Substring(0, e));
                string value = e == line.Length - 1 ? "" : CutPrecedingSpace(line.Substring(e + 1));
                _current.Set(name0, value);
            }
            else if (line[line.Length - 1] == '{') {
                string name = line.Substring(0, line.IndexOf(' '));
                _current = _current == null ? new StructuredText(null, name) : _current.AddChild(name);
                if (_root == null)
                    _root = _current; //最初のNodeをルートとする
            }
            else if (line[line.Length - 1] == '}') {
                _current = _current.Parent;
            }
        }
        public void RemoveChild(StructuredText child)
        {
            _dirtyFlag = true;

            _children.Remove(child);
        }
Beispiel #29
0
 /// <summary>
 /// ディープコピーするが、親からは切り離される
 /// </summary>
 public object Clone()
 {
     StructuredText np = new StructuredText(null, _name);
     np._children = CollectionUtil.DeepCopyArrayList(_children);
     return np;
 }
 public override void Write(StructuredText node)
 {
     WriteNode(node);
 }
Beispiel #31
0
        private void WriteNode(StructuredText node)
        {
            WriteIndent();
            _writer.Write(node.Name);
            _writer.WriteLine(" {");
            _indent += INDENT_UNIT;

            foreach (object ch in node.Children) {
                StructuredText.Entry e = ch as StructuredText.Entry;
                if (e != null) { //entry
                    WriteIndent();
                    _writer.Write(e.name);
                    _writer.Write('=');
                    _writer.WriteLine(e.value);
                }
                else { //child node
                    WriteNode((StructuredText)ch);
                }
            }

            _indent -= INDENT_UNIT;
            WriteIndent();
            _writer.WriteLine("}");
        }
 public override void Write(StructuredText node)
 {
     _indent = 0;
     WriteNode(node);
 }
Beispiel #33
0
 //note that assembly private
 internal StructuredText(StructuredText parent, string name)
 {
     _name = name;
     _parent = parent;
     _children = new ArrayList();
 }
Beispiel #34
0
        public void RemoveChild(StructuredText child)
        {
            _dirtyFlag = true;

            _children.Remove(child);
        }
Beispiel #35
0
 public override void Write(StructuredText node)
 {
     _indent = 0;
     WriteNode(node);
 }
Beispiel #36
0
        // child nodes
        //子孫まで一気に作成もOK
        public StructuredText GetOrCreateChild(string name)
        {
            _dirtyFlag = true;

            int comma = name.IndexOf('.');
            string local = comma == -1 ? name : name.Substring(0, comma);
            StructuredText n = FindChild(local);
            if (n == null) {
                n = new StructuredText(this, local);
                _children.Add(n);
            }
            return comma == -1 ? n : n.GetOrCreateChild(name.Substring(comma + 1));
        }
 public override void Write(StructuredText node) {
     WriteNode(node);
 }
 //note that assembly private
 internal StructuredText(StructuredText parent, string name)
 {
     _name     = name;
     _parent   = parent;
     _children = new ArrayList();
 }
Beispiel #39
0
 public StructuredText AddChild(StructuredText child)
 {
     _dirtyFlag = true;
     child._parent = this;
     _children.Add(child);
     return this;
 }
 public abstract void Write(StructuredText node);
Beispiel #41
0
        public StructuredText AddChild(string name)
        {
            _dirtyFlag = true;

            StructuredText n = new StructuredText(this, name);
            _children.Add(n);
            return n;
        }