From 5a972cf00f332e2bb8cd67adec50bb3384e58177 Mon Sep 17 00:00:00 2001 From: Gerhard Fobe Date: Sun, 2 Sep 2012 02:21:53 +0200 Subject: [PATCH] validate in parser with EmotionML schema --- EmotionML.csproj | 4 +- Parser.cs | 33 +++++- resources/vocabularies.emotionml | 190 +++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 resources/vocabularies.emotionml diff --git a/EmotionML.csproj b/EmotionML.csproj index d120341..abe99d4 100644 --- a/EmotionML.csproj +++ b/EmotionML.csproj @@ -60,7 +60,9 @@ - + + + Designer diff --git a/Parser.cs b/Parser.cs index 4d879d1..73a303e 100644 --- a/Parser.cs +++ b/Parser.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Text; using System.Xml; +using System.IO; +using System.Xml.Schema; namespace Vsr.Hawaii.EmotionmlLib { @@ -60,12 +62,12 @@ public Emotion getSingleEmotion() /// parses the whole staff of EmotionML /// /// do not validate input against EmotionML schema - public void parse(bool ignoreSchema = false) + public void parse(bool ignoreSchema = true) { init(); if (!ignoreSchema) { - validateAgainstScheme(); + isValidAgainstSchema(); } //TODO: named entities auflösen: http://msdn.microsoft.com/en-us/library/system.xml.xmlnodereader.resolveentity(v=vs.71).aspx @@ -130,10 +132,33 @@ protected void init() nsManager.AddNamespace("emo", EmotionML.NAMESPACE); } - protected void validateAgainstScheme() + protected bool isValidAgainstSchema() { + string schemaStringGeneral = Helper.loadInternalResource("emotionml.xsd"); + string schemaStringFragments = Helper.loadInternalResource("emotionml-fragments.xsd"); + string xmlString = emotionml.toXmlDocument(); - //TODO + //load xml document + XmlDocument xml = new XmlDocument(); + xml.LoadXml(xmlString); + + //add schemata + XmlSchema schema = XmlSchema.Read(new StringReader(schemaStringGeneral), null); + xml.Schemas.Add(schema); + schema = XmlSchema.Read(new StringReader(schemaStringFragments), null); + xml.Schemas.Add(schema); + + //TODO: schauen, ob als Input ein dokument oder ein fragment kommt + + try + { + xml.Validate(null); + return true; + } + catch + { + return false; + } } public bool validate() diff --git a/resources/vocabularies.emotionml b/resources/vocabularies.emotionml new file mode 100644 index 0000000..bbfd4ac --- /dev/null +++ b/resources/vocabularies.emotionml @@ -0,0 +1,190 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +