2005-06-14 Alexander Olk <xenomorph2@onlinehome.de>

* AliasReader.cs, CommentReader.cs, FDOxml2cs.cs, GlobReader.cs,
	  MagicReader.cs, MainReader.cs, Match.cs, MatchReader.cs, MatchTypes.cs,
	  MimeType.cs, MimeTypeReader.cs, MimeUtils.cs, SourceWriter.cs,
	  SubClassReader.cs, SubMatchReader.cs:
	  Initial import of the Freedesktop.org mime xml to C# source converter

svn path=/trunk/winforms-tools/; revision=45975
This commit is contained in:
Alexander Olk 2005-06-14 15:38:49 +00:00
Родитель 1870a6516e
Коммит 69ea18a076
19 изменённых файлов: 1595 добавлений и 0 удалений

30
FDOxml2cs/AliasReader.cs Normal file
Просмотреть файл

@ -0,0 +1,30 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class AliasReader
{
XmlTextReader xtr;
MimeType mt;
public AliasReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
mt.AliasTypes.Add( xtr.GetAttribute( "type" ) );
}
}
}
}

7
FDOxml2cs/ChangeLog Normal file
Просмотреть файл

@ -0,0 +1,7 @@
2005-06-14 Alexander Olk <xenomorph2@onlinehome.de>
* AliasReader.cs, CommentReader.cs, FDOxml2cs.cs, GlobReader.cs,
MagicReader.cs, MainReader.cs, Match.cs, MatchReader.cs, MatchTypes.cs,
MimeType.cs, MimeTypeReader.cs, MimeUtils.cs, SourceWriter.cs,
SubClassReader.cs, SubMatchReader.cs:
Initial import of the Freedesktop.org mime xml to C# source converter

Просмотреть файл

@ -0,0 +1,58 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class CommentReader
{
XmlTextReader xtr;
MimeType mt;
public CommentReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
bool hasLanguageAttribute = false;
string language = "";
if ( xtr.HasAttributes )
{
hasLanguageAttribute = true;
language = xtr.GetAttribute( "xml:lang" );
}
xtr.Read( );
if ( !hasLanguageAttribute )
{
string comment = xtr.Value;
if ( comment.IndexOf( "\"" ) != -1 )
comment = comment.Replace( "\"", "\\\"" );
mt.Comment = comment;
}
// currently disabled, need to look for some weird chars in the xml comments
// System.Convert ?
// else
// {
// string comment = xtr.Value;
//
// if ( comment.IndexOf ( "\"" ) != -1 )
// comment = comment.Replace( "\"", "\\\"" );
//
// mt.CommentsLanguage.Add( language, comment );
// }
}
}
}

96
FDOxml2cs/FDOxml2cs.cs Normal file
Просмотреть файл

@ -0,0 +1,96 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
// TODO: add command line parameter for output path
using System;
using System.IO;
namespace FDOxml2cs
{
class FDOxml2csMain
{
public static string nameSpace = "System.Windows.Forms";
static void PrintUsage( )
{
string usage =
"The freedesktop.org.xml file can be found at:\nhttp://cvs.freedesktop.org/mime/shared-mime-info/\n\n" +
"Usage: mono FDOxmls2cs.exe <--namespace NameSpace (optional)> <Freedesktop.org mime xml file(s) directory>\n\n" +
"If you do not specify a directory FDOxmls2cs looks in the current working\ndirectory for the xml file(s).\n\n";
Console.WriteLine( usage );
System.Environment.Exit( 0 );
}
static void CheckArgs( string[] args, ref string directory )
{
if ( args[ 0 ] == "--help" )
{
PrintUsage( );
}
if ( args[ 0 ] == "--namespace" )
{
if ( args.Length == 1 )
{
Console.WriteLine( "\nError: Please specify a namespace\n" );
System.Environment.Exit( 0 );
}
nameSpace = args[ 1 ];
if ( args.Length == 3 )
{
directory = args[ 2 ];
}
return;
}
else
{
directory = args[ 0 ];
if ( !Directory.Exists( directory ) )
{
Console.WriteLine( "Directory \"" + directory + "\" not found..." );
System.Environment.Exit( 0 );
}
}
}
[STAThread]
static void Main( string[] args )
{
Console.WriteLine( "FDOxml2cs - Convert Freedesktop.org mime xml files to C# source file." );
Console.WriteLine( "\"mono FDOxml2cs.exe --help\" for help...\n" );
string directory = Directory.GetCurrentDirectory( );
if ( args.Length > 0 )
{
CheckArgs( args, ref directory );
}
MainReader mr = new MainReader( directory );
mr.Start( );
if ( MainReader.MainReaderSuccess )
{
SourceWriter sw = new SourceWriter( );
sw.Start( );
sw.Flush( );
Console.WriteLine( "Success..." );
}
else
Console.WriteLine( "No correct xml file found..." );
}
}
}

