private ValidationResult AddViolation(ViolationCollector a, OperationInvalid v) { if (a != null) { a.Add(v); } return v.ValidationResult; }
private ValidationResult AddViolation(ViolationCollector a, SchemaViolation v) { if (a != null) { a.Add(v); } return v.ValidationResult; }
/// <summary> /// Returns whether op is well-formed. /// /// Any violations recorded in the output <paramref name="collector"/> that are not well-formedness /// violations are meaningless. /// </summary> public static bool IsWellformed(ViolationCollector collector, IDocOp docOp) { if (docOp is IBufferedDocOp) { return IsWellformed(collector, (BufferedDocOp) docOp); } return IsWellformedRaw(collector, docOp); }
/// <summary> /// Same as <see cref="IsWellformed"/>, but without the fast path for <see cref="BufferedDocOp"/> /// </summary> private static bool IsWellformedRaw(ViolationCollector collector, IDocOp docOp) { // We validate the operation against the empty document. It will likely // be invalid; however, we ignore the validity aspect anyway since we // only care about well-formedness. var validationResult = Validate(collector, DocumentSchema.NoSchemaConstraints, DocOpAutomaton.EmptyDocument, docOp); return validationResult != ValidationResult.IllFormed; }
public ValidationResult CheckCharacters(string characters, ViolationCollector collector) { if (characters == null) return NullCharacters(collector); if (string.IsNullOrEmpty(characters)) return EmptyCharacters(collector); // Todo: implement other checks if (!DeletionStackIsEmpty()) { return InsertInsideDelete(collector); } // Todo: implement other checks throw new NotImplementedException(); return Valid(); }
/// <summary> /// Same as <see cref="IsWellformed" />, but with a fast path for already-validated instances of <see cref="BufferedDocOp"/> /// </summary> private static bool IsWellformed(ViolationCollector collector, BufferedDocOp docOp) { if (docOp.IsKnownToBeWellformed) { return true; } if (IsWellformedRaw(collector, docOp)) { docOp.IsKnownToBeWellformed = true; return true; } return false; }
private static ValidationResult Validate(ViolationCollector collector, IDocumentSchema schema, IAutomatonDocument document, IDocOp docOp) { if (schema == null) { schema = DocumentSchema.NoSchemaConstraints; } var automation = new DocOpAutomaton(document, schema); var accu = new ValidationResult[] { ValidationResult.Valid }; try { docOp.Apply(new ValidationDocOpCursor(collector, automation, accu)); } catch (IllFormedException illFormed) { return ValidationResult.IllFormed; } accu[0] = accu[0].MergeWith(automation.CheckFinish(collector)); return accu[0]; }
private ValidationResult MismatchedStartAnnotation(ViolationCollector v, String key) { return AddViolation(v, IllFormedOperation("annotation of key " + key + " starts but never ends")); }
private ValidationResult NullTag(ViolationCollector collector) { return AddViolation(collector, IllFormedOperation("element type is null")); }
private ValidationResult EmptyCharacters(ViolationCollector collector) { return AddViolation(collector, IllFormedOperation("characters is empty")); }
/// <summary> /// Checks that a buffered doc op is well-formed. /// </summary> /// <param name="value">value op to check</param> /// <exception cref="InvalidOperationException">if the op is ill-formed</exception> private static void CheckWellformedness(IBufferedDocOp value) { if (!DocOpValidator.IsWellformed(null, value)) { // Check again, collecting violations this time. var violationCollector = new ViolationCollector(); DocOpValidator.IsWellformed(violationCollector, value); Preconditions.IllegalState( string.Format("Attempt to build ill-formed operation ({0}): {1}", violationCollector, value)); } }
private ValidationResult MismatchedEndAnnotation(ViolationCollector v, String key) { return AddViolation(v, IllFormedOperation("annotation of key " + key + " ends without having started")); }
private ValidationResult NullCharacters(ViolationCollector collector) { return AddViolation(collector, IllFormedOperation("characters is null")); }
private ValidationResult RetainInsideInsertOrDelete(ViolationCollector v) { return AddViolation(v, IllFormedOperation("retain inside insert or delete")); }
private ValidationResult MissingRetainToEnd(ViolationCollector v, int expectedLength, int actualLength) { return AddViolation(v, InvalidOperation("operation shorter than document, document length " + expectedLength + ", length of input of operation " + actualLength)); }
private ValidationResult MismatchedDeleteStart(ViolationCollector v) { return AddViolation(v, IllFormedOperation("deleteElementStart with no matching deleteElementEnd")); }
internal ValidationResult NotClosed(DocOpAutomaton automaton, ViolationCollector collector) { return automaton.MismatchedInsertStart(collector); }
public ValidationResult CheckRetain(int itemCount, ViolationCollector v) { // well-formedness if (itemCount <= 0) { return RetainItemCountNotPositive(v); } if (!InsertionStackIsEmpty()) { return RetainInsideInsertOrDelete(v); } if (!DeletionStackIsEmpty()) { return RetainInsideInsertOrDelete(v); } //// validity if (!CanRetain(itemCount)) { return RetainPastEnd(v, _document.Length(), itemCount); } return CheckAnnotationsForRetain(v, itemCount); }
public ValidationResult CheckElementStart(string type, IAttributes attributes, ViolationCollector collector) { // Well-formedness if (type == null) { return NullTag(collector); } if (!DeletionStackIsEmpty()) { return InsertInsideDelete(collector); } throw new NotImplementedException(); }
/// <summary> /// Checks whether the automaton is in an accepting state, i.e., whether the /// operation would be valid if no further operation components follow. /// </summary> public ValidationResult CheckFinish(ViolationCollector v) { // well-formedness if (!InsertionStackIsEmpty()) { foreach (InsertStart e in _insertionStack) { return e.NotClosed(this, v); } } if (!DeletionStackIsEmpty()) { return MismatchedDeleteStart(v); } if (_annotationsUpdate.ChangeSize() > 0) { return MismatchedStartAnnotation(v, _annotationsUpdate.GetChangeKey(0)); } // validity if (_effectivePos != _document.Length()) { return MissingRetainToEnd(v, _document.Length(), _effectivePos); } return Valid(); }
private ValidationResult RetainPastEnd(ViolationCollector collector, int expectedLength, int retainItemCount) { return AddViolation(collector, InvalidOperation("retain past end of document, document length " + expectedLength + ", retain item count " + retainItemCount)); }
private ValidationResult CheckAnnotationsForRetain(ViolationCollector collector, int itemCount) { for (int i = 0; i < _annotationsUpdate.ChangeSize(); i++) { throw new NotImplementedException(); //var key = _annotationsUpdate.getChangeKey(i); //var oldValue = _annotationsUpdate.getOldValue(i); //var firstChange = _document.FirstAnnotationChange(_effectivePos, _effectivePos + itemCount, key, oldValue); //if (firstChange != -1) //{ // return OldAnnotationsDifferFromDocument(collector, key, oldValue, // _document.GetAnnotation(firstChange, key)); //} } return Valid(); }
private ValidationResult InsertInsideDelete(ViolationCollector v) { return AddViolation(v, IllFormedOperation("insertion inside deletion")); }
private ValidationResult RetainItemCountNotPositive(ViolationCollector v) { return AddViolation(v, IllFormedOperation("retain item count not positive")); }