Exemplo n.º 1
0
      /// <summary>Initializes a new instance of the <see cref="BplProperty"/> class.</summary>
      public MetaProperty(BplProperty bplProperty) {
         // Fill class level attributes
         this.Name = bplProperty.Name;
         this.CanonicName = bplProperty.CanonicName;
         this.IsScalar = !(bplProperty.IsArray || bplProperty.IsCollection);
         this.IsArray = bplProperty.IsArray;
         this.IsCollection = bplProperty.IsCollection;
         this.IsPrimitive = bplProperty.IsPrimitive;
         if (this.IsArray || this.IsCollection) {
            this.DataType = Repository.ResolveType(bplProperty.UnderlyingItemType.FullName);
         } else {
            this.DataType = Repository.ResolveType(bplProperty.UnderlyingType.FullName);
         }

         if (this.DataType.Contains("[[")) {
            this.DataType = bplProperty.IsNullable ? string.Format("{0}?", bplProperty.UnderlyingItemType.FullName) : bplProperty.UnderlyingItemType.FullName;
         }

         this.IsReadOnly = bplProperty.IsCalculated;

         var documentation = BplLanguage.Documentation[bplProperty].Content;
         if (documentation != null) {
            this.Documentation = new Documentation(documentation);
         }

         // Register primitive type
         /*****************************/
         if (bplProperty.IsPrimitive) {
            if (bplProperty.PrimitiveType.IsEnum) {
               MetaEnum me = new MetaEnum(bplProperty.PrimitiveType);
            } else {
               if (this.IsScalar) {
                  if (!Repository.TypesMapper.ContainsKey(bplProperty.UnderlyingType.FullName)) {
                     MetaPrimitive mp = new MetaPrimitive(bplProperty.PrimitiveType);
                  }
               } else {
                  if (!Repository.TypesMapper.ContainsKey(bplProperty.UnderlyingItemType.FullName)) {
                     MetaPrimitive mp = new MetaPrimitive(bplProperty.PrimitiveType.ItemType);
                  }
               }
            }
         }
         /*****************************/
      }
Exemplo n.º 2
0
      /// <summary>Validates the target object and if there are no errors converts it to a string reference. The target object must 
      /// have a valid and non-empty identity property, and must be contained at some level under the root object.</summary>
      public string ToReference(BplObject target, BplProperty property) {
         var targetEntity = target as BplEntity;
         if (target == null) {
            BplRuntimeException.Throw("Association target is invalid or missing.");
         }
         
         var targetId = targetEntity.Id;
         if (targetId.IsEmpty) {
            BplRuntimeException.Throw("Association target {0} has no Id.", target);
         }

         bool? flag = null;
         if (CustomVerifier != null) {
            flag = CustomVerifier(target);
         }
         if (!flag.HasValue) {
            flag = (target.IsA<BplTaxonomy>() || _inScope(target));
         }
         if (!flag.Value) {
            BplRuntimeException.Throw("Association target {0} is out of serialization scope. It must be either contained in {1} or be a taxonomy.".Substitute(target, Root));
         }
         return targetId.ToString(property.ReferencedClass.IdentityScope);
      }
Exemplo n.º 3
0
 private void _deserializeContainer(BplObject bplObject, BplProperty property, ArrayList children) {
    var collection = property.GetValue(bplObject) as IBplCollection;
    _errorDepth++;
    for (var i = 0; i < children.Count; i++) {
       if (PreserveErrorsInfo) _updateErrorInfo(i);
       var childObject = _deserializeObject(children[i] as Hashtable, property.ReferencedClass);
       if (childObject != null) {
          collection.Add(childObject);
       }
    }
    _errorDepth--;
 }
Exemplo n.º 4
0
      /// <summary>Dettaches this node from the specified source object.</summary>
      internal void DetachFrom(BplObject source, BplProperty property) {
         if (source == null || property == null) {
            BplRuntimeException.Throw("Source object or property are missing");
         }
         if (source.IsSealed) {
            BplRuntimeException.Throw("Node cannot be detached from a sealed object");
         }

         var isEntity = (this is BplEntity);
         var isTaxonomy = (this is BplTaxonomy);
         if ((source is BplTaxonomy) && !isTaxonomy) {
            BplRuntimeException.Throw("'{0}' cannot be referenced from a taxonomy", this);
         }

         if (property.IsContainer) {

            // property is a container, so we need to reparent the node

            if (Parent != source || ParentContainer != property) {
               // this happens when the node has been attached to another container
               return;
            } else {
               // this happens when the node has been truly detached
               Parent = null;
               ParentContainer = null;
            }

         } else {
            // property is an assocation, so we need to update the backward associations

            if (!isEntity) {
               // association target must be an entity
               BplRuntimeException.Throw("'{0}' cannot be the target of association since it is not an entity", this);
            }

            //if (!isTaxonomy) {
               // no backward association from taxonomies
               Sources.RemoveTuple(source, property);
            //}

         }

         // detach the node and all its dependents from this context
         if (Context != null) {
            _detachFromContext(this);
         }

      }