Просмотреть файл

@ -0,0 +1,51 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{120B4150-B502-4755-8886-5BC811F45698}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>FDOxml2cs</RootNamespace>
<AssemblyName>FDOxml2cs</AssemblyName>
<WarningLevel>4</WarningLevel>
<StartupObject/>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSHARP.Targets"/>
<ItemGroup>
<Compile Include="AliasReader.cs"/>
<Compile Include="CommentReader.cs"/>
<Compile Include="FDOxml2cs.cs"/>
<Compile Include="GlobReader.cs"/>
<Compile Include="MagicReader.cs"/>
<Compile Include="MainReader.cs"/>
<Compile Include="Match.cs"/>
<Compile Include="MatchReader.cs"/>
<Compile Include="MatchTypes.cs"/>
<Compile Include="MimeType.cs"/>
<Compile Include="MimeTypeReader.cs"/>
<Compile Include="MimeUtils.cs"/>
<Compile Include="SourceWriter.cs"/>
<Compile Include="SubClassReader.cs"/>
<Compile Include="SubMatchReader.cs"/>
</ItemGroup>
</Project>

18
FDOxml2cs/FDOxml2cs.sln Normal file
Просмотреть файл

@ -0,0 +1,18 @@
Microsoft Visual Studio Solution File, Format Version 9.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FDOxml2cs", "FDOxml2cs.csproj", "{120B4150-B502-4755-8886-5BC811F45698}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{120B4150-B502-4755-8886-5BC811F45698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{120B4150-B502-4755-8886-5BC811F45698}.Debug|Any CPU.Build.0 = Debug|Any CPU
{120B4150-B502-4755-8886-5BC811F45698}.Release|Any CPU.ActiveCfg = Release|Any CPU
{120B4150-B502-4755-8886-5BC811F45698}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

34
FDOxml2cs/GlobReader.cs Normal file
Просмотреть файл

@ -0,0 +1,34 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class GlobReader
{
XmlTextReader xtr;
MimeType mt;
public GlobReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
// string pattern = xtr.GetAttribute( "pattern" );
//
// if ( pattern.StartsWith( "*" ) )
// pattern = pattern.Replace( "*", "" );
mt.GlobPatterns.Add( xtr.GetAttribute( "pattern" ) );
}
}
}
}

54
FDOxml2cs/MagicReader.cs Normal file
Просмотреть файл

@ -0,0 +1,54 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class MagicReader
{
XmlTextReader xtr;
MimeType mt;
public MagicReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
mt.MagicPriority = System.Convert.ToInt32( xtr.GetAttribute( "priority" ) );
}
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "match" )
{
Match m = new Match( );
MatchReader mr = new MatchReader( xtr, m );
mr.Start( );
mt.Matches.Add( m );
}
break;
case XmlNodeType.EndElement:
if ( xtr.Name == "magic" )
return;
break;
}
}
}
}
}

111
FDOxml2cs/MainReader.cs Normal file
Просмотреть файл

