コード例 #1
0
 public AttributeInfo(string name, string value, AttributeOrderRule orderRule)
 {
     this.Name = name;
     this.Value = value;
     this.IsMarkupExtension = MarkupExtensionPattern.IsMatch(value);
     this.attributeOrderRule = orderRule;
 }
コード例 #2
0
        public AttributeOrderRule GetRuleFor(string attributeName)
        {
            AttributeOrderRule result = null;

            if (internalDictionary.Keys.Contains(attributeName))
            {
                result = internalDictionary[attributeName];
            }
            else
            {
                AttributeTokenInfoEnum tempAttributeTokenType = AttributeTokenInfoEnum.Other;

                if (attributeName.StartsWith("xmlns"))
                {
                    tempAttributeTokenType = AttributeTokenInfoEnum.OtherNamespace;
                }
                else
                {
                    tempAttributeTokenType = AttributeTokenInfoEnum.Other;
                }

                result = new AttributeOrderRule()
                {
                    AttributeTokenType = tempAttributeTokenType,
                    Priority = 0
                };
            }

            return result;
        }
コード例 #3
0
        public AttributeInfo Create(XmlReader xmlReader)
        {
            string             attributeName   = xmlReader.Name;
            string             attributeValue  = xmlReader.Value;
            AttributeOrderRule orderRule       = this.orderRules.GetRuleFor(attributeName);
            MarkupExtension    markupExtension = this.ParseMarkupExtension(attributeValue);

            return(new AttributeInfo(attributeName, attributeValue, orderRule, markupExtension));
        }
コード例 #4
0
        public AttributeInfo Create(XmlReader xmlReader)
        {
            string attributeName  = xmlReader.Name;
            string attributeValue = xmlReader.Value;

            var attributeNameWithoutNamespace = String.Empty;
            var attributeHasIgnoredNamespace  = this.ignoreDesignTimeReferencePrefix && this.CheckIfAttributeHasIgnoredNamespace(attributeName, out attributeNameWithoutNamespace);

            AttributeOrderRule orderRule       = this.orderRules.GetRuleFor(attributeHasIgnoredNamespace ? attributeNameWithoutNamespace : attributeName);
            MarkupExtension    markupExtension = this.ParseMarkupExtension(attributeValue);

            return(new AttributeInfo(attributeName, attributeValue, attributeHasIgnoredNamespace, attributeNameWithoutNamespace, orderRule, markupExtension));
        }