Exemplo n.º 5
0
 /// <summary>Creates a new <see cref="BplRuntimeException"/> instance.</summary>
 private BplRuntimeException(String message, Exception inner, BplObject sourceObject, BplProperty sourceProperty)
    : base(message, inner) {
    SourceObject = sourceObject;
    SourceProperty = sourceProperty;
 }
Exemplo n.º 6
0
 private void _deserializeAssociation(BplObject bplObject, BplProperty property, XAttribute target) {
    _resolver.AddSource(target.Value, bplObject, property);
 }
Exemplo n.º 7
0
 private void _deserializeContainer(BplObject bplObject, BplProperty property, XElement child) {
    var childObject = _deserializeObject(child);
    if (childObject != null) {
       property.SetValue(bplObject, childObject);
    }
 }
Exemplo n.º 8
0
 private void _deserializeArray(BplObject bplObject, BplProperty property, XAttribute attr) {
    try {
       var value = BplLanguage.XmlConverters[property].Parse(attr.Value);
       property.SetValue(bplObject, value);
    } catch (Exception e) {
       AddError("Invalid value in BPL property '{:name}', {:where}. {0}", e.Message, attr);
    }
 }
Exemplo n.º 9
0
 private object _parseScalar(BplProperty property, IBplXmlConverter converter, XObject node) {
    if (property.IsWeakAssociation) {
       var idscope = property.ReferencedClass.IdentityScope;
       if (node is XAttribute) {
          return BplIdentity.Get(((XAttribute)node).Value, idscope);
       } else if (node is XElement) {
          return BplIdentity.Get(((XElement)node).Value, idscope);
       }
    } else {
       if (node is XAttribute) {
          return converter.Parse(((XAttribute)node).Value);
       } else if (node is XElement) {
          return converter.Parse((XElement)node);
       }
    }
    throw new InvalidOperationException();
 }
Exemplo n.º 10
0
 // serializes a container (singleton)
 private void _serializeContainer(XElement element, BplProperty property, BplObject bplObject) {
    if (bplObject == null) return;
    var singleton = new XElement(property.TagName);
    var targetClass = property.ReferencedClass;
    _serializeObject(singleton, bplObject, targetClass);
    element.Add(singleton);
 }
Exemplo n.º 11
0
      // serializes an array property
      private void _serializeArray(XElement element, BplProperty property, IEnumerable values) {
         var arrayTag = property.TagName;
         var primitiveTag = XNamespaces.none + property.PrimitiveType.TagName.LocalName;
         var arrayConverter = BplLanguage.XmlConverters[property.PrimitiveType];
         var itemConverter = BplLanguage.XmlConverters[property.PrimitiveItemType];

         if (property.IsWeakAssociation) {
            var array = new XElement(arrayTag);
            foreach (var value in values) {
               var weakId = ((BplIdentity)value).ToString(property.ReferencedClass.IdentityScope);
               array.Add(new XElement(primitiveTag, weakId));
            }
            if (array.HasElements) element.Add(array);
         } else if (_serializeAsElement(itemConverter) || _serializeAsElement(arrayConverter)) {
            var array = new XElement(arrayTag);
            foreach (var value in values) {
               var arrayItem = new XElement(primitiveTag);
               itemConverter.Format(arrayItem, value);
               array.Add(arrayItem);
            }
            if (array.HasElements) element.Add(array);
         } else {
            var literal = arrayConverter.Format(values);
            if (literal.NotEmpty()) {
               element.Add(new XAttribute(arrayTag, literal));
            }
         }
      }
Exemplo n.º 12
0
 // serializes a scalar property
 private void _serializeScalar(XElement element, BplProperty property, object value) {
    var converter = BplLanguage.XmlConverters[property];
    var tagName = property.TagName;
    if (property.IsWeakAssociation) {
       var weakId = ((BplIdentity)value).ToString(property.ReferencedClass.IdentityScope);
       if (weakId.NotEmpty()) {
          element.Add(new XAttribute(tagName, weakId));
       }
    } else if (_serializeAsElement(converter)) {
       if (!converter.IsDefaultValue(value)) {
          var child = new XElement(tagName);
          converter.Format(child, value);
          element.Add(child);
       }
    } else {
       var literal = converter.Format(value);
       if (literal.NotEmpty()) {
          element.Add(new XAttribute(tagName, literal));
       }
    }
 }
Exemplo n.º 13
0
 /// <summary>Creates a new <see cref="BplItemReplaced"/> instance.</summary>
 internal BplItemReplaced(BplObject source, BplProperty property, int index, BplContextNode oldItem, BplContextNode newItem)
    : base(source, property) {
    Index = index;
    OldItem = oldItem;
    NewItem = newItem;
 }
