|
From: <br...@us...> - 2008-08-09 13:25:00
|
Revision: 309
http://acmcontester.svn.sourceforge.net/acmcontester/?rev=309&view=rev
Author: brus07
Date: 2008-08-09 13:25:09 +0000 (Sat, 09 Aug 2008)
Log Message:
-----------
Added helper methods, for creating object from XML. Changed code for used this methods, where need.
Modified Paths:
--------------
ACMServer/trunk/ACMServer/Library/Data/Result.cs
ACMServer/trunk/ACMServer/Library/Data/Submit.cs
ACMServer/trunk/ACMServer/Library/Data/SubmitList.cs
ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj
Added Paths:
-----------
ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlHelper.cs
Removed Paths:
-------------
ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs
Modified: ACMServer/trunk/ACMServer/Library/Data/Result.cs
===================================================================
--- ACMServer/trunk/ACMServer/Library/Data/Result.cs 2008-08-06 21:29:48 UTC (rev 308)
+++ ACMServer/trunk/ACMServer/Library/Data/Result.cs 2008-08-09 13:25:09 UTC (rev 309)
@@ -49,15 +49,7 @@
public static Result CreateFromXml(string message)
{
- message = Xml.GetOuterXmlOfSingleNodeFromXpath(message, "result");
- if (message == null || message == "")
- return null;
- string schema = Reader.ReadString("data.xsd");
- if (Xml.ValidateXmlWithSchema(message, schema) == false)
- return null;
-
- Result result = XmlSerializer<Result>.Load(message);
- return result;
+ return XmlHelper.GetObject<Result>(message, "result", "data.xsd");
}
public override string ToString()
Modified: ACMServer/trunk/ACMServer/Library/Data/Submit.cs
===================================================================
--- ACMServer/trunk/ACMServer/Library/Data/Submit.cs 2008-08-06 21:29:48 UTC (rev 308)
+++ ACMServer/trunk/ACMServer/Library/Data/Submit.cs 2008-08-09 13:25:09 UTC (rev 309)
@@ -16,8 +16,6 @@
[XmlElement("language", typeof(int))]
public int language;
- string temp;
-
public Submit()
{
}
@@ -31,15 +29,7 @@
public static Submit CreateFromXml(string message)
{
- message = Xml.GetOuterXmlOfSingleNodeFromXpath(message, "submit");
- if (message == null || message == "")
- return null;
- string schema = Reader.ReadString("data.xsd");
- if (Xml.ValidateXmlWithSchema(message, schema) == false)
- return null;
-
- Submit newList = XmlSerializer<Submit>.Load(message);
- return newList;
+ return XmlHelper.GetObject<Submit>(message, "submit", "data.xsd");
}
//HACK: \xE4\xEB\xFF \xF2\xE5\xF1\xF2\xF3 \xF5\xE0\xE9 \xE1\xF3\xE4\xE5 \xF2\xE0\xEA, \xE0\xEB\xE5 \xEC\xE0\xBA \xE1\xF3\xF2\xE8 \xF7\xE5\xF0\xE5\xE7 XML
Modified: ACMServer/trunk/ACMServer/Library/Data/SubmitList.cs
===================================================================
--- ACMServer/trunk/ACMServer/Library/Data/SubmitList.cs 2008-08-06 21:29:48 UTC (rev 308)
+++ ACMServer/trunk/ACMServer/Library/Data/SubmitList.cs 2008-08-09 13:25:09 UTC (rev 309)
@@ -17,15 +17,7 @@
public static SubmitList CreateFromXml(string message)
{
- message = Xml.GetOuterXmlOfSingleNodeFromXpath(message, "submitList");
- if (message == null || message == "")
- return null;
- string schema = Reader.ReadString("data.xsd");
- if (Xml.ValidateXmlWithSchema(message, schema) == false)
- return null;
-
- SubmitList newList = XmlSerializer<SubmitList>.Load(message);
- return newList;
+ return XmlHelper.GetObject<SubmitList>(message, "submitList", "data.xsd");
}
public override string ToString()
{
Modified: ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj
===================================================================
--- ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-08-06 21:29:48 UTC (rev 308)
+++ ACMServer/trunk/ACMServer/Library/LibraryExtention/LibraryExtention.csproj 2008-08-09 13:25:09 UTC (rev 309)
@@ -36,7 +36,7 @@
<Compile Include="Log.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" />
- <Compile Include="XML\Xml.cs" />
+ <Compile Include="XML\XmlHelper.cs" />
<Compile Include="XML\XmlSerializer.cs" />
<Compile Include="XML\XmlValidator.cs" />
</ItemGroup>
Deleted: ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs
===================================================================
--- ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs 2008-08-06 21:29:48 UTC (rev 308)
+++ ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs 2008-08-09 13:25:09 UTC (rev 309)
@@ -1,62 +0,0 @@
-using System;
-using System.Collections;
-using System.Xml;
-
-namespace AcmContester.AcmLibraryExtention.XML
-{
- public class Xml
- {
- XmlDocument document = new XmlDocument();
-
- string name;
- string xmlDeclaration = "";
-
- public string Name
- {
- get
- {
- return name;
- }
- }
-
- public Xml(string str)
- {
- document.LoadXml(str);
- if (document.ChildNodes[0].NodeType == XmlNodeType.XmlDeclaration)
- {
- xmlDeclaration = document.ChildNodes[0].OuterXml;
- document.RemoveChild(document.FirstChild);
- }
- name = document.FirstChild.Name;
- }
- public override string ToString()
- {
- return document.OuterXml;
- }
-
- public static string GetOuterXmlOfSingleNodeFromXpath(string fullXml, string xmlPath)
- {
- //XmlNodeList nodes = doc.SelectNodes("descendant::root/submit");
- XmlDocument document = new XmlDocument();
- string result = null;
- try
- {
- document.LoadXml(fullXml);
- XmlNode node = document.SelectSingleNode("descendant::" + xmlPath);
- if (node != null)
- result = node.OuterXml;
- }
- catch (XmlException ex)
- {
- throw ex;
- }
- return result;
- }
-
- public static bool ValidateXmlWithSchema(string xml, string schema)
- {
- XmlValidator validator = new XmlValidator(xml);
- return validator.Validate(schema);
- }
- }
-}
Copied: ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlHelper.cs (from rev 308, ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/Xml.cs)
===================================================================
--- ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlHelper.cs (rev 0)
+++ ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlHelper.cs 2008-08-09 13:25:09 UTC (rev 309)
@@ -0,0 +1,100 @@
+using System;
+using System.Collections;
+using System.Xml;
+using AcmContester.Library.LibraryExtention;
+
+namespace AcmContester.AcmLibraryExtention.XML
+{
+ public class XmlHelper
+ {
+
+ public static string GetOuterXmlOfSingleNodeFromXpath(string fullXml, string xmlPath)
+ {
+ //XmlNodeList nodes = doc.SelectNodes("descendant::root/submit");
+ XmlDocument document = new XmlDocument();
+ string result = null;
+ try
+ {
+ document.LoadXml(fullXml);
+ XmlNode node = document.SelectSingleNode("descendant::" + xmlPath);
+ if (node != null)
+ result = node.OuterXml;
+ }
+ catch (XmlException ex)
+ {
+ throw ex;
+ }
+ return result;
+ }
+
+ public static bool ValidateXmlWithSchema(string xml, string schema)
+ {
+ XmlValidator validator = new XmlValidator(xml);
+ return validator.Validate(schema);
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="fullXml"></param>
+ /// <param name="xmlPath"></param>
+ /// <param name="pathToSchemaFile"></param>
+ /// <returns></returns>
+ /// <exception cref="ArgumentNullException"></exception>
+ /// <exception cref="ArgumentException"></exception>
+ public static T GetObject<T>(string fullXml, string xmlPath, string pathToSchemaFile) where T: class
+ {
+ if (fullXml == null)
+ throw new ArgumentNullException("fullXml", "Can't be null");
+ if (fullXml == null)
+ throw new ArgumentException("fullXml", "Can't be empty");
+ if (xmlPath == null)
+ throw new ArgumentNullException("xmlPath", "Can't be null");
+ if (xmlPath == null)
+ throw new ArgumentException("xmlPath", "Can't be empty");
+ if (pathToSchemaFile == null)
+ throw new ArgumentNullException("pathToSchemaFile", "Can't be null. You can use other method without this parameter");
+ if (pathToSchemaFile == null)
+ throw new ArgumentException("pathToSchemaFile", "Can't be null. You can use other method without this parameter");
+
+ fullXml = XmlHelper.GetOuterXmlOfSingleNodeFromXpath(fullXml, xmlPath);
+ if (fullXml == null || fullXml == "")
+ return null;
+ string schema = Reader.ReadString(pathToSchemaFile);
+ if (XmlHelper.ValidateXmlWithSchema(fullXml, schema) == false)
+ return null;
+
+ T result = XmlSerializer<T>.Load(fullXml);
+ return result;
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="fullXml"></param>
+ /// <param name="xmlPath"></param>
+ /// <returns></returns>
+ /// <exception cref="ArgumentNullException"></exception>
+ /// <exception cref="ArgumentException"></exception>
+ public static T GetObject<T>(string fullXml, string xmlPath) where T : class
+ {
+ if (fullXml == null)
+ throw new ArgumentNullException("fullXml", "Can't be null");
+ if (fullXml == null)
+ throw new ArgumentException("fullXml", "Can't be empty");
+ if (xmlPath == null)
+ throw new ArgumentNullException("xmlPath", "Can't be null");
+ if (xmlPath == null)
+ throw new ArgumentException("xmlPath", "Can't be empty");
+
+ fullXml = XmlHelper.GetOuterXmlOfSingleNodeFromXpath(fullXml, xmlPath);
+ if (fullXml == null || fullXml == "")
+ return null;
+
+ T result = XmlSerializer<T>.Load(fullXml);
+ return result;
+ }
+ }
+}
Property changes on: ACMServer/trunk/ACMServer/Library/LibraryExtention/XML/XmlHelper.cs
___________________________________________________________________
Added: svn:mergeinfo
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|