예제 #1
0
 private static LinkAttribute ParseAttribute(Scanner scanner)
 {
     String name = scanner.Find(WordRegex);
     if (name == null)
         return null;
     else
     {
         Object value = null;
         // check for name-value-pair
         if (scanner.Find(new Regex("="), 1) == null)
             // flag attribute
             value = true;
         else
         {
             String s = null;
             if ((s = scanner.Find(QuotedString)) != null)
                 // trim " "
                 value = s.Substring(1, s.Length - 2);
             else if ((s = scanner.Find(Cardinal)) != null)
                 value = Int32.Parse(s);
             // TODO what if both pattern failed?
         }
         return new LinkAttribute(name, value);
     }
 }
예제 #2
0
        public static IEnumerable<WebLink> Parse(String linkFormat)
        {
            if (!String.IsNullOrEmpty(linkFormat))
            {
                Scanner scanner = new Scanner(linkFormat);
                String path = null;
                while ((path = scanner.Find(ResourceNameRegex)) != null)
                {
                    path = path.Substring(1, path.Length - 2);
                    WebLink link = new WebLink(path);

                    String attr = null;
                    while (scanner.Find(DelimiterRegex, 1) == null &&
                        (attr = scanner.Find(WordRegex)) != null)
                    {
                        if (scanner.Find(EqualRegex, 1) == null)
                        {
                            // flag attribute without value
                            link.Attributes.Add(attr);
                        }
                        else
                        {
                            String value = null;
                            if ((value = scanner.Find(QuotedString)) != null)
                            {
                                // trim " "
                                value = value.Substring(1, value.Length - 2);
                                if (Title.Equals(attr))
                                    link.Attributes.Add(attr, value);
                                else
                                    foreach (String part in BlankRegex.Split(value))
                                        link.Attributes.Add(attr, part);
                            }
                            else if ((value = scanner.Find(WordRegex)) != null)
                            {
                                link.Attributes.Set(attr, value);
                            }
                            else if ((value = scanner.Find(Cardinal)) != null)
                            {
                                link.Attributes.Set(attr, value);
                            }
                        }
                    }

                    yield return link;
                }
            }

            yield break;
        }
예제 #3
0
        public static RemoteResource Deserialize(String linkFormat)
        {
            RemoteResource root = new RemoteResource(String.Empty);
            Scanner scanner = new Scanner(linkFormat);

            String path = null;
            while ((path = scanner.Find(ResourceNameRegex)) != null)
            {
                path = path.Substring(1, path.Length - 2);

                // Retrieve specified resource, create if necessary
                RemoteResource resource = new RemoteResource(path);

                LinkAttribute attr = null;
                while (scanner.Find(DelimiterRegex, 1) == null && (attr = ParseAttribute(scanner)) != null)
                {
                    AddAttribute(resource.Attributes, attr);
                }

                root.AddSubResource(resource);
            }

            return root;
        }