public bool parse(ref char[] buffer, int count, ref MarkupNode rootNode) { if (buffer == null || count < 0 || count > buffer.Count()) { throw new Exception("缓存为空或者索引越界"); } if (count == 0) { return(false); } { int curIdx = 0; Stack <MarkupNode> stackNode = new Stack <MarkupNode>(); MarkupNode newNode = null; stackNode.Push(rootNode); while (stackNode.Count() > 0) { MarkupNode curNode = stackNode.Peek(); { // 解析结束标签 curIdx = skipWhiteSpace(ref buffer, curIdx); if (buffer[curIdx] == (char)0) { return(true); } if (buffer[curIdx] == '<' && buffer[curIdx + 1] == '/') { // 找到结点结束标签,栈顶结点出栈 curNode = stackNode.Pop(); curIdx += 2; string nodeName = ""; curIdx = parseIdentifier(ref buffer, curIdx, out nodeName); if (curNode.getName() != nodeName) { throw new Exception("结束标签名称不匹配"); } if (buffer[curIdx] != '>') { throw new Exception("结束标签名称不匹配"); } curIdx += 1; curIdx = skipWhiteSpace(ref buffer, curIdx); continue; } } { // 解析开始标志 curIdx = skipWhiteSpace(ref buffer, curIdx); if (buffer[curIdx] == (char)0) { return(true); } if (buffer[curIdx] != '<') { throw new Exception("找不到开始标签 <"); } curIdx++; } { // 过滤注释 int lastIdx = curIdx; curIdx = skipComment(ref buffer, curIdx); if (lastIdx != curIdx) { curIdx = skipWhiteSpace(ref buffer, curIdx); continue; } } { // 解析节点名字 curIdx = skipWhiteSpace(ref buffer, curIdx); if (newNode == null) { string nodeName = ""; curIdx = parseIdentifier(ref buffer, curIdx, out nodeName); if (buffer[curIdx] == (char)0) { throw new Exception("无法解析节点名字"); } curNode.setName(nodeName); newNode = curNode; } else { string nodeName = ""; curIdx = parseIdentifier(ref buffer, curIdx, out nodeName); if (buffer[curIdx] == (char)0) { throw new Exception("无法解析节点名字"); } newNode = new MarkupNode(curNode); newNode.setName(nodeName); stackNode.Push(newNode); curNode = newNode; } } { // 解析节点属性值 bool result = parseAttributes(ref buffer, ref curIdx, ref curNode); if (result == false) { return(false); } // 解析节点结束标签 curIdx = skipWhiteSpace(ref buffer, curIdx); if (buffer[curIdx] == '/' && buffer[curIdx + 1] == '>') { curIdx += 2; stackNode.Pop(); } else { // 解析节点关闭标签 if (buffer[curIdx] != '>') { throw new Exception("找不到关闭标签 >"); } { // 解析节点值 curIdx++; string value = ""; bool parseResult = parseData(ref buffer, ref curIdx, '<', out value); if (parseResult == false) { return(false); } if (isWhiteSpace(value)) { value = ""; } curNode.setValue(value); } { if (buffer[curIdx] == (char)0) { return(true); } if (buffer[curIdx] != '<') { throw new Exception("找不到子节点开始标签 <"); } } } } } stackNode.Clear(); stackNode = null; } return(true); }
protected bool parseWindowAttributes(ref MarkupNode root, ref PaintManagerUI manager) { if (manager != null) { string className = root.getName(); if (className == "Window") { List <MarkupNode> listNode = root.getChildList(); string imageName = ""; string imageResType = ""; uint mask = 0; foreach (var node in listNode) { if (node.getName() == "Image") { List <XMLAttribute> listAttr = node.getAttributeList(); foreach (var attr in listAttr) { if (attr.getName() == "name") { imageName = attr.getValue(); } else if (attr.getName() == "restype") { imageResType = attr.getValue(); } else if (attr.getName() == "mask") { string sMask = attr.getValue(); sMask = sMask.TrimStart('#'); mask = uint.Parse(sMask); } } if (imageName != "") { manager.addImage(imageName, imageResType, (int)mask); } } else if (node.getName() == "Font") { string fontName = ""; int size = 10; bool bold = false; bool underline = false; bool italic = false; bool defaultfont = false; bool defaultboldfont = false; bool defaultlinkfont = false; List <XMLAttribute> listAttr = node.getAttributeList(); foreach (var attr in listAttr) { if (attr.getName() == "name") { fontName = attr.getValue(); } else if (attr.getName() == "size") { size = int.Parse(attr.getValue()); } else if (attr.getName() == "bold") { bold = attr.getValue() == "true"; } else if (attr.getName() == "underline") { underline = attr.getValue() == "true"; } else if (attr.getName() == "italic") { italic = attr.getValue() == "true"; } else if (attr.getName() == "default") { defaultfont = attr.getValue() == "true"; } else if (attr.getName() == "defaultbold") { defaultboldfont = attr.getValue() == "true"; } else if (attr.getName() == "defaultlink") { defaultlinkfont = attr.getValue() == "true"; } } if (fontName != "") { Font font = manager.addFont(fontName, size - 3, bold, underline, italic); if (font != null) { if (defaultfont) { manager.setDefaultFont(font); } if (defaultboldfont) { manager.setDefaultBoldFont(font); } if (defaultlinkfont) { manager.setDefaultLinkFont(font); } } } } else if (node.getName() == "Default") { List <XMLAttribute> listAttr = node.getAttributeList(); string ctlName = ""; string ctlValue = ""; foreach (var attr in listAttr) { if (attr.getName() == "name") { ctlName = attr.getValue(); } else if (attr.getName() == "value") { ctlValue = attr.getValue(); } } if (ctlName != "") { manager.addDefaultAttributeList(ctlName, ctlValue); } } } if (manager.getPaintWindow() != null && mIsMainWindow) { List <XMLAttribute> listAttr = root.getAttributeList(); foreach (var attr in listAttr) { if (attr.getName() == "size") { string[] listValue = attr.getValue().Split(','); if (listValue.Length != 2) { throw new Exception(""); } int cx = int.Parse(listValue[0]); int cy = int.Parse(listValue[1]); manager.setInitSize(cx, cy); } else if (attr.getName() == "sizebox") { string[] listValue = attr.getValue().Split(','); if (listValue.Length != 4) { throw new Exception(""); } int left = int.Parse(listValue[0]); int top = int.Parse(listValue[1]); int right = int.Parse(listValue[2]); int bottom = int.Parse(listValue[3]); Rectangle rect = new Rectangle(left, top, right - left, bottom - top); manager.setSizeBox(ref rect); } else if (attr.getName() == "caption") { string[] listValue = attr.getValue().Split(','); if (listValue.Length != 4) { throw new Exception(""); } int left = int.Parse(listValue[0]); int top = int.Parse(listValue[1]); int right = int.Parse(listValue[2]); int bottom = int.Parse(listValue[3]); Rectangle rect = new Rectangle(left, top, right - left, bottom - top); manager.setCaptionRect(ref rect); } else if (attr.getName() == "roundcorner") { string[] listValue = attr.getValue().Split(','); if (listValue.Length < 2) { throw new Exception(""); } int cx = int.Parse(listValue[0]); int cy = int.Parse(listValue[1]); manager.setRoundCorner(cx, cy); } else if (attr.getName() == "mininfo") { string[] listValue = attr.getValue().Split(','); if (listValue.Length != 2) { throw new Exception(""); } int cx = int.Parse(listValue[0]); int cy = int.Parse(listValue[1]); manager.setMinMaxInfo(cx, cy); } else if (attr.getName() == "showdirty") { manager.setShowUpdateRect(attr.getValue() == "true"); } } } } } return(true); }
public Markup() { mRootNode = new MarkupNode(null); }
// 广度优先搜索XML节点树创建相应的控件树,过滤非控件节点 protected ControlUI parse(ref MarkupNode parentNode, ref ControlUI parentControl, ref PaintManagerUI manager) { if (parentNode == null) { throw new Exception("对象未赋值"); } ControlUI ctlParent = null; { //parentControl.setManager(manager, null); Queue <MarkupNode> queueNode = new Queue <MarkupNode>(); Queue <ControlUI> queueControl = new Queue <ControlUI>(); queueNode.Enqueue(parentNode); queueControl.Enqueue(parentControl); while (queueNode.Count > 0) { MarkupNode curNode = queueNode.Dequeue(); ControlUI curParentControl = queueControl.Dequeue(); List <MarkupNode> listNode = curNode.getChildList(); // 访问根节点 if (listNode != null && listNode.Count > 0) { // 子节点入队 foreach (var node in listNode) { // 过滤非控件节点 if (node.getName() == "Window" || node.getName() == "Image" || node.getName() == "Font" || node.getName() == "Default") { continue; } ControlUI newControl = null; { queueNode.Enqueue(node); // 创建控件,加入控件树后入队 newControl = getControl(node.getName()); if (newControl != null && newControl is ControlUI) { queueControl.Enqueue(newControl); newControl.setManager(manager, curParentControl); if (curParentControl != null) { IContainerUI container = (IContainerUI)curParentControl.getInterface("IContainer"); container.add(newControl); } else { if (ctlParent != null) { throw new Exception(""); } ctlParent = newControl; } } else if (mCallback != null) { newControl = mCallback.createControl(node.getName(), manager); if (newControl == null) { throw new Exception(""); } queueControl.Enqueue(newControl); newControl.setManager(manager, curParentControl); if (curParentControl != null) { IContainerUI container = (IContainerUI)curParentControl.getInterface("IContainer"); container.add(newControl); } else { if (ctlParent != null) { throw new Exception(""); } ctlParent = newControl; } } else { throw new Exception(""); } } { // 设置属性 if (manager != null) { //newControl.setManager(manager, curParentControl); string defaultAttributes = manager.getDefaultAttributeList(node.getName()); if (defaultAttributes != "") { newControl.applyAttributeList(defaultAttributes); } } List <XMLAttribute> listAttr = node.getAttributeList(); foreach (var attr in listAttr) { newControl.addAttribute(attr.getName(), attr.getValue()); newControl.setAttribute(attr.getName(), attr.getValue()); } } } } } } return(ctlParent); }