@ -0,0 +1,111 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.IO;
using System.Xml;
namespace FDOxml2cs
{
public class MainReader
{
public static bool MainReaderSuccess = false;
string path;
XmlTextReader xtr;
public MainReader( string path )
{
this.path = path;
}
public void Start( )
{
DirectoryInfo directoryInfo = new DirectoryInfo( path );
FileInfo[] files = directoryInfo.GetFiles( );
foreach ( FileInfo fi in files )
{
if ( fi.Extension.ToUpper( ) == ".XML" )
{
if ( ReadXMLFile( fi ) )
{
MainReaderSuccess = true;
Console.WriteLine( fi.Name + " success..." );
}
}
}
}
private bool ReadXMLFile( FileInfo fi )
{
xtr = new XmlTextReader( fi.FullName );
if ( !CheckIfMimeFileIsCorrect( ) )
{
xtr.Close( );
Console.WriteLine( fi.Name + " doesn't seem to be a correct freedesktop shared mime info file..." );
return false;
}
Console.WriteLine( fi.Name + " seems to be a correct freedesktop shared mime info file..." );
Console.WriteLine( "Start parsing..." );
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "mime-type" )
{
MimeType mt = new MimeType( );
MimeTypeReader mtr = new MimeTypeReader( xtr, mt );
mtr.Start( );
if ( !MimeUtils.CheckIfMimetypeExists( mt ) )
MimeUtils.mimeTypes.Add( mt );
}
break;
case XmlNodeType.EndElement:
if ( xtr.Name == "mime-info" )
break;
break;
}
}
xtr.Close( );
return true;
}
private bool CheckIfMimeFileIsCorrect( )
{
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "mime-info" )
{
if ( xtr.MoveToFirstAttribute( ) )
{
if ( xtr.Name == "xmlns" )
{
if ( xtr.Value == "http://www.freedesktop.org/standards/shared-mime-info" )
return true;
}
}
}
break;
}
}
return false;
}
}
}

10
FDOxml2cs/Makefile Normal file
Просмотреть файл

@ -0,0 +1,10 @@
all: mono
mono:
mcs -out:FDOxml2cs.exe AliasReader.cs CommentReader.cs FDOxml2cs.cs GlobReader.cs MagicReader.cs MainReader.cs Match.cs MatchReader.cs MatchTypes.cs MimeType.cs MimeTypeReader.cs MimeUtils.cs SourceWriter.cs SubClassReader.cs SubMatchReader.cs /r:System.Xml.dll
dotnet:
csc -out:FDOxml2cs.exe AliasReader.cs CommentReader.cs FDOxml2cs.cs GlobReader.cs MagicReader.cs MainReader.cs Match.cs MatchReader.cs MatchTypes.cs MimeType.cs MimeTypeReader.cs MimeUtils.cs SourceWriter.cs SubClassReader.cs SubMatchReader.cs /r:System.Xml.dll
clean:
rm FDOxml2cs.exe -r -f

89
FDOxml2cs/Match.cs Normal file
Просмотреть файл

@ -0,0 +1,89 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Collections;
namespace FDOxml2cs
{
public class Match
{
string matchvalue;
MatchTypes matchType;
int offset;
int offsetEnd = -1;
string mask = null;
ArrayList subMatches = new ArrayList();
public string MatchValue
{
set {
matchvalue = value;
}
get {
return matchvalue;
}
}
public MatchTypes MatchType
{
set {
matchType = value;
}
get {
return matchType;
}
}
public int Offset
{
set {
offset = value;
}
get {
return offset;
}
}
public string Mask
{
set {
mask = value;
}
get {
return mask;
}
}
public ArrayList Matches
{
set {
subMatches = value;
}
get {
return subMatches;
}
}
public int OffsetEnd
{
set {
offsetEnd = value;
}
get {
return offsetEnd;
}
}
}
}

105
FDOxml2cs/MatchReader.cs Normal file
Просмотреть файл

@ -0,0 +1,105 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class MatchReader
{
XmlTextReader xtr;
Match m;
public MatchReader( XmlTextReader xtr, Match m )
{
this.xtr = xtr;
this.m = m;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
m.MatchValue = xtr.GetAttribute( "value" );
string match_type = xtr.GetAttribute( "type" );
if ( match_type == "string" )
m.MatchType = MatchTypes.TypeString;
else
if ( match_type == "host16" )
m.MatchType = MatchTypes.TypeHost16;
else
if ( match_type == "host32" )
m.MatchType = MatchTypes.TypeHost32;
else
if ( match_type == "big16" )
m.MatchType = MatchTypes.TypeBig16;
else
if ( match_type == "big32" )
m.MatchType = MatchTypes.TypeBig32;
else
if ( match_type == "little16" )
m.MatchType = MatchTypes.TypeLittle16;
else
if ( match_type == "little32" )
m.MatchType = MatchTypes.TypeLittle32;
else
if ( match_type == "byte" )
m.MatchType = MatchTypes.TypeByte;
string offset = xtr.GetAttribute( "offset" );
if ( offset.IndexOf( ":" ) != -1 )
{
string[] split = offset.Split( new char[] { ':' } );
if ( split.Length == 2 )
{
m.Offset = System.Convert.ToInt32( split[ 0 ] );
m.OffsetEnd = System.Convert.ToInt32( split[ 1 ] );
}
}
else
m.Offset = System.Convert.ToInt32( offset );
string mask = xtr.GetAttribute( "mask" );
if ( mask != "" )
m.Mask = mask;
}
if ( xtr.IsEmptyElement )
return;
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "match" )
{
Match nm = new Match( );
SubMatchReader mr = new SubMatchReader( xtr, nm );
mr.Start( );
m.Matches.Add( nm );
}
break;
case XmlNodeType.EndElement:
if ( xtr.Name == "match" )
return;
break;
}
}
}
}
}

