public void Translate() { try { LogManager.SourceTrace(ATMLTranslator.SOURCE, "Beginning Translation of the AIXML document to the 1671.1 Test Description..."); ProjectInfo pi = ProjectManager.ProjectInfo; if (pi == null || pi.TranslationInfo == null) { throw new TranslationException("Failed to obtain the Translation Configuration"); } string projectName = pi.ProjectName; string projectId = pi.Uuid; string uutName = pi.UutName; string uutId = pi.UutId; string testStation = pi.ClassName; bool isSegmented = pi.TranslationInfo.Segmented; DateTime startTime = DateTime.Now; string xmlPath = Path.Combine(ATMLContext.ProjectTranslatorAixmlPath, projectName + ".aixml.xml"); string atmlPath = ATMLContext.ProjectAtmlPath; //(String)ATMLContext.GetProperty("environment.atml.xml-path"); if (String.IsNullOrWhiteSpace(projectName)) { LogManager.SourceWarn(ATMLTranslator.SOURCE, "You must open a project with source code to translate."); } else { //string projectPath = ATMLContext.PROJECT_PATH;// Path.Combine( ATMLContext.TESTSET_PATH, projectName ); // xmlPath = xmlPath.Replace( "${FILENAME}", ProjectManager.ProjectName + ".atml" ) // .Replace( "${PROJECTPATH}", projectPath ); Document xslDocument = DocumentManager.GetDocument("D7F7A05B-DB5F-4E93-BF2C-F79CF72F6047"); //XSLT Translation Document //atmlPath = atmlPath.Replace( "${PROJECTPATH}", projectPath ); if (xslDocument != null) { try { var ok2Translate = true; var sb = new StringBuilder(); var settings = new XmlWriterSettings(); var fi = new FileInfo(Path.Combine(atmlPath, ProjectManager.ProjectName + ".1671.1.xml")); settings.Indent = true; settings.IndentChars = " "; settings.NewLineChars = "\r\n"; settings.NewLineHandling = NewLineHandling.Replace; var xslt = new XslCompiledTransform(); if (!File.Exists(xmlPath)) { throw new TranslationException("Translation Failed: Missing AIXML File."); } if (fi.Exists) { ok2Translate = MessageBox.Show( string.Format("A Test Description for {0} already exists, would you like to overwrite it?", ProjectManager.ProjectName), @"Overwrite Test Description", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes; } if (!ok2Translate) { LogManager.Trace("AIXML Translation to ATML 1671.1 has been canceled."); } else { using (XmlWriter writer = XmlWriter.Create(sb, settings)) { var xmlDocument = new XPathDocument(xmlPath); xslt.Load( XmlReader.Create( new StringReader(Encoding.UTF8.GetString(xslDocument.DocumentContent)))); var xslArg = new XsltArgumentList(); var obj = new TranslationLibrary(); xslArg.AddExtensionObject("urn:utrs.atml-translator-tools", obj); xslArg.XsltMessageEncountered += delegate(object sender, XsltMessageEncounteredEventArgs args) { LogManager.SourceError(ATMLTranslator.SOURCE, args.Message); }; xslt.Transform(xmlDocument, xslArg, writer, null); DateTime stopTime = DateTime.Now; TimeSpan diff = stopTime - startTime; LogManager.SourceTrace(ATMLTranslator.SOURCE, "Translation Time: {0}", diff.ToString()); } FileManager.WriteFile(fi.FullName, Encoding.UTF8.GetBytes(sb.ToString())); LogManager.SourceTrace(ATMLTranslator.SOURCE, "File \"{0}\" has been saved. ", fi.Name); try { TestDescription td = TestDescription.Deserialize(sb.ToString()); OnTranslated(td, sb.ToString()); Document document = DocumentManager.GetDocument(fi.Name, (int) dbDocument.DocumentType .TEST_DESCRIPTION); if (document == null) { document = new Document(); document.uuid = td.uuid; document.DocumentType = dbDocument.DocumentType.TEST_DESCRIPTION; document.ContentType = DocumentManager.DetermineContentType(fi.Name); document.DataState = BASEBean.eDataState.DS_ADD; } else { document.DataState = BASEBean.eDataState.DS_EDIT; } document.FileInfo = fi; document.name = fi.Name; document.Description = "Test Description for " + projectName; document.DocumentContent = Encoding.UTF8.GetBytes(sb.ToString()); document.version = td.version; DocumentManager.SaveDocument(document); } catch (Exception e) { OnTranslated(null, null); LogManager.SourceWarn(ATMLTranslator.SOURCE, "Failed to create a TestDescription object at this time."); LogManager.Debug(e); } } } catch (TranslationException e) { OnTranslated(null, null); LogManager.SourceError(ATMLTranslator.SOURCE, e.Message); } catch (Exception e) { OnTranslated(null, null); LogManager.SourceError(ATMLTranslator.SOURCE, e, "Error translating AIXML for Project: {0} XML Path: {1} ATML Path: {2}", projectName, xmlPath, atmlPath); } } } } catch (Exception e) { LogManager.SourceError(ATMLTranslator.SOURCE, e); } }
public void TranslateAIXMLDocument() { if (_content != null && IsParsible()) { try { //---------------------------------// //--- Setup XML reader settings ---// //---------------------------------// ValidationEventHandler validationHandler = (s, ee) => LogManager.SourceError(ATMLTranslator.SOURCE, ee.Exception); var settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.None; settings.ValidationFlags = XmlSchemaValidationFlags.None; settings.ValidationEventHandler += validationHandler; string content = Encoding.UTF8.GetString( _content ); if (string.IsNullOrWhiteSpace( content )) return; using (var sr = new StringReader( content )) { using (XmlReader xrXml = new XmlTextReader( sr )) { var doc = new XPathDocument( xrXml ); string xsdName = SchemaManager.GetSchemaName( Encoding.UTF8.GetString( _content ) ); string xslName = xsdName.Replace( ".xsd", ".xsl" ); StringReader xslReader = GetXSLReader( xslName ); var xr = new XmlTextReader( xslReader ); var xslt = new XslCompiledTransform(); xslt.Load( xr ); //--------------------------------------------------------------------------// //--- Create an XsltArgumentList for custom transformation functionality ---// //--------------------------------------------------------------------------// var xslArg = new XsltArgumentList(); xslArg.AddParam( "documentName", "", ProjectManager.ProjectName + ".1671.4.xml" ); // Add an object to calculate the new book price. var obj = new TranslationLibrary(); //TODO: Figure out all the functionality required for translation xslArg.AddExtensionObject( "urn:utrs.atml-translator-tools", obj ); //---------------------------// //--- Transform the file. ---// //---------------------------// using (var w = new StringWriter()) { var stringBuilder = new StringBuilder(); var xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; xws.Indent = true; xws.NewLineOnAttributes = false; xws.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter xmlWriter = XmlWriter.Create( stringBuilder, xws )) { xslt.Transform( doc, xslArg, xmlWriter ); LogManager.SourceTrace(ATMLTranslator.SOURCE, "ATML Test Description translation Completed"); try { string xmlOut = stringBuilder.ToString(); FileManager.WriteFile( Encoding.UTF8.GetBytes( xmlOut ) ); } catch (Exception e) { LogManager.SourceError(ATMLTranslator.SOURCE, "An error has occurred attempting to marshall the translated document into an ATML Test Configuration object.", e ); } //OnTranslatedInputDocument(stringBuilder.ToString(), _testConfiguration); } } } } } catch (Exception ee) { LogManager.SourceError(ATMLTranslator.SOURCE, ee, "An error has occurred attempting to process the input document."); } } }
public void TranslateAIXMLDocument() { if (_content != null && IsParsible()) { try { //---------------------------------// //--- Setup XML reader settings ---// //---------------------------------// ValidationEventHandler validationHandler = (s, ee) => LogManager.SourceError(ATMLTranslator.SOURCE, ee.Exception); var settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.None; settings.ValidationFlags = XmlSchemaValidationFlags.None; settings.ValidationEventHandler += validationHandler; string content = Encoding.UTF8.GetString(_content); if (string.IsNullOrWhiteSpace(content)) { return; } using (var sr = new StringReader(content)) { using (XmlReader xrXml = new XmlTextReader(sr)) { var doc = new XPathDocument(xrXml); string xsdName = SchemaManager.GetSchemaName(Encoding.UTF8.GetString(_content)); string xslName = xsdName.Replace(".xsd", ".xsl"); StringReader xslReader = GetXSLReader(xslName); var xr = new XmlTextReader(xslReader); var xslt = new XslCompiledTransform(); xslt.Load(xr); //--------------------------------------------------------------------------// //--- Create an XsltArgumentList for custom transformation functionality ---// //--------------------------------------------------------------------------// var xslArg = new XsltArgumentList(); xslArg.AddParam("documentName", "", ProjectManager.ProjectName + ".1671.4.xml"); // Add an object to calculate the new book price. var obj = new TranslationLibrary(); //TODO: Figure out all the functionality required for translation xslArg.AddExtensionObject("urn:utrs.atml-translator-tools", obj); //---------------------------// //--- Transform the file. ---// //---------------------------// using (var w = new StringWriter()) { var stringBuilder = new StringBuilder(); var xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; xws.Indent = true; xws.NewLineOnAttributes = false; xws.NamespaceHandling = NamespaceHandling.OmitDuplicates; using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xws)) { xslt.Transform(doc, xslArg, xmlWriter); LogManager.SourceTrace(ATMLTranslator.SOURCE, "ATML Test Description translation Completed"); try { string xmlOut = stringBuilder.ToString(); FileManager.WriteFile(Encoding.UTF8.GetBytes(xmlOut)); } catch (Exception e) { LogManager.SourceError(ATMLTranslator.SOURCE, "An error has occurred attempting to marshall the translated document into an ATML Test Configuration object.", e); } //OnTranslatedInputDocument(stringBuilder.ToString(), _testConfiguration); } } } } } catch (Exception ee) { LogManager.SourceError(ATMLTranslator.SOURCE, ee, "An error has occurred attempting to process the input document."); } } }
public void Translate() { try { LogManager.SourceTrace( ATMLTranslator.SOURCE, "Beginning Translation of the AIXML document to the 1671.1 Test Description..." ); ProjectInfo pi = ProjectManager.ProjectInfo; if (pi == null || pi.TranslationInfo == null) throw new TranslationException( "Failed to obtain the Translation Configuration" ); string projectName = pi.ProjectName; string projectId = pi.Uuid; string uutName = pi.UutName; string uutId = pi.UutId; string testStation = pi.ClassName; bool isSegmented = pi.TranslationInfo.Segmented; DateTime startTime = DateTime.Now; string xmlPath = Path.Combine( ATMLContext.ProjectTranslatorAixmlPath, projectName + ".aixml.xml" ); string atmlPath = ATMLContext.ProjectAtmlPath; //(String)ATMLContext.GetProperty("environment.atml.xml-path"); if (String.IsNullOrWhiteSpace( projectName )) { LogManager.SourceWarn( ATMLTranslator.SOURCE, "You must open a project with source code to translate." ); } else { //string projectPath = ATMLContext.PROJECT_PATH;// Path.Combine( ATMLContext.TESTSET_PATH, projectName ); // xmlPath = xmlPath.Replace( "${FILENAME}", ProjectManager.ProjectName + ".atml" ) // .Replace( "${PROJECTPATH}", projectPath ); Document xslDocument = DocumentManager.GetDocument( "D7F7A05B-DB5F-4E93-BF2C-F79CF72F6047" ); //XSLT Translation Document //atmlPath = atmlPath.Replace( "${PROJECTPATH}", projectPath ); if (xslDocument != null) { try { var ok2Translate = true; var sb = new StringBuilder(); var settings = new XmlWriterSettings(); var fi = new FileInfo(Path.Combine(atmlPath, ProjectManager.ProjectName + ".1671.1.xml")); settings.Indent = true; settings.IndentChars = " "; settings.NewLineChars = "\r\n"; settings.NewLineHandling = NewLineHandling.Replace; var xslt = new XslCompiledTransform(); if (!File.Exists( xmlPath )) throw new TranslationException( "Translation Failed: Missing AIXML File." ); if (fi.Exists) ok2Translate = MessageBox.Show( string.Format( "A Test Description for {0} already exists, would you like to overwrite it?", ProjectManager.ProjectName ), @"Overwrite Test Description", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes; if (!ok2Translate) { LogManager.Trace( "AIXML Translation to ATML 1671.1 has been canceled." ); } else { using (XmlWriter writer = XmlWriter.Create( sb, settings )) { var xmlDocument = new XPathDocument( xmlPath ); xslt.Load( XmlReader.Create( new StringReader( Encoding.UTF8.GetString( xslDocument.DocumentContent ) ) ) ); var xslArg = new XsltArgumentList(); var obj = new TranslationLibrary(); xslArg.AddExtensionObject( "urn:utrs.atml-translator-tools", obj ); xslArg.XsltMessageEncountered += delegate( object sender, XsltMessageEncounteredEventArgs args ) { LogManager.SourceError( ATMLTranslator.SOURCE, args.Message ); }; xslt.Transform( xmlDocument, xslArg, writer, null ); DateTime stopTime = DateTime.Now; TimeSpan diff = stopTime - startTime; LogManager.SourceTrace( ATMLTranslator.SOURCE, "Translation Time: {0}", diff.ToString() ); } FileManager.WriteFile( fi.FullName, Encoding.UTF8.GetBytes( sb.ToString() ) ); LogManager.SourceTrace( ATMLTranslator.SOURCE, "File \"{0}\" has been saved. ", fi.Name ); try { TestDescription td = TestDescription.Deserialize( sb.ToString() ); OnTranslated( td, sb.ToString() ); Document document = DocumentManager.GetDocument( fi.Name, (int) dbDocument.DocumentType .TEST_DESCRIPTION ); if (document == null) { document = new Document(); document.uuid = td.uuid; document.DocumentType = dbDocument.DocumentType.TEST_DESCRIPTION; document.ContentType = DocumentManager.DetermineContentType( fi.Name ); document.DataState = BASEBean.eDataState.DS_ADD; } else { document.DataState = BASEBean.eDataState.DS_EDIT; } document.FileInfo = fi; document.name = fi.Name; document.Description = "Test Description for " + projectName; document.DocumentContent = Encoding.UTF8.GetBytes( sb.ToString() ); document.version = td.version; DocumentManager.SaveDocument( document ); } catch (Exception e) { OnTranslated( null, null ); LogManager.SourceWarn( ATMLTranslator.SOURCE, "Failed to create a TestDescription object at this time." ); LogManager.Debug( e ); } } } catch (TranslationException e) { OnTranslated( null, null ); LogManager.SourceError( ATMLTranslator.SOURCE, e.Message ); } catch (Exception e) { OnTranslated( null, null ); LogManager.SourceError( ATMLTranslator.SOURCE, e, "Error translating AIXML for Project: {0} XML Path: {1} ATML Path: {2}", projectName, xmlPath, atmlPath ); } } } } catch (Exception e) { LogManager.SourceError( ATMLTranslator.SOURCE, e ); } }