@ -41,9 +41,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
bool intoAttribute = false ;
bool intoAttribute = false ;
bool initialized ;
bool initialized ;
bool _ eof ;
bool _ eof ;
bool isPartialDefKeysClosed = true ;
bool isDefKeysClosed = true ;
#region Context
#region Context
Stack < ReaderContext > layer = new Stack < ReaderContext > ( ) ;
Stack < ReaderContext > layer = new Stack < ReaderContext > ( ) ;
@ -79,13 +76,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
List < KeyMapping > keys = new List < KeyMapping > ( ) ;
List < KeyMapping > keys = new List < KeyMapping > ( ) ;
KeyMapping LastKey {
KeyMapping LastKey {
get {
get { return keys . LastOrDefault ( ) ; }
KeyMapping last = keys . LastOrDefault ( ) ;
if ( last = = null )
keys . Add ( last = new KeyMapping ( ) ) ;
return last ;
}
}
}
void LayerPop ( )
void LayerPop ( )
@ -112,11 +103,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml" ;
public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml" ;
public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" ;
public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" ;
public XmlBamlReader ( Stream stream ) : this ( stream , AppDomainTypeResolver . GetIntoNewAppDomain ( Environment . CurrentDirectory ) )
{
}
public XmlBamlReader ( Stream stream , ITypeResolver resolver )
public XmlBamlReader ( Stream stream , ITypeResolver resolver )
{
{
if ( stream = = null )
if ( stream = = null )
@ -339,7 +325,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
else
else
currentType = ( BamlRecordType ) type ;
currentType = ( BamlRecordType ) type ;
if ( currentType . ToString ( ) . EndsWith ( "End" ) )
Debug . Unindent ( ) ;
Debug . WriteLine ( string . Format ( "{0} (0x{0:x})" , currentType ) ) ;
Debug . WriteLine ( string . Format ( "{0} (0x{0:x})" , currentType ) ) ;
if ( currentType . ToString ( ) . EndsWith ( "Start" ) )
Debug . Indent ( ) ;
}
}
bool SetNextNode ( )
bool SetNextNode ( )
@ -402,6 +392,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
break ;
break ;
case BamlRecordType . DeferableContentStart :
case BamlRecordType . DeferableContentStart :
Current . IsDeferred = true ;
Current . IsDeferred = true ;
keys = new List < KeyMapping > ( ) ;
currentKey = 0 ;
reader . ReadInt32 ( ) ;
reader . ReadInt32 ( ) ;
break ;
break ;
case BamlRecordType . DefAttribute :
case BamlRecordType . DefAttribute :
@ -637,9 +629,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
{
{
get
get
{
{
if ( intoAttribute ) return S tring. Empty ;
if ( intoAttribute ) return s tring. Empty ;
String localName = S tring. Empty ;
String localName = s tring. Empty ;
XmlBamlNode node = this . CurrentNode ;
XmlBamlNode node = this . CurrentNode ;
if ( node is XmlBamlSimpleProperty ) {
if ( node is XmlBamlSimpleProperty ) {
@ -689,13 +681,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
object GetResourceName ( short identifier )
object GetResourceName ( short identifier )
{
{
if ( identifier > = 0 )
if ( identifier > = 0 ) {
{
PropertyDeclaration declaration = this . propertyTable [ identifier ] ;
PropertyDeclaration declaration = this . propertyTable [ identifier ] ;
return declaration ;
return declaration ;
}
} else {
else
{
identifier = ( short ) - identifier ;
identifier = ( short ) - identifier ;
bool isNotKey = ( identifier > 0xe8 ) ;
bool isNotKey = ( identifier > 0xe8 ) ;
if ( isNotKey )
if ( isNotKey )
@ -998,6 +987,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
bool sharedSet = reader . ReadBoolean ( ) ;
bool sharedSet = reader . ReadBoolean ( ) ;
string text = this . stringTable [ stringId ] ;
string text = this . stringTable [ stringId ] ;
Debug . Print ( "KeyString: " + text ) ;
if ( text = = null )
if ( text = = null )
throw new NotSupportedException ( ) ;
throw new NotSupportedException ( ) ;
@ -1039,6 +1029,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
void ReadElementEnd ( )
void ReadElementEnd ( )
{
{
CloseElement ( ) ;
CloseElement ( ) ;
if ( Current . IsDeferred )
keys = null ;
LayerPop ( ) ;
LayerPop ( ) ;
}
}
@ -1095,12 +1087,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
// this property could be a markup extension
// this property could be a markup extension
// try to convert it
// try to convert it
int start = nodes . IndexOf ( propertyElement ) + 1 ;
int start = nodes . IndexOf ( propertyElement ) + 1 ;
IEnumerator enumerator = nodes . GetEnumerator ( ) ;
IEnumerator < XmlBamlNode > enumerator = nodes . GetEnumerator ( ) ;
// move enumerator to the start of this property value
// move enumerator to the start of this property value
for ( int i = 0 ; i < start & & enumerator . MoveNext ( ) ; i + + ) ;
for ( int i = 0 ; i < start & & enumerator . MoveNext ( ) ; i + + ) ;
if ( IsExtension ( enumerator ) ) {
if ( IsExtension ( enumerator ) & & start < nodes . Count - 1 ) {
start - - ;
start - - ;
nodes . RemoveAt ( start ) ;
nodes . RemoveAt ( start ) ;
nodes . RemoveLast ( ) ;
nodes . RemoveLast ( ) ;
@ -1173,11 +1165,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
}
}
}
}
bool IsExtension ( IEnumerator enumerator )
bool IsExtension ( IEnumerator < XmlBamlNode > enumerator )
{
{
while ( enumerator . MoveNext ( ) ) {
while ( enumerator . MoveNext ( ) ) {
object node = enumerator . Current ;
var node = enumerator . Current ;
if ( node is XmlBamlElement & & ! ( node is XmlBamlEndElement ) & & ! ( ( XmlBamlElement ) node ) . TypeDeclaration . IsExtension )
if ( node . NodeType = = XmlNodeType . Element & & ! ( ( XmlBamlElement ) node ) . TypeDeclaration . IsExtension )
return false ;
return false ;
}
}
@ -1231,7 +1223,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
nodes . Enqueue ( element ) ;
nodes . Enqueue ( element ) ;
if ( oldDeclaration ! = null ) {
if ( oldDeclaration ! = null ) {
nodes . Enqueue ( new XmlBamlSimpleProperty ( XWPFNamespace , "Class" , string . Format ( "{0}.{1}" , oldDeclaration . Namespace , oldDeclaration . Name ) ) ) ;
nodes . Enqueue ( new XmlBamlSimpleProperty ( XWPFNamespace , "Class" , oldDeclaration . FullyQualifiedName . Replace ( '+' , '.' ) ) ) ;
}
}
if ( parentElement ! = null & & complexPropertyOpened = = 0 & & ! Current . IsInStaticResource & & Current . Previous . IsDeferred ) {
if ( parentElement ! = null & & complexPropertyOpened = = 0 & & ! Current . IsInStaticResource & & Current . Previous . IsDeferred ) {
@ -1342,9 +1334,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
CloseElement ( ) ;
CloseElement ( ) ;
complexPropertyOpened - - ;
complexPropertyOpened - - ;
if ( complexPropertyOpened = = 0 )
if ( complexPropertyOpened = = 0 ) {
{
int start = nodes . IndexOf ( propertyElement ) ;
int start = nodes . IndexOf ( propertyElement ) ;
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
@ -1409,27 +1399,26 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
void ReadOptimizedStaticResource ( )
void ReadOptimizedStaticResource ( )
{
{
byte num = reader . ReadByte ( ) ;
byte flags = reader . ReadByte ( ) ;
short typeIdentifier = reader . ReadInt16 ( ) ;
short typeIdentifier = reader . ReadInt16 ( ) ;
bool isValueType = ( num & 1 ) = = 1 ;
bool isValueType = ( flags & 1 ) = = 1 ;
bool isStaticType = ( num & 2 ) = = 2 ;
bool isStaticType = ( flags & 2 ) = = 2 ;
object resource ;
object resource ;
if ( isValueType )
if ( isValueType )
resource = this . GetTypeExtension ( typeIdentifier ) ;
resource = GetTypeExtension ( typeIdentifier ) ;
else if ( isStaticType )
else if ( isStaticType ) {
{
object name = GetResourceName ( typeIdentifier ) ;
ResourceName resourceName = ( ResourceName ) this . GetResourceName ( typeIdentifier ) ;
if ( name is ResourceName )
resource = GetStaticExtension ( resourceName . Name ) ;
resource = GetStaticExtension ( ( ( ResourceName ) name ) . Name ) ;
}
else if ( name is PropertyDeclaration )
else
resource = GetStaticExtension ( FormatPropertyDeclaration ( ( ( PropertyDeclaration ) name ) , true , false , false ) ) ;
{
else
throw new InvalidOperationException ( "Invalid resource: " + name . GetType ( ) ) ;
} else {
resource = this . stringTable [ typeIdentifier ] ;
resource = this . stringTable [ typeIdentifier ] ;
}
}
//this.staticResourceTable.Add(resource);
isPartialDefKeysClosed = true ;
// Aggiungo la risorsa nell'ultimo gruppo
LastKey . StaticResources . Add ( resource ) ;
LastKey . StaticResources . Add ( resource ) ;
}
}
@ -1474,8 +1463,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return String . Format ( "{0}:{1}" , prefix , name ) ;
return String . Format ( "{0}:{1}" , prefix , name ) ;
}
}
string FormatPropertyDeclaration ( PropertyDeclaration propertyDeclaration , bool withPrefix , bool useReading , bool checkType )
string FormatPropertyDeclaration ( PropertyDeclaration propertyDeclaration , bool withPrefix , bool useReading , bool checkType )
{
{
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
@ -1511,11 +1498,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
void ReadPropertyWithStaticResourceIdentifier ( )
void ReadPropertyWithStaticResourceIdentifier ( )
{
{
short identifier = reader . ReadInt16 ( ) ;
short propertyId = reader . ReadInt16 ( ) ;
short staticIdentifier = reader . ReadInt16 ( ) ;
short index = reader . ReadInt16 ( ) ;
PropertyDeclaration pd = this . GetPropertyDeclaration ( identifier ) ;
PropertyDeclaration pd = this . GetPropertyDeclaration ( propertyId ) ;
object staticResource = GetStaticResource ( staticIdentifier ) ;
object staticResource = GetStaticResource ( index ) ;
string prefix = this . LookupPrefix ( XmlPIMapping . PresentationNamespace , false ) ;
string prefix = this . LookupPrefix ( XmlPIMapping . PresentationNamespace , false ) ;
string value = String . Format ( "{{{0}{1}StaticResource {2}}}" , prefix , ( String . IsNullOrEmpty ( prefix ) ) ? String . Empty : ":" , staticResource ) ;
string value = String . Format ( "{{{0}{1}StaticResource {2}}}" , prefix , ( String . IsNullOrEmpty ( prefix ) ) ? String . Empty : ":" , staticResource ) ;
@ -1528,10 +1515,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
object GetStaticResource ( short identifier )
object GetStaticResource ( short identifier )
{
{
if ( identifier < LastKey . StaticResources . Count )
if ( identifier < keys [ currentKey - 1 ] . StaticResources . Count )
return LastKey . StaticResources [ ( int ) identifier ] ;
return keys [ currentKey - 1 ] . StaticResources [ ( int ) identifier ] ;
// return "???" + identifier +"???";
// return "???" + identifier + "???";
throw new ArgumentException ( "Cannot find StaticResource" , "identifier" ) ;
throw new ArgumentException ( "Cannot find StaticResource" , "identifier" ) ;
}
}
@ -1595,11 +1582,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
TypeDeclaration GetKnownTypeDeclarationByName ( string name )
TypeDeclaration GetKnownTypeDeclarationByName ( string name )
{
{
foreach ( var type in KnownInfo . KnownTypeTable ) {
foreach ( var type in KnownInfo . KnownTypeTable ) {
if ( name = = string . Format ( "{0}.{1}, {2}" , type . Namespace , type . Name , type . Assembly ) )
if ( name = = type . AssemblyQualifiedName )
return type ;
return type ;
}
}
throw new NotSupportedException ( ) ;
throw new NotSupportedException ( "Type '" + name + "' not found!" ) ;
}
}
internal string GetAssembly ( short identifier )
internal string GetAssembly ( short identifier )
@ -1607,26 +1594,18 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
return this . assemblyTable [ identifier ] ;
return this . assemblyTable [ identifier ] ;
}
}
XmlBamlNode CurrentNode
XmlBamlNode CurrentNode {
{
get { return _ currentNode ; }
get
{
return _ currentNode ;
}
}
}
///<summary>
///<summary>
///When overridden in a derived class, gets the namespace URI (as defined in the W3C Namespace specification) of the node on which the reader is positioned.
///When overridden in a derived class, gets the namespace URI (as defined in the W3C Namespace specification) of the node on which the reader is positioned.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The namespace URI of the current node; otherwise an empty string.
///The namespace URI of the current node; otherwise an empty string.
///</returns>
///</returns>
///
public override string NamespaceURI {
public override string NamespaceURI
get {
{
get
{
if ( intoAttribute ) return String . Empty ;
if ( intoAttribute ) return String . Empty ;
TypeDeclaration declaration ;
TypeDeclaration declaration ;
@ -1664,11 +1643,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets the namespace prefix associated with the current node.
///When overridden in a derived class, gets the namespace prefix associated with the current node.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The namespace prefix associated with the current node.
///The namespace prefix associated with the current node.
///</returns>
///</returns>
///
public override string Prefix
public override string Prefix
{
{
get
get
@ -1682,11 +1659,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets a value indicating whether the current node can have a <see cref="P:System.Xml.XmlReader.Value"></see>.
///When overridden in a derived class, gets a value indicating whether the current node can have a <see cref="P:System.Xml.XmlReader.Value"></see>.
///</summary>
///</summary>
///
///<returns>
///<returns>
///true if the node on which the reader is currently positioned can have a Value; otherwise, false. If false, the node has a value of String.Empty.
///true if the node on which the reader is currently positioned can have a Value; otherwise, false. If false, the node has a value of String.Empty.
///</returns>
///</returns>
///
public override bool HasValue
public override bool HasValue
{
{
get { return this . Value ! = null ; }
get { return this . Value ! = null ; }
@ -1703,11 +1678,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets the text value of the current node.
///When overridden in a derived class, gets the text value of the current node.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The value returned depends on the <see cref="P:System.Xml.XmlReader.NodeType"></see> of the node. The following table lists node types that have a value to return. All other node types return String.Empty.Node type Value AttributeThe value of the attribute. CDATAThe content of the CDATA section. CommentThe content of the comment. DocumentTypeThe internal subset. ProcessingInstructionThe entire content, excluding the target. SignificantWhitespaceThe white space between markup in a mixed content model. TextThe content of the text node. WhitespaceThe white space between markup. XmlDeclarationThe content of the declaration.
///The value returned depends on the <see cref="P:System.Xml.XmlReader.NodeType"></see> of the node. The following table lists node types that have a value to return. All other node types return String.Empty.Node type Value AttributeThe value of the attribute. CDATAThe content of the CDATA section. CommentThe content of the comment. DocumentTypeThe internal subset. ProcessingInstructionThe entire content, excluding the target. SignificantWhitespaceThe white space between markup in a mixed content model. TextThe content of the text node. WhitespaceThe white space between markup. XmlDeclarationThe content of the declaration.
///</returns>
///</returns>
///
public override string Value
public override string Value
{
{
get
get
@ -1737,11 +1710,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets the depth of the current node in the XML document.
///When overridden in a derived class, gets the depth of the current node in the XML document.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The depth of the current node in the XML document.
///The depth of the current node in the XML document.
///</returns>
///</returns>
///
public override int Depth
public override int Depth
{
{
get { return this . readingElements . Count ; }
get { return this . readingElements . Count ; }
@ -1750,11 +1721,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets the base URI of the current node.
///When overridden in a derived class, gets the base URI of the current node.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The base URI of the current node.
///The base URI of the current node.
///</returns>
///</returns>
///
public override string BaseURI
public override string BaseURI
{
{
get { return String . Empty ; }
get { return String . Empty ; }
@ -1763,62 +1732,42 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets a value indicating whether the current node is an empty element (for example, <MyElement/>).
///When overridden in a derived class, gets a value indicating whether the current node is an empty element (for example, <MyElement/>).
///</summary>
///</summary>
///
///<returns>
///<returns>
///true if the current node is an element (<see cref="P:System.Xml.XmlReader.NodeType"></see> equals XmlNodeType.Element) that ends with />; otherwise, false.
///true if the current node is an element (<see cref="P:System.Xml.XmlReader.NodeType"></see> equals XmlNodeType.Element) that ends with />; otherwise, false.
///</returns>
///</returns>
///
public override bool IsEmptyElement
public override bool IsEmptyElement
{
{
get { return false ; }
get { return false ; }
}
}
//public override bool IsDefault
//{
// get
// {
// return this.NamespaceURI == null;
// }
//}
///<summary>
///<summary>
///When overridden in a derived class, gets the number of attributes on the current node.
///When overridden in a derived class, gets the number of attributes on the current node.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The number of attributes on the current node.
///The number of attributes on the current node.
///</returns>
///</returns>
///
public override int AttributeCount {
public override int AttributeCount
{
get { throw new NotImplementedException ( ) ; }
get { throw new NotImplementedException ( ) ; }
}
}
///<summary>
///<summary>
///When overridden in a derived class, gets a value indicating whether the reader is positioned at the end of the stream.
///When overridden in a derived class, gets a value indicating whether the reader is positioned at the end of the stream.
///</summary>
///</summary>
///
///<returns>
///<returns>
///true if the reader is positioned at the end of the stream; otherwise, false.
///true if the reader is positioned at the end of the stream; otherwise, false.
///</returns>
///</returns>
///
public override bool EOF {
public override bool EOF
{
get { return _ eof ; }
get { return _ eof ; }
}
}
///<summary>
///<summary>
///When overridden in a derived class, gets the state of the reader.
///When overridden in a derived class, gets the state of the reader.
///</summary>
///</summary>
///
///<returns>
///<returns>
///One of the <see cref="T:System.Xml.ReadState"></see> values.
///One of the <see cref="T:System.Xml.ReadState"></see> values.
///</returns>
///</returns>
///
public override ReadState ReadState {
public override ReadState ReadState
get {
{
get
{
if ( ! initialized )
if ( ! initialized )
return ReadState . Initial ;
return ReadState . Initial ;
else if ( reader = = null )
else if ( reader = = null )
@ -1838,11 +1787,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
///<summary>
///<summary>
///When overridden in a derived class, gets the <see cref="T:System.Xml.XmlNameTable"></see> associated with this implementation.
///When overridden in a derived class, gets the <see cref="T:System.Xml.XmlNameTable"></see> associated with this implementation.
///</summary>
///</summary>
///
///<returns>
///<returns>
///The XmlNameTable enabling you to get the atomized version of a string within the node.
///The XmlNameTable enabling you to get the atomized version of a string within the node.
///</returns>
///</returns>
///
public override XmlNameTable NameTable
public override XmlNameTable NameTable
{
{
get { return _ nameTable ; }
get { return _ nameTable ; }
@ -1938,60 +1885,5 @@ namespace Ricciolo.StylesExplorer.MarkupReflection
}
}
#endregion
#endregion
#region NodesCollection
internal class NodesCollection : List < XmlBamlNode >
{
public XmlBamlNode Last
{
get
{
if ( this . Count > 0 )
{
int i = this . Count - 1 ;
return this [ i ] ;
}
return null ;
}
}
public void RemoveLast ( )
{
if ( this . Count > 0 )
this . Remove ( this . Last ) ;
}
public XmlBamlNode Dequeue ( )
{
return DequeueInternal ( true ) ;
}
public XmlBamlNode Peek ( )
{
return DequeueInternal ( false ) ;
}
XmlBamlNode DequeueInternal ( bool remove )
{
if ( this . Count > 0 )
{
XmlBamlNode node = this [ 0 ] ;
if ( remove )
this . RemoveAt ( 0 ) ;
return node ;
}
else
return null ;
}
public void Enqueue ( XmlBamlNode node )
{
this . Add ( node ) ;
}
}
#endregion
}
}
}
}