17
FDOxml2cs/MatchTypes.cs Normal file
Просмотреть файл

@ -0,0 +1,17 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
namespace FDOxml2cs
{
public enum MatchTypes
{
TypeString,
TypeHost16,
TypeHost32,
TypeBig16,
TypeBig32,
TypeLittle16,
TypeLittle32,
TypeByte
}
}

116
FDOxml2cs/MimeType.cs Normal file
Просмотреть файл

@ -0,0 +1,116 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Collections;
using System.Collections.Specialized;
namespace FDOxml2cs
{
public class MimeType
{
string typeName;
string comment; // default comment
Hashtable commentsLanguage = new Hashtable(); // key: language (de, en); value: comment
int magicPriority = 50;
ArrayList matches = new ArrayList();
StringCollection globPatterns = new StringCollection();
StringCollection subClasses = new StringCollection();
StringCollection aliasTypes = new StringCollection();
public string TypeName
{
set {
typeName = value;
}
get {
return typeName;
}
}
public string Comment
{
set {
comment = value;
}
get {
return comment;
}
}
public Hashtable CommentsLanguage
{
set {
commentsLanguage = value;
}
get {
return commentsLanguage;
}
}
public int MagicPriority
{
set {
magicPriority = value;
}
get {
return magicPriority;
}
}
public ArrayList Matches
{
set {
matches = value;
}
get {
return matches;
}
}
public StringCollection GlobPatterns
{
set {
globPatterns = value;
}
get {
return globPatterns;
}
}
public StringCollection SubClasses
{
set {
subClasses = value;
}
get {
return subClasses;
}
}
public StringCollection AliasTypes
{
set {
aliasTypes = value;
}
get {
return aliasTypes;
}
}
}
}

Просмотреть файл

@ -0,0 +1,77 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class MimeTypeReader
{
XmlTextReader xtr;
MimeType mt;
public MimeTypeReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
mt.TypeName = xtr.GetAttribute( "type" );
}
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "sub-class-of" )
{
SubClassReader scr = new SubClassReader( xtr, mt );
scr.Start( );
}
else
if ( xtr.Name == "comment" )
{
CommentReader cr = new CommentReader( xtr, mt );
cr.Start( );
}
else
if ( xtr.Name == "glob" )
{
GlobReader gr = new GlobReader( xtr, mt );
gr.Start( );
}
else
if ( xtr.Name == "magic" )
{
MagicReader mr = new MagicReader( xtr, mt );
mr.Start( );
}
else
if ( xtr.Name == "alias" )
{
AliasReader ar = new AliasReader( xtr, mt );
ar.Start( );
}
break;
case XmlNodeType.EndElement:
if ( xtr.Name == "mime-type" )
return;
break;
}
}
}
}
}

24
FDOxml2cs/MimeUtils.cs Normal file
Просмотреть файл

@ -0,0 +1,24 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Collections;
namespace FDOxml2cs
{
public class MimeUtils
{
public static ArrayList mimeTypes = new ArrayList();
public static bool CheckIfMimetypeExists( MimeType mt )
{
foreach ( MimeType m in mimeTypes )
{
if ( m.TypeName == mt.TypeName )
return true;
}
return false;
}
}
}

564
FDOxml2cs/SourceWriter.cs Normal file
Просмотреть файл