Exemplo n.º 14
0
 /// <summary>Gets the documentation of the specified BPL property.</summary>
 public BplDocumentationTopic this[BplProperty property] {
    get { return new BplDocumentationTopic(property); }
 }
Exemplo n.º 15
0
 private void _deserializeAssociation(BplObject bplObject, BplProperty property, ArrayList targetIds) {
    _errorDepth++;
    for (var i = 0; i < targetIds.Count; i++) {
       if (PreserveErrorsInfo) _updateErrorInfo(i);
       var target = (string)targetIds[i];
       _resolver.AddSource(target, bplObject, property);
    }
    _errorDepth--;
 }
Exemplo n.º 16
0
 private void _deserializeAssociation(BplObject bplObject, BplProperty property, string targetId) {
    _resolver.AddSource(targetId, bplObject, property);
 }
Exemplo n.º 17
0
 /// <summary>Add meta-property to the meta-class</summary>
 private void addProperty(BplProperty bplProperty, List<MetaProperty> properties) {
    properties.Add(new MetaProperty(bplProperty));
 }
Exemplo n.º 18
0
 // serializes a container (collection)
 private void _serializeContainer(XElement element, BplProperty property, IBplCollection bplCollection) {
    var count = 0;
    var collection = new XElement(property.TagName);
    var targetClass = property.ReferencedClass;
    foreach (var bplObject in bplCollection.OfType<BplObject>()) {
       _serializeObject(collection, bplObject, targetClass);
       count++;
    }
    if (count > 0) {
       element.Add(collection);
    }
 }
Exemplo n.º 19
0
 private void _deserializeScalar(BplObject bplObject, BplProperty property, XObject node) {
    try {
       var value = _parseScalar(property, BplLanguage.XmlConverters[property], node);
       property.SetValue(bplObject, value);
    } catch (Exception e) {
       AddError("Invalid value in BPL property '{:name}', {:where}. {0}", e.Message, node);
    }
 }
Exemplo n.º 20
0
 // serializes an association (singleton)
 private void _serializeAssociation(XElement element, BplProperty property, BplObject bplObject) {
    if (bplObject != null) {
       try {
          element.Add(new XAttribute(property.TagName, Verifier.ToReference(bplObject, property)));
       } catch (BplRuntimeException e) {
          AddException(e);
       }
    }
 }
Exemplo n.º 21
0
 private void _deserializeArray(BplObject bplObject, BplProperty property, XElement[] children) {
    var arrayValues = new object[children.Length];
    var arrayConverter = BplLanguage.XmlConverters[property];
    var itemConverter = BplLanguage.XmlConverters[property.PrimitiveItemType];
    var itemTag = XNamespaces.none + property.PrimitiveItemType.TagName.LocalName;
    for (var i = 0; i < children.Length; i++) {
       var child = children[i];
       try {
          if (child.Name != itemTag) {
             throw new Exception("Expected tag name '{0}'".Substitute(itemTag));
          }
          arrayValues[i] = _parseScalar(property, itemConverter, child);
       } catch (Exception e) {
          AddError("Invalid value in BPL property '{0}[{1}]', {:where}. {2}", property.Name, i, e.Message, child);
       }
    }
    if (arrayConverter is BplArrayConverter) {
       var array = ((BplArrayConverter)arrayConverter).CreateInstance(null, arrayValues);
       property.SetValue(bplObject, array);
    } else {
       AddError("Could not instantiate BPL array property '{0}'", property.Name);
    }
 }
Exemplo n.º 22
0
 // serializes an association (collection)
 private void _serializeAssociation(XElement element, BplProperty property, IBplCollection bplCollection) {
    var count = 0;
    var association = new XElement(property.TagName);
    var idTagName = XNamespaces.none + BplPrimitive.Get<BplIdentity>().TagName.LocalName;
    foreach (var bplObject in bplCollection.OfType<BplObject>()) {
       try {
          association.Add(new XElement(idTagName, Verifier.ToReference(bplObject, property)));
          count++;
       } catch (BplRuntimeException e) {
          AddException(e);
       }
    }
    if (count > 0) {
       element.Add(association);
    }
 }
Exemplo n.º 23
0
 private void _deserializeContainer(BplObject bplObject, BplProperty property, XElement[] children) {
    var collection = property.GetValue(bplObject) as IBplCollection;
    for (var i = 0; i < children.Length; i++) {
       var childObject = _deserializeObject(children[i]);
       if (childObject != null) {
          collection.Add(childObject);
       }
    }
 }
