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); } }
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; }
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; }