@ -0,0 +1,564 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Collections;
using System.Collections.Specialized;
namespace FDOxml2cs
{
public class SourceWriter
{
StringCollection outputLines = new StringCollection();
public SourceWriter( )
{
InitOutput( );
}
public StringCollection OutputLines
{
set {
outputLines = value;
}
get {
return outputLines;
}
}
public void Start( )
{
GenerateCode( );
EndOutput( );
}
private void GenerateCode( )
{
GenerateAliases( );
outputLines.Add( "" );
GenerateSubClasses( );
outputLines.Add( "" );
GenerateGlobals( );
outputLines.Add( "" );
outputLines.Add( "\t\t\tMimeType mt = null;" );
outputLines.Add( "" );
GenerateMimeTypes( );
outputLines.Add( "\t\t\tMatch match0 = null;" );
outputLines.Add( "" );
GenerateMatches( );
}
private void GenerateMatches( )
{
foreach ( MimeType mt in MimeUtils.mimeTypes )
{
if ( mt.Matches.Count > 0 )
{
AddMatches( mt.Matches, mt, 0, null );
}
}
}
private void GenerateMimeTypes( )
{
foreach ( MimeType mt in MimeUtils.mimeTypes )
{
outputLines.Add( "\t\t\tmt = new MimeType();" );
outputLines.Add( "\t\t\tmt.Comment = \"" + mt.Comment + "\";" );
foreach ( DictionaryEntry de in mt.CommentsLanguage )
{
outputLines.Add( "\t\t\tmt.CommentsLanguage.Add( \"" + de.Key + "\", \"" + de.Value + "\" );" );
}
outputLines.Add( "\t\t\tMimeTypes.Add( \"" + mt.TypeName + "\", mt );" );
outputLines.Add( "" );
}
}
private void GenerateGlobals( )
{
foreach ( MimeType mt in MimeUtils.mimeTypes )
{
foreach ( string s in mt.GlobPatterns )
{
if ( s.IndexOf( "*" ) == -1 )
{
outputLines.Add( "\t\t\tGlobalLiterals.Add( \"" + s + "\", \"" + mt.TypeName + "\" );" );
}
else
if ( s.IndexOf( "*." ) == -1 )
{
outputLines.Add( "\t\t\tGlobalSufPref.Add( \"" + s + "\", \"" + mt.TypeName + "\" );" );
}
else
{
string gp = s.Replace( "*", "" );
string[] gpsplit = gp.Split( new char[] { '.' } );
if ( gpsplit.Length > 2 )
outputLines.Add( "\t\t\tGlobalPatternsLong.Add( \"" + gp + "\", \"" + mt.TypeName + "\" );" );
else
outputLines.Add( "\t\t\tGlobalPatternsShort.Add( \"" + gp + "\", \"" + mt.TypeName + "\" );" );
}
}
}
}
private void GenerateSubClasses( )
{
foreach ( MimeType mt in MimeUtils.mimeTypes )
{
foreach ( string s in mt.SubClasses )
{
outputLines.Add( "\t\t\tSubClasses.Add( \"" + mt.TypeName + "\", \"" + s + "\" );" );
}
}
}
private void GenerateAliases( )
{
foreach ( MimeType mt in MimeUtils.mimeTypes )
{
foreach ( string s in mt.AliasTypes )
{
outputLines.Add( "\t\t\tAliases.Add( \"" + mt.TypeName + "\", \"" + s + "\" );" );
}
}
}
private void AddMatches( ArrayList matches, MimeType mimeType, int cookie, string lastmatchname )
{
foreach ( Match match in matches )
{
string matchname = "";
string tabs = new String( '\t', 3 + cookie );
if ( cookie == 0 )
{
outputLines.Add( tabs + "match0 = new Match();" );
outputLines.Add( tabs + "match0.MimeType = \"" + mimeType.TypeName + "\";" );
matchname = "match0";
}
else
{
matchname = "match" + cookie;
if ( lastmatchname != null && lastmatchname != matchname )
outputLines.Add( tabs + "Match " + matchname + " = null;" );
lastmatchname = matchname;
outputLines.Add( tabs + matchname + " = new Match();" );
}
if ( cookie == 0 )
outputLines.Add( tabs + matchname + ".Priority = " + mimeType.MagicPriority + ";" );
outputLines.Add( tabs + matchname + ".Offset = " + match.Offset + ";" );
outputLines.Add( tabs + matchname + ".OffsetLength = " + ( match.OffsetEnd == -1 ? "1" : ( match.OffsetEnd - match.Offset ).ToString( ) ) + ";" );
outputLines.Add( tabs + matchname + ".MatchType = MatchTypes." + match.MatchType + ";" );
int word_size = -1;
switch ( match.MatchType )
{
case MatchTypes.TypeHost16:
word_size = 2;
break;
case MatchTypes.TypeHost32:
word_size = 4;
break;
default:
break;
}
if ( word_size != -1 )
{
outputLines.Add( tabs + matchname + ".WordSize = " + word_size + ";" );
}
if ( match.MatchType == MatchTypes.TypeString )
{
byte[] bmatchvalue = MatchValueStringToByteArray( match.MatchValue );
BuildByteArrays( tabs, matchname, bmatchvalue, match );
}
else
{
byte[] bmatchvalue = MatchValueOtherToByteArray( match.MatchValue, match.MatchType );
BuildByteArrays( tabs, matchname, bmatchvalue, match );
}
if ( match.Matches.Count != 0 )
{
cookie++;
outputLines.Add( "" );
outputLines.Add( tabs + "if ( " + matchname + ".Matches.Count > 0 )" );
outputLines.Add( tabs + "{" );
AddMatches( match.Matches, null, cookie, matchname );
outputLines.Add( tabs + "}" );
cookie--;
}
if ( cookie == 0 )
{
if ( mimeType.MagicPriority < 80 )
outputLines.Add( "\t\t\tMatchesBelow80.Add( match0 );" );
else
outputLines.Add( "\t\t\tMatches80Plus.Add( match0 );" );
}
else
outputLines.Add( tabs + "match" + ( cookie - 1 ).ToString( ) + ".Matches.Add( match" + cookie + " );" );
outputLines.Add( "" );
}
}
private void BuildByteArrays( string tabs, string matchname, byte[] bmatchvalue, Match match )
{
AddByteArray( tabs, matchname, bmatchvalue, 0 );
if ( match.Mask != null )
{
byte[] bmask = HexStringToByteArray( match.Mask );
AddByteArray( tabs, matchname, bmask, 1 );
}
}
private void AddByteArray( string tabs, string matchname, byte[] bmatchvalue, int btype ) // 0 == ByteValue, 1 == mask
{
string type_string = ".ByteValue";
if ( btype == 1 )
type_string = ".Mask";
string outs = tabs + matchname + type_string + " = new byte[ " + bmatchvalue.Length + " ] { ";
int counter = 0;
foreach ( byte b in bmatchvalue )
{
if ( counter % 8 == 0 )
{
outs += "\n" + tabs + "\t\t";
}
outs += b.ToString( ) + ", ";
counter++;
}
outs = outs.Remove( outs.Length - 2, 2 );
outs += " };";
outputLines.Add( outs );
}
private byte[] MatchValueOtherToByteArray( string match, MatchTypes matchType )
{
byte[] retval = null;
// hex
if ( match.StartsWith( "0x" ) )
{
retval = HexStringToByteArray( match );
}
else
{
if ( matchType == MatchTypes.TypeByte )
{
retval = new byte[ 1 ];
retval[ 0 ] = System.Convert.ToByte( match );
}
else
if ( matchType == MatchTypes.TypeHost16 || matchType == MatchTypes.TypeLittle16 )
{
retval = new byte[ 2 ];
UInt16 i16 = System.Convert.ToUInt16( OctalStringToInt( match ) );
retval[ 0 ] = System.Convert.ToByte( i16 & 0x00FF );
retval[ 1 ] = System.Convert.ToByte( i16 >> 8 );
}
if ( matchType == MatchTypes.TypeBig16 )
{
retval = new byte[ 2 ];
UInt16 i16 = System.Convert.ToUInt16( OctalStringToInt( match ) );
retval[ 0 ] = System.Convert.ToByte( i16 >> 8 );
retval[ 1 ] = System.Convert.ToByte( i16 & 0x00FF );
}
else
if ( matchType == MatchTypes.TypeHost32 || matchType == MatchTypes.TypeLittle32 )
{
retval = new byte[ 4 ];
UInt32 i32 = OctalStringToInt( match );
retval[ 0 ] = System.Convert.ToByte( i32 & 0x000000FF );
retval[ 1 ] = System.Convert.ToByte( ( i32 & 0x0000FF00 ) >> 8 );
retval[ 2 ] = System.Convert.ToByte( ( i32 & 0x00FF0000 ) >> 16 );
retval[ 3 ] = System.Convert.ToByte( ( i32 & 0xFF000000 ) >> 24 );
}
else
if ( matchType == MatchTypes.TypeBig32 )
{
retval = new byte[ 4 ];
UInt32 i32 = OctalStringToInt( match );
retval[ 0 ] = System.Convert.ToByte( ( i32 & 0xFF000000 ) >> 24 );
retval[ 1 ] = System.Convert.ToByte( ( i32 & 0x00FF0000 ) >> 16 );
retval[ 2 ] = System.Convert.ToByte( ( i32 & 0x0000FF00 ) >> 8 );
retval[ 3 ] = System.Convert.ToByte( i32 & 0x000000FF );
}
}
return retval;
}
private uint OctalStringToInt( string input )
{
if ( input.Length == 0 )
return 0;
int result = input[ 0 ] - '0';
if ( input.Length > 1 )
for ( int i = 1; i < input.Length; i++ )
{
char c = input[ i ];
result = 8 * result + ( c - '0' );
}
return (uint)result;
}
private byte[] HexStringToByteArray( string match )
{
ArrayList bytelist = new ArrayList( );
string match_tmp = match.Replace( "0x", "" );
for ( int i = 0; i < match_tmp.Length; i += 2 )
{
bytelist.Add( System.Convert.ToByte( match_tmp.Substring( i, 2 ), 16 ) );
}
byte[] retval = new byte[ bytelist.Count ];
bytelist.CopyTo( retval );
return retval;
}
private byte[] MatchValueStringToByteArray( string match )
{
ArrayList bytelist = new ArrayList( );
int i = 0;
int l = match.Length;
while ( i < l )
{
char c = match[ i++ ];
if ( c == '\\' )
{
if ( i == l )
{
bytelist.Add( System.Convert.ToByte( '\\' ) );
break;
}
c = match[ i++ ];
switch ( c )
{
case 'n':
bytelist.Add( System.Convert.ToByte( '\n' ) );
break;
case 'r':
bytelist.Add( System.Convert.ToByte( '\r' ) );
break;
case 'b':
bytelist.Add( System.Convert.ToByte( '\b' ) );
break;
case 't':
bytelist.Add( System.Convert.ToByte( '\t' ) );
break;
case 'f':
bytelist.Add( System.Convert.ToByte( '\f' ) );
break;
case 'v':
bytelist.Add( System.Convert.ToByte( '\v' ) );
break;
// octal
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
int result = c - '0';
if ( i < l )
{
c = match[ i++ ];
if ( c >= '0' && c <= '7' )
{
result = 8 * result + ( c - '0' );
if ( i < l )
{
c = match[ i++ ];
if ( c >= '0' && c <= '7' )
{
result = 8 * result + ( c - '0' );
}
else
i--;
}
}
else
i--;
}
bytelist.Add( System.Convert.ToByte( result ) );
break;
// hex
case 'x':
int byte_to_add = System.Convert.ToByte( 'x' );
c = match[ i++ ];
try
{
int tmp = System.Convert.ToByte( c.ToString( ), 16 );
byte_to_add = tmp;
if ( i < l )
{
c = match[ i++ ];
try
{
tmp = System.Convert.ToByte( c.ToString( ), 16 );
byte_to_add = ( byte_to_add << 4 ) + tmp;
}
catch ( System.FormatException )
{
i--;
}
}
}
catch ( System.FormatException )
{
i--;
}
bytelist.Add( System.Convert.ToByte( byte_to_add ) );
break;
default:
bytelist.Add( System.Convert.ToByte( c ) );
break;
}
}
else
bytelist.Add( System.Convert.ToByte( c ) );
}
byte[] retval = new byte[ bytelist.Count ];
bytelist.CopyTo( retval );
return retval;
}
private void InitOutput( )
{
string date = DateTime.Now.ToString( );
string begin_str =
"#region generated code " + date + "\n\n" +
"using System;\n" +
"using System.Collections;\n" +
"using System.Collections.Specialized;\n\n" +
"namespace " + FDOxml2csMain.nameSpace + "\n" +
"{\n" +
"\tinternal struct MimeGenerated\n" +
"\t{\n" +
"\t\tpublic static NameValueCollection Aliases = new NameValueCollection();\n" +
"\t\tpublic static NameValueCollection SubClasses = new NameValueCollection();\n\n" +
"\t\tpublic static NameValueCollection GlobalPatternsShort = new NameValueCollection();\n" +
"\t\tpublic static NameValueCollection GlobalPatternsLong = new NameValueCollection();\n" +
"\t\tpublic static NameValueCollection GlobalLiterals = new NameValueCollection();\n" +
"\t\tpublic static NameValueCollection GlobalSufPref = new NameValueCollection();\n" +
"\t\tpublic static Hashtable MimeTypes = new Hashtable();\n\n" +
"\t\tpublic static ArrayList Matches80Plus = new ArrayList();\n" +
"\t\tpublic static ArrayList MatchesBelow80 = new ArrayList();\n\n" +
"\t\tpublic static void Init()\n" +
"\t\t{\n";
outputLines.Add( begin_str );
}
private void EndOutput( )
{
string end_str =
"\t\t}\n" +
"\t}\n" +
"}\n\n" +
"#endregion\n";
outputLines.Add( end_str );
}
public void Flush( )
{
Console.WriteLine( "Writing \"MimeGenerated.cs\"..." );
System.IO.StreamWriter sw = new System.IO.StreamWriter( "MimeGenerated.cs" );
foreach ( string s in outputLines )
{
sw.WriteLine( s );
}
sw.Close( );
}
}
}

