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 );
}
}