public ITermReader CreateReader(TermFactory termFactory) { Contract.Requires<ArgumentNullException>(termFactory != null); Contract.Requires<InvalidOperationException>(CanRead); Contract.Ensures(Contract.Result<ITermReader>() != null); return default(ITermReader); }
/// <summary> /// Initializes a new instance of the <see cref="ATermReader"/> class. /// </summary> /// <param name="termFactory">The term factory to use.</param> internal ATermReader(TermFactory termFactory) : this(termFactory, TermTextReader.DefaultCulture) { #region Contract Contract.Requires<ArgumentNullException>(termFactory != null); #endregion }
public virtual void Apply(AbstractNode abstractNode, SemanticDocument doc) { var type = abstractNode.GetType().GetTypeInfo(); var markedUpProps = type.DeclaredProperties .Select(propertyInfo => new { propertyInfo = propertyInfo, att = CustomAttributeExtensions.GetCustomAttribute <ExposeAttribute>((MemberInfo)propertyInfo), propertyUri = UriHelper.Combine(abstractNode.Uri, propertyInfo.Name) }).Where(x => x.att != null); foreach (var x in markedUpProps) { if (typeof(AbstractNode).GetTypeInfo().IsAssignableFrom(x.propertyInfo.PropertyType.GetTypeInfo())) { var childNode = (AbstractNode)x.propertyInfo.GetValue(abstractNode); doc.AddLink(Links.CreateLink(childNode.Title, childNode.Uri, childNode.Term)); } else { doc.Value.Add(SemanticProperty.CreateValue(TermFactory.From(x.propertyInfo), JToken.FromObject(x.propertyInfo.GetValue(abstractNode)))); } } }
/// <summary> /// Creates a reader for this format. /// </summary> /// <param name="termFactory">The term factory to use.</param> /// <returns>The created <see cref="ATermReader"/>.</returns> public ATermReader CreateReader(TermFactory termFactory) { #region Contract Contract.Requires<ArgumentNullException>(termFactory != null); Contract.Ensures(Contract.Result<ITermReader>() != null); #endregion return new ATermReader(termFactory); }
/// <summary> /// Initializes a new instance of the <see cref="ATermReader"/> class. /// </summary> /// <param name="termFactory">The term factory to use.</param> /// <param name="culture">The culture of the reader.</param> internal ATermReader(TermFactory termFactory, CultureInfo culture) : base(termFactory, culture) { #region Contract Contract.Requires<ArgumentNullException>(termFactory != null); Contract.Requires<ArgumentNullException>(culture != null); #endregion }
/// <summary> /// Initializes a new instance of the <see cref="TermTextReader"/> class. /// </summary> /// <param name="termFactory">The term factory to use.</param> /// <param name="culture">The culture of the reader.</param> protected TermTextReader(TermFactory termFactory, CultureInfo culture) { #region Contract Contract.Requires<ArgumentNullException>(termFactory != null); Contract.Requires<ArgumentNullException>(culture != null); #endregion this.TermFactory = termFactory; this.Culture = culture; }
private static void AddSemanticItemsFromMethodInfo(MethodInfoNode methodInfoNode, SemanticDocument representation) { var term = TermFactory.From(methodInfoNode.MethodInfo); var methodParameters = methodInfoNode.GetParameters(); var operation = Operation.Create(methodInfoNode.Title, methodParameters, methodInfoNode.Uri, term); representation.Value.Add(operation); representation.AddLink(Links.CreateLink(methodInfoNode.Parent.Title, methodInfoNode.Parent.Uri, Terms.Parent)); }
internal static SemanticProperty AddLink(this SemanticDocument doc, SemanticPropertiesSet link) { var linksRel = TermFactory.From <Links>(); var links = doc.Value[linksRel] ?? (SemanticProperty.CreateList(linksRel, new SemanticPropertiesList())).Then( sp => doc.Value.Add(sp)); var semanticPropertiesList = links.Value.AsT3; semanticPropertiesList.Add(link); return(links); }
public void Configuration(IAppBuilder app) { var representors = new Representor <SemanticDocument>[] { new Siren.SirenRepresentor(), new SirenServerSideHtmlRepresentor(), //new SirenReactHtmlRepresentor() }; var taskListAppRoot = BuildTaskListAppRoot(new Uri("/tasks", UriKind.Relative)); app.ExposeRootNodeAsHypermediaApi(taskListAppRoot, LocateAdaptors, representors); var chessAppRoot = new ChessEngineApp("ChessEngine", new Uri("/chess", UriKind.Relative), TermFactory.From <ChessEngineApp>()); app.ExposeRootNodeAsHypermediaApi(chessAppRoot, LocateAdaptors, representors); var indexAppRoot = new IndexRootNode("Index", new Uri("/", UriKind.Relative)); indexAppRoot.AddLink("chess engine app", chessAppRoot.Uri, TermFactory.From <ChessEngineApp>()); indexAppRoot.AddLink("task list app", taskListAppRoot.Uri, TermFactory.From <TaskListAppRoot>()); app.ExposeRootNodeAsHypermediaApi(indexAppRoot, LocateAdaptors, representors); }
public static SemanticProperty <Term> CreateTerm <TTerm, TValueTerm>() { return(CreateTerm(TermFactory.From <TTerm>(), TermFactory.From <TValueTerm>())); }
public static SemanticProperty <SemanticPropertiesList> CreateList <TTerm>(SemanticPropertiesList value) { return(CreateList(TermFactory.From <TTerm>(), value)); }
public static SemanticProperty <JToken> CreateValue <T>(JToken value) { return(CreateValue(TermFactory.From <T>(), value)); }
public static SemanticProperty <Term> CreateTerm <TTerm>(Term value) { return(CreateTerm(TermFactory.From <TTerm>(), value)); }
internal static MetaConditions Parse(IReadOnlyCollection <IUriCondition> uriMetaConditions, IEntityResource resource, TermFactory termFactory) { if (uriMetaConditions?.Any() != true) { return(null); } var renames = uriMetaConditions.Where(c => c.Key.EqualsNoCase("rename")); var others = uriMetaConditions.Where(c => !c.Key.EqualsNoCase("rename")); var metaConditions = new MetaConditions { Empty = false }; ICollection <string> dynamicDomain = default; void make(IEnumerable <IUriCondition> conditions) { foreach (var condition in conditions) { var(key, op, valueLiteral) = (condition.Key, condition.Operator, condition.ValueLiteral); if (op != EQUALS) { throw new InvalidSyntax(InvalidMetaConditionOperator, "Invalid operator for meta-condition. One and only one '=' is allowed"); } if (!Enum.TryParse(key, true, out RESTableMetaCondition metaCondition)) { throw new InvalidSyntax(InvalidMetaConditionKey, $"Invalid meta-condition '{key}'. Available meta-conditions: " + $"{string.Join(", ", EnumMember<RESTableMetaCondition>.Names)}"); } var expectedType = metaCondition.GetExpectedType(); if (valueLiteral == "∞") { valueLiteral = int.MaxValue.ToString(); } else if (valueLiteral == "-∞") { valueLiteral = int.MinValue.ToString(); } var(first, length) = (valueLiteral.FirstOrDefault(), valueLiteral.Length); switch (first) { case '\'': case '\"': if (length > 1 && valueLiteral[length - 1] == first) { valueLiteral = valueLiteral.Substring(1, length - 2); } break; } object value; try { value = Convert.ChangeType(valueLiteral, expectedType); } catch { throw new InvalidSyntax(InvalidMetaConditionValueType, $"Invalid data type for value '{valueLiteral}' assigned to meta-condition '{key}'. Expected {GetTypeString(expectedType)}."); } switch (metaCondition) { case RESTableMetaCondition.Unsafe: metaConditions.Unsafe = (bool)value; break; case RESTableMetaCondition.Limit: metaConditions.Limit = (int)value; break; case RESTableMetaCondition.Offset: metaConditions.Offset = (int)value; break; case RESTableMetaCondition.Order_asc: { var term = termFactory.MakeOutputTerm(resource, (string)value, dynamicDomain); metaConditions.OrderBy = new OrderByAscending(resource, term); break; } case RESTableMetaCondition.Order_desc: { var term = termFactory.MakeOutputTerm(resource, (string)value, dynamicDomain); metaConditions.OrderBy = new OrderByDescending(resource, term); break; } case RESTableMetaCondition.Select: { var selectKeys = (string)value; var domain = dynamicDomain; var terms = selectKeys .Split(',') .Distinct() .Select(selectKey => termFactory.MakeOutputTerm(resource, selectKey, domain)); metaConditions.Select = new Select(terms); break; } case RESTableMetaCondition.Add: { var addKeys = (string)value; var domain = dynamicDomain; var terms = addKeys .ToLower() .Split(',') .Distinct() .Select(addKey => termFactory.MakeOutputTerm(resource, addKey, domain)); metaConditions.Add = new Add(terms); break; } case RESTableMetaCondition.Rename: { var renameKeys = (string)value; var terms = renameKeys.Split(',').Select(keyString => { var(termKey, newName) = keyString.TSplit(renameKeys.Contains("->") ? "->" : "-%3E"); return ( termFactory.MakeOutputTerm(resource, termKey.ToLowerInvariant(), null), newName ); }); metaConditions.Rename = new Rename(terms, out dynamicDomain); break; } case RESTableMetaCondition.Distinct: if ((bool)value) { metaConditions.Distinct = new Distinct(); } break; case RESTableMetaCondition.Search: metaConditions.Search = new Search((string)value); break; case RESTableMetaCondition.Search_regex: metaConditions.Search = new RegexSearch((string)value); break; case RESTableMetaCondition.Safepost: metaConditions.SafePost = (string)value; break; default: throw new ArgumentOutOfRangeException(); } } } make(renames); make(others); metaConditions.Processors = new IProcessor[] { metaConditions.Add, metaConditions.Rename, metaConditions.Select }.Where(p => p != null).ToArray(); metaConditions.HasProcessors = metaConditions.Processors.Any(); metaConditions.CanUseExternalCounter = metaConditions.Search == null && metaConditions.Distinct == null && metaConditions.Limit.Number == -1 && metaConditions.Offset.Number == 0; if (metaConditions.OrderBy != null) { if (metaConditions.Rename?.Any(p => p.Key.Key.EqualsNoCase(metaConditions.OrderBy.Key)) == true && !metaConditions.Rename.Any(p => p.Value.EqualsNoCase(metaConditions.OrderBy.Key))) { throw new InvalidSyntax(InvalidMetaConditionSyntax, $"The {(metaConditions.OrderBy is OrderByAscending ? RESTableMetaCondition.Order_asc : RESTableMetaCondition.Order_desc)} " + "meta-condition cannot refer to a property x that is to be renamed " + "unless some other property is renamed to x"); } } if (metaConditions.Select != null && metaConditions.Rename != null) { if (metaConditions.Select.Any(pc => metaConditions.Rename.Any(p => p.Key.Key.EqualsNoCase(pc.Key)) && !metaConditions.Rename.Any(p => p.Value.EqualsNoCase(pc.Key)))) { throw new InvalidSyntax(InvalidMetaConditionSyntax, "A 'Select' meta-condition cannot refer to a property x that is " + "to be renamed unless some other property is renamed to x. Use the " + "new property name instead."); } } return(metaConditions); }
public static SemanticProperty <SemanticPropertiesSet> CreateSet <TTerm>(SemanticPropertiesSet value = null) { return(CreateSet(TermFactory.From <TTerm>(), value)); }
public ConditionRedirector(ResourceCollection resourceCollection, TermFactory termFactory, TypeCache typeCache) { ResourceCollection = resourceCollection; TermFactory = termFactory; TypeCache = typeCache; }
public GlossaryNode(AbstractNode parent) { this.parent = parent; this.terms.Add(TermFactory.From <GlossaryNode>(), new TermNode(this, TermFactory.From <GlossaryNode>())); }
public Card(Board parent, UrlPart urlPart, string title) : base(parent, urlPart, title, TermFactory.From <Card>()) { }
public Task(Card parent, UrlPart urlPart, string title) : base(parent, urlPart, title, TermFactory.From <Task>()) { }
public Boards(TaskListAppRoot parent, UrlPart urlPart) : base(parent, urlPart, nameof(Boards), TermFactory.From <Boards>()) { }
public Authentication(TaskListAppRoot parent, UrlPart urlPart) : base(parent, urlPart, nameof(Authentication), TermFactory.From <Authentication>()) { }
/// <inheritdoc /> ITermReader ITermFormat.CreateReader(TermFactory termFactory) { // CONTRACT: Inherited from ITermFormat return this.CreateReader(termFactory); }
public static SemanticProperty Create <T>(OneOf <SemanticPropertiesSet, JToken, Term, SemanticPropertiesList> value) { return(new SemanticProperty(TermFactory.From <T>(), value)); }
public Board(Boards parent, string key, string title) : base(parent, key, title, TermFactory.From <Board>()) { }
public ChessGameNode(ChessGame chessGame, ChessEngineApp parent, UrlPart gameId) : base(parent, gameId, "Game " + gameId, TermFactory.From <ChessGameNode>()) { _chessGame = chessGame; }
public TaskListAppRoot(Uri uri) : base("Task Lists App", uri, TermFactory.From <TaskListAppRoot>()) { this.Authentication = (new Authentication(this, nameof(Authentication))); this.Boards = (new Boards(this, nameof(Boards))); }
public MethodParameter[] GetParameters() { var parameterInfo = this.MethodInfo.GetParameters() .Where(mi => mi.GetCustomAttribute <InjectAttribute>() == null) .Select(pi => new MethodParameter(pi.Name, GetMethodParameterType(pi), TermFactory.From(pi))).ToArray(); return(parameterInfo.ToArray()); }
public SemanticDocument() : base(TermFactory.From <SemanticDocument>(), new SemanticPropertiesSet()) { }
public IndexRootNode(string title, Uri uri) : base(title, uri, TermFactory.From <IndexRootNode>()) { }
public SirenDotNet.Entity BuildFromSemanticDocument(SemanticDocument semanticDocument, FindUriForTerm uriTermFinder) { var links = new List <SirenDotNet.Link>(); var actions = new List <Action>(); var subEntities = new List <SubEntity>(); var jo = new JObject(); string sirenTitle = null; foreach (var property in semanticDocument.Value) { property.Value.Switch(set => { if (property.Term.Means(TermFactory.From <Operation>())) { Func <SemanticProperty, Field> buildField = mp => { var fieldSet = mp.Value.AsT0; var fieldKind = fieldSet[TermFactory.From <Operation.FieldKind>()].Value.AsT2; var field = new Field() { Name = fieldSet[TermFactory.From <FieldName>()].Value.AsT1.Value <string>(), }; if (fieldKind == TermFactory.From <Operation.TextField>()) { field.Type = SirenDotNet.FieldTypes.Text; } else if (fieldKind == TermFactory.From <Operation.PasswordField>()) { field.Type = SirenDotNet.FieldTypes.Password; } else if (fieldKind == TermFactory.From <Operation.SelectField>()) { field.Type = new FieldTypes("select"); var options = fieldSet[TermFactory.From <Operation.Options>()].Value.AsT3; field.ExtensionData["options"] = options.Select(o => o.AsT0) .Select( o => new { name = o[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(), value = o[TermFactory.From <Operation.FieldValue>()].Value.AsT1.Value <string>() }) .Select(JObject.FromObject).ToArray(); } return(field); }; ; var href = new Uri(set[TermFactory.From <Operation.ActionUrl>()].Value.AsT1.Value <string>(), UriKind.Relative); var action = new SirenDotNet.Action(uriTermFinder(property.Term).ToString(), href) { Method = HttpVerbs.POST, Title = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(), Fields = set[TermFactory.From <Operation.Fields>()].Value.AsT0.Select(x => buildField(x)).ToArray() }; actions.Add(action); } }, value => { var isTitle = property.Term == Terms.Title; if (isTitle) { sirenTitle = value.ToObject <string>(); } else { jo[uriTermFinder(property.Term).ToString()] = value.ToString(); } }, term => { }, list => { if (property.Term == TermFactory.From <Vocab.Links>()) { foreach (var value in list) { var set = value.AsT0; var displayName = set[TermFactory.From <DisplayText>()].Value.AsT1.Value <string>(); Term rel = set[TermFactory.From <Vocab.Links.Rel>()].Value.AsT2; var href = new Uri(set[TermFactory.From <Vocab.Links.Href>()].Value.AsT1.Value <string>(), UriKind.Relative); var action = new SirenDotNet.Link(href, uriTermFinder(rel).ToString()) { Title = displayName }; links.Add(action); } } }); } if (sirenTitle == null) { throw new Exception("Title cannot be null for siren, attach a Term.Title property"); } var entity = new SirenDotNet.Entity { Title = sirenTitle, //Class = semanticDocument.Class?.Select(c => c.ToString()), Links = links.Any() ? links : null, Actions = actions.Any() ? actions : null, Entities = subEntities.Any() ? subEntities : null, Properties = jo.HasValues ? jo : null }; return(entity); }