Просмотреть файл

@ -0,0 +1,29 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class SubClassReader
{
XmlTextReader xtr;
MimeType mt;
public SubClassReader( XmlTextReader xtr, MimeType mt )
{
this.xtr = xtr;
this.mt = mt;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
mt.SubClasses.Add( xtr.GetAttribute( "type" ) );
}
}
}
}

105
FDOxml2cs/SubMatchReader.cs Normal file
Просмотреть файл

@ -0,0 +1,105 @@
// Authors:
// Alexander Olk, <xenomorph2@onlinehome.de>
using System;
using System.Xml;
namespace FDOxml2cs
{
public class SubMatchReader
{
XmlTextReader xtr;
Match sm;
public SubMatchReader( XmlTextReader xtr, Match sm )
{
this.xtr = xtr;
this.sm = sm;
}
public void Start( )
{
if ( xtr.HasAttributes )
{
sm.MatchValue = xtr.GetAttribute( "value" );
string match_type = xtr.GetAttribute( "type" );
if ( match_type == "string" )
sm.MatchType = MatchTypes.TypeString;
else
if ( match_type == "host16" )
sm.MatchType = MatchTypes.TypeHost16;
else
if ( match_type == "host32" )
sm.MatchType = MatchTypes.TypeHost32;
else
if ( match_type == "big16" )
sm.MatchType = MatchTypes.TypeBig16;
else
if ( match_type == "big32" )
sm.MatchType = MatchTypes.TypeBig32;
else
if ( match_type == "little16" )
sm.MatchType = MatchTypes.TypeLittle16;
else
if ( match_type == "little32" )
sm.MatchType = MatchTypes.TypeLittle32;
else
if ( match_type == "byte" )
sm.MatchType = MatchTypes.TypeByte;
string offset = xtr.GetAttribute( "offset" );
if ( offset.IndexOf( ":" ) != -1 )
{
string[] split = offset.Split( new char[] { ':' } );
if ( split.Length == 2 )
{
sm.Offset = System.Convert.ToInt32( split[ 0 ] );
sm.OffsetEnd = System.Convert.ToInt32( split[ 1 ] );
}
}
else
sm.Offset = System.Convert.ToInt32( offset );
string mask = xtr.GetAttribute( "mask" );
if ( mask != "" )
sm.Mask = mask;
}
if ( xtr.IsEmptyElement )
return;
while ( xtr.Read( ) )
{
switch ( xtr.NodeType )
{
case XmlNodeType.Element:
if ( xtr.Name == "match" )
{
Match nm = new Match( );
SubMatchReader mr = new SubMatchReader( xtr, nm );
mr.Start( );
sm.Matches.Add( nm );
}
break;
case XmlNodeType.EndElement:
if ( xtr.Name == "match" )
return;
break;
}
}
}
}
}