コード例 #5
0
ファイル: StylerService.cs プロジェクト: hxhlb/XamlStyler
        private void ProcessElement(XmlReader xmlReader, ref string output)
        {
            string currentIndentString = GetIndentString(xmlReader.Depth);
            string elementName         = xmlReader.Name;

            if ("Run".Equals(elementName))
            {
                if (output.EndsWith("\n"))
                {
                    // Shall not add extra whitespaces (including linefeeds) before <Run/>,
                    // because it will affect the rendering of <TextBlock><Run/><Run/></TextBlock>
                    output += currentIndentString + "<" + xmlReader.Name;
                }
                else
                {
                    output += "<" + xmlReader.Name;
                }
            }
            else if (output.Length == 0 || output.EndsWith("\n"))
            {
                output += currentIndentString + "<" + xmlReader.Name;
            }
            else
            {
                output += Environment.NewLine + currentIndentString + "<" + xmlReader.Name;
            }

            bool isEmptyElement = xmlReader.IsEmptyElement;
            bool hasPutEndingBracketOnNewLine = false;
            var  list = new List <AttributeInfo>(xmlReader.AttributeCount);

            if (xmlReader.HasAttributes)
            {
                while (xmlReader.MoveToNextAttribute())
                {
                    string             attributeName  = xmlReader.Name;
                    string             attributeValue = xmlReader.Value;
                    AttributeOrderRule orderRule      = OrderRules.GetRuleFor(attributeName);
                    list.Add(new AttributeInfo(attributeName, attributeValue, orderRule));
                }

                if (Options.OrderAttributesByName)
                {
                    list.Sort();
                }

                currentIndentString = GetIndentString(xmlReader.Depth);

                var noLineBreakInAttributes = (list.Count <= Options.AttributesTolerance) || IsNoLineBreakElement(elementName);
                // Root element?
                if (_elementProcessStatusStack.Count == 2)
                {
                    switch (Options.RootElementLineBreakRule)
                    {
                    case LineBreakRule.Default:
                        break;

                    case LineBreakRule.Always:
                        noLineBreakInAttributes = false;
                        break;

                    case LineBreakRule.Never:
                        noLineBreakInAttributes = true;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                // No need to break attributes
                if (noLineBreakInAttributes)
                {
                    output = list.Select(attrInfo => attrInfo.ToSingleLineString()).Aggregate(output, (current, pendingAppend) => current + String.Format(" {0}", pendingAppend));

                    _elementProcessStatusStack.Peek().IsMultlineStartTag = false;
                }

                // Need to break attributes
                else
                {
                    IList <String> attributeLines = new List <String>();

                    var currentLineBuffer = new StringBuilder();
                    int attributeCountInCurrentLineBuffer = 0;

                    AttributeInfo lastAttributeInfo = null;
                    foreach (AttributeInfo attrInfo in list)
                    {
                        // Attributes with markup extension, always put on new line
                        if (attrInfo.IsMarkupExtension && Options.FormatMarkupExtension)
                        {
                            string baseIndetationString = GetIndentString(xmlReader.Depth - 1) +
                                                          String.Empty.PadLeft(elementName.Length + 2, ' ');
                            string pendingAppend = attrInfo.ToMultiLineString(baseIndetationString);

                            if (currentLineBuffer.Length > 0)
                            {
                                attributeLines.Add(currentLineBuffer.ToString());
                                currentLineBuffer.Length          = 0;
                                attributeCountInCurrentLineBuffer = 0;
                            }

                            attributeLines.Add(pendingAppend);
                        }
                        else
                        {
                            string pendingAppend = attrInfo.ToSingleLineString();

                            bool isAttributeCharLengthExceeded =
                                (attributeCountInCurrentLineBuffer > 0 && Options.MaxAttributeCharatersPerLine > 0
                                 &&
                                 currentLineBuffer.Length + pendingAppend.Length > Options.MaxAttributeCharatersPerLine);

                            bool isAttributeCountExceeded =
                                (Options.MaxAttributesPerLine > 0 &&
                                 attributeCountInCurrentLineBuffer + 1 > Options.MaxAttributesPerLine);

                            bool isAttributeRuleGroupChanged = Options.PutAttributeOrderRuleGroupsOnSeparateLines &&
                                                               lastAttributeInfo != null &&
                                                               lastAttributeInfo.OrderRule.AttributeTokenType != attrInfo.OrderRule.AttributeTokenType;

                            if (isAttributeCharLengthExceeded || isAttributeCountExceeded || isAttributeRuleGroupChanged)
                            {
                                attributeLines.Add(currentLineBuffer.ToString());
                                currentLineBuffer.Length          = 0;
                                attributeCountInCurrentLineBuffer = 0;
                            }

                            currentLineBuffer.AppendFormat("{0} ", pendingAppend);
                            attributeCountInCurrentLineBuffer++;
                        }

                        lastAttributeInfo = attrInfo;
                    }

                    if (currentLineBuffer.Length > 0)
                    {
                        attributeLines.Add(currentLineBuffer.ToString());
                    }

                    for (int i = 0; i < attributeLines.Count; i++)
                    {
                        if (0 == i && Options.KeepFirstAttributeOnSameLine)
                        {
                            output += String.Format(" {0}", attributeLines[i].Trim());

                            // Align subsequent attributes with first attribute
                            currentIndentString = GetIndentString(xmlReader.Depth - 1) +
                                                  String.Empty.PadLeft(elementName.Length + 2, ' ');
                            continue;
                        }
                        output += Environment.NewLine + currentIndentString + attributeLines[i].Trim();
                    }

                    _elementProcessStatusStack.Peek().IsMultlineStartTag = true;
                }

                // Determine if to put ending bracket on new line
                if (Options.PutEndingBracketOnNewLine &&
                    _elementProcessStatusStack.Peek().IsMultlineStartTag)
                {
                    output += Environment.NewLine + currentIndentString;
                    hasPutEndingBracketOnNewLine = true;
                }
            }

            if (isEmptyElement)
            {
                if (hasPutEndingBracketOnNewLine)
                {
                    output += "/>";
                }
                else
                {
                    output += " />";
                }

                _elementProcessStatusStack.Peek().IsSelfClosingElement = true;
            }
            else
            {
                output += ">";
            }
        }
コード例 #6
0
ファイル: StylerService.cs プロジェクト: munyabe/XamlStyler
        private void ProcessElement(XmlReader xmlReader, StringBuilder output)
        {
            string currentIndentString = GetIndentString(xmlReader.Depth);
            string elementName         = xmlReader.Name;

            // Calculate how element should be indented
            if (!_elementProcessStatusStack.Peek().IsPreservingSpace)
            {
                // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure!
                if (elementName.Equals("Run"))
                {
                    _elementProcessStatusStack.Peek().Parent.IsSignificantWhiteSpace = true;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                }
                else
                {
                    _elementProcessStatusStack.Peek().Parent.IsSignificantWhiteSpace = false;
                    if (output.Length == 0 || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                    else
                    {
                        output
                        .Append(Environment.NewLine)
                        .Append(currentIndentString);
                    }
                }
            }

            // Output the element itself
            output
            .Append('<')
            .Append(xmlReader.Name);

            bool isEmptyElement = xmlReader.IsEmptyElement;
            bool hasPutEndingBracketOnNewLine = false;
            var  list = new List <AttributeInfo>(xmlReader.AttributeCount);

            if (xmlReader.HasAttributes)
            {
                while (xmlReader.MoveToNextAttribute())
                {
                    string             attributeName  = xmlReader.Name;
                    string             attributeValue = xmlReader.Value;
                    AttributeOrderRule orderRule      = OrderRules.GetRuleFor(attributeName);
                    list.Add(new AttributeInfo(attributeName, attributeValue, orderRule));

                    // Check for xml:space as defined in http://www.w3.org/TR/2008/REC-xml-20081126/#sec-white-space
                    if (xmlReader.IsXmlSpaceAttribute())
                    {
                        _elementProcessStatusStack.Peek().IsPreservingSpace = (xmlReader.Value == "preserve");
                    }
                }

                if (Options.EnableAttributeReordering)
                {
                    list.Sort(AttributeInfoComparison);
                }

                currentIndentString = GetIndentString(xmlReader.Depth);

                var noLineBreakInAttributes    = (list.Count <= Options.AttributesTolerance) || IsNoLineBreakElement(elementName);
                var forceLineBreakInAttributes = false;

                // Root element?
                if (_elementProcessStatusStack.Count == 2)
                {
                    switch (Options.RootElementLineBreakRule)
                    {
                    case LineBreakRule.Default:
                        break;

                    case LineBreakRule.Always:
                        noLineBreakInAttributes    = false;
                        forceLineBreakInAttributes = true;
                        break;

                    case LineBreakRule.Never:
                        noLineBreakInAttributes = true;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                // No need to break attributes
                if (noLineBreakInAttributes)
                {
                    foreach (var attrInfo in list)
                    {
                        output
                        .Append(' ')
                        .Append(attrInfo.ToSingleLineString());
                    }

                    _elementProcessStatusStack.Peek().IsMultlineStartTag = false;
                }

                // Need to break attributes
                else
                {
                    IList <String> attributeLines = new List <String>();

                    var currentLineBuffer = new StringBuilder();
                    int attributeCountInCurrentLineBuffer = 0;

                    AttributeInfo lastAttributeInfo = null;
                    foreach (AttributeInfo attrInfo in list)
                    {
                        // Attributes with markup extension, always put on new line
                        if (attrInfo.IsMarkupExtension && Options.FormatMarkupExtension)
                        {
                            string baseIndetationString;

                            if (!Options.KeepFirstAttributeOnSameLine)
                            {
                                baseIndetationString = GetIndentString(xmlReader.Depth);
                            }
                            else
                            {
                                baseIndetationString = GetIndentString(xmlReader.Depth - 1) +
                                                       string.Empty.PadLeft(elementName.Length + 2, ' ');
                            }

                            string pendingAppend;

                            if (NoNewLineMarkupExtensionsList.Contains(attrInfo.MarkupExtension))
                            {
                                pendingAppend = " " + attrInfo.ToSingleLineString();
                            }
                            else
                            {
                                pendingAppend = attrInfo.ToMultiLineString(baseIndetationString);
                            }

                            if (currentLineBuffer.Length > 0)
                            {
                                attributeLines.Add(currentLineBuffer.ToString());
                                currentLineBuffer.Length          = 0;
                                attributeCountInCurrentLineBuffer = 0;
                            }

                            attributeLines.Add(pendingAppend);
                        }
                        else
                        {
                            string pendingAppend = attrInfo.ToSingleLineString();

                            bool isAttributeCharLengthExceeded =
                                (attributeCountInCurrentLineBuffer > 0 && Options.MaxAttributeCharatersPerLine > 0
                                 &&
                                 currentLineBuffer.Length + pendingAppend.Length > Options.MaxAttributeCharatersPerLine);

                            bool isAttributeCountExceeded =
                                (Options.MaxAttributesPerLine > 0 &&
                                 attributeCountInCurrentLineBuffer + 1 > Options.MaxAttributesPerLine);

                            bool isAttributeRuleGroupChanged = Options.PutAttributeOrderRuleGroupsOnSeparateLines &&
                                                               lastAttributeInfo != null &&
                                                               lastAttributeInfo.OrderRule.Group != attrInfo.OrderRule.Group;

                            if (currentLineBuffer.Length > 0 && (forceLineBreakInAttributes || isAttributeCharLengthExceeded || isAttributeCountExceeded || isAttributeRuleGroupChanged))
                            {
                                attributeLines.Add(currentLineBuffer.ToString());
                                currentLineBuffer.Length          = 0;
                                attributeCountInCurrentLineBuffer = 0;
                            }

                            currentLineBuffer.AppendFormat("{0} ", pendingAppend);
                            attributeCountInCurrentLineBuffer++;
                        }

                        lastAttributeInfo = attrInfo;
                    }

                    if (currentLineBuffer.Length > 0)
                    {
                        attributeLines.Add(currentLineBuffer.ToString());
                    }

                    for (int i = 0; i < attributeLines.Count; i++)
                    {
                        if (0 == i && Options.KeepFirstAttributeOnSameLine)
                        {
                            output
                            .Append(' ')
                            .Append(attributeLines[i].Trim());

                            // Align subsequent attributes with first attribute
                            currentIndentString = GetIndentString(xmlReader.Depth - 1) +
                                                  String.Empty.PadLeft(elementName.Length + 2, ' ');
                            continue;
                        }
                        output
                        .Append(Environment.NewLine)
                        .Append(currentIndentString)
                        .Append(attributeLines[i].Trim());
                    }

                    _elementProcessStatusStack.Peek().IsMultlineStartTag = true;
                }

                // Determine if to put ending bracket on new line
                if (Options.PutEndingBracketOnNewLine &&
                    _elementProcessStatusStack.Peek().IsMultlineStartTag)
                {
                    output
                    .Append(Environment.NewLine)
                    .Append(currentIndentString);
                    hasPutEndingBracketOnNewLine = true;
                }
            }

            if (isEmptyElement)
            {
                if (hasPutEndingBracketOnNewLine == false && Options.SpaceBeforeClosingSlash)
                {
                    output.Append(' ');
                }
                output.Append("/>");

                _elementProcessStatusStack.Peek().IsSelfClosingElement = true;
            }
            else
            {
                output.Append(">");
            }
        }
コード例 #7
0
        private AttributeOrderRules Populate(string option, AttributeTokenInfoEnum tokenType)
        {
            if (!String.IsNullOrWhiteSpace(option))
            {
                int priority = 1;

                string[] attributeNames = option.Split(',')
                    .Where<string>(x => !String.IsNullOrWhiteSpace(x))
                    .Select<string, string>(x => x.Trim())
                    .ToArray<string>();

                foreach (string attributeName in attributeNames)
                {
                    internalDictionary[attributeName] = new AttributeOrderRule()
                        {
                            AttributeTokenType = tokenType,
                            Priority = priority
                        };

                    priority++;
                }
            }

            return this;
        }