Exemplo n.º 24
0
 /// <summary>Creates a new <see cref="BplItemRemoved"/> instance.</summary>
 internal BplItemRemoved(BplObject source, BplProperty property, int oldIndex, BplContextNode oldItem)
    : base(source, property) {
    OldIndex = oldIndex;
    OldItem = oldItem;
 }
Exemplo n.º 25
0
 private void _deserializeAssociation(BplObject bplObject, BplProperty property, XElement[] targets) {
    var idTagName = XNamespaces.none + BplPrimitive.Get<BplIdentity>().TagName.LocalName;
    for (var i = 0; i < targets.Length; i++) {
       var target = targets[i];
       if (target.Name != idTagName) {
          AddError("Invalid value in BPL property '{0}[{1}]', {:where}. Expected tag name '{2}'.", property.Name, i, idTagName, target);
       }
       _resolver.AddSource(target.Value, bplObject, property);
    }
 }
Exemplo n.º 26
0
 /// <summary>Adds a source object/property reference to the specified target id.</summary>
 /// <returns><c>true</c> if the source was successfully added; <c>false</c> otherwise.</returns>
 public bool AddSource(string targetId, BplObject source, BplProperty property) {
    var fullTargetId = BplIdentity.Get(targetId, property.ReferencedClass.IdentityScope);
    _pending.Add(new BplPendingReference(source, property, fullTargetId));
    return true;
 }
Exemplo n.º 27
0
 /// <summary>Throws a <see cref="BplRuntimeException"/> with the given parameters.</summary>
 public static void Throw(string message, Exception innerException, BplObject sourceObject, BplProperty sourceProperty) {
    var formattedMessage = Assumption.TidyMessage(message.Substitute(sourceObject, sourceProperty.Name));
    throw new BplRuntimeException(formattedMessage, innerException, sourceObject, sourceProperty);
 }
Exemplo n.º 28
0
      /// <summary>Add BPL Property to the collection.</summary>
      private void addProperty(BplProperty property) {
         if (property.IsCalculated || property.IsVirtual) return;

         //if (property.CanonicName == "") {
         //   string s = "This is the one";
         //}

         if (property.IsPrimitive) {
            addPrimitive(property.PrimitiveType);
            if (property.IsArray) {
               arrays.Add(property.PrimitiveType.ItemType);
            }
         } else {
            addClass(property.ReferencedClass);

            if (property.IsContainer) {
               if (property.IsCollection) {
                  collections.Add(property.ReferencedClass);
               } else {
                  singletons.Add(property.ReferencedClass);
               }
            }
         }
      }
Exemplo n.º 29
0
      /// <summary>Attaches this node to the specified source object.</summary>
      internal void AttachTo(BplObject source, BplProperty property) {
         if (source == null || property == null) {
            BplRuntimeException.Throw("Source object or property are missing");
         }
         if (source.IsSealed) {
            BplRuntimeException.Throw("Node cannot be attached to a sealed object");
         }

         var isEntity = (this is BplEntity);
         var isTaxonomy = (this is BplTaxonomy);
         var sourceIsTaxonomy = (source is BplTaxonomy);
         if (sourceIsTaxonomy && !isTaxonomy) {
            BplRuntimeException.Throw("'{0}' cannot be referenced from a taxonomy", this);
         }

         if (property.IsContainer) {

            // property is a container, so we need to reparent the node

            var oldParent = Parent;
            var oldContainer = ParentContainer;
            if (oldParent == source && oldContainer == property) {
               BplRuntimeException.Throw("Node is already contained in '{1}' in {0}.", source, property);
            }

            // attach to new parent
            Parent = source;
            ParentContainer = property;

            // detach from old parent (this is done after attaching the new parent, so the node does not get disconnected from the context)
            if (oldContainer != null) {
               if (oldContainer.IsReference) {
                  oldContainer.SetValue(oldParent, null);
               } else {
                  oldContainer.GetCollection(oldParent).Remove(this);
               }
            }

         } else {

            // property is an assocation, so we need to update the backward associations

            if (!isEntity) {
               // association target must be an entity
               BplRuntimeException.Throw("'{0}' cannot be the target of association since it is not an entity", this);
            }

            if (!(sourceIsTaxonomy ^ isTaxonomy)) {
               // no backward association between taxonomies and non-taxonomies
               Sources.AddTuple(source, property);
            }

         }

         // attach the source and target to same context
         var sourceContext = BplContext.GetOwner(source);
         var targetContext = Context;
         if (sourceContext != targetContext) {
            if (sourceContext == null) {
               _attachToContext((BplContextNode)source, targetContext);
            } else if (targetContext == null) {
               _attachToContext(this, sourceContext);
            } else {
               _verifyContextCompatibility(sourceContext, targetContext);
            }
         }

      }
Exemplo n.º 30
0
 // Internal constructor (to prevent direct inheritance by external plugins)
 internal BplCollectionChange(BplObject source, BplProperty property) : base(source, property) {
 }