Register  Login       Search  
Forum  
 
  
 
Forum  Forum     
 
SearchForum Home
  XML Lab Products  nxslt  Two little docu...
 Two little document enhancements
 
Karijn
4 posts
www.berserker.nl
Joined
3/3/2006

Two little document enhancements
Posted: 03 Mar 06 2:00 PM (Netherlands)

Hi,

 

First of all I want to thank XmlLab for the nxslt2 too, great.

 

I’ve made two little document enhancements that i would like to share.

 

1) There is a little bug in the href attribute, namely, you can’t combine a combination of parameters, variables and constants the it. This comes because the href value is setted to the last part set to is. i.e. when you try something like href="{$prefix}{@Name}.cs", the href will be “.cs”.

2) I want to use nxslt2 for code generation, this means that I would like to generate files only once (optionally of coarse), therefore I introduced a ‘writeonce’ attribute.

 

We now can use the document function as follows:

<xsl:param name="prefix" />         <!— value = ‘c:\xxx\’ -->

<xsl:template match="Table">

<exsl:document href="{$prefix}{@Name}.cs" method="text" encoding="ascii" writeonce="true" >//      <xsl:value-of select="@Name"/>.cs

 

to create “C:\xxx\Person.cs

 

 

In MultiXmlTextWriter.cs

 

public override void WriteString(string text)

{

  //Possible exsl:document's attribute value

  if (redirectState == RedirectState.WritingRedirectElementAttrValue)

  {

    switch (currentAttributeName)

    {

      case "href":

        // add multiple parts to the href.

        // href will be cleaned after the creation of the output file.

        state.Href += text;

        break;

      case "writeonce":

        // new writeonce attribute

        state.WriteOnce = text.ToLower() == "true";

        break;

            

 

In OutputState.cs

// replace the InitWriter

public void InitWriter()

{

  // Save current directory

  storedDir = Directory.GetCurrentDirectory();

  FileInfo file = new FileInfo(href);

  DirectoryInfo dir = Directory.GetParent(href);

  if (!dir.Exists)

  {

    dir.Create();

  }

  // Create writer

  if (method == OutputMethod.Xml)

  {

    if (file.Exists && writeOnce)

    {

      // dummy writer

      xmlWriter = new XmlTextWriter(new MemoryStream(), encoding);

    }

    else

    {

      xmlWriter = new XmlTextWriter(href, encoding);

      if (indent)

      {

        xmlWriter.Formatting = Formatting.Indented;

      }

      if (!omitXmlDecl)

      {

        if (standalone)

        {

          xmlWriter.WriteStartDocument(true);

        }

        else

        {

          xmlWriter.WriteStartDocument();

        }

      }

    }

  }

  else

  {

    if (file.Exists && writeOnce)

    {

      // dummy writer

      textWriter = new StreamWriter(new MemoryStream(), encoding);

    }

    else

    {

      textWriter = new StreamWriter(href, false, encoding);

    }

  }

  href = "";         // clean the href for the next usage

  writeOnce = false; // clean the writeonce flage for the next usage

  // Set new current directory           

  Directory.SetCurrentDirectory(dir.ToString());

}

 

// Add the next members

bool writeOnce = false;

public bool WriteOnce

{

  get

  {

    return writeOnce;

  }

  set

  {

    writeOnce = value;

  }

}

 

 

Karijn
4 posts
www.berserker.nl
Joined
3/3/2006

Re: Two little document enhancements
Posted: 04 Mar 06 4:22 AM (Netherlands) Modified By Karijn  on 3/5/2006 8:32:02 AM)

I Later desided I could use a BogusStream in stead of a MemoryStream, Just derive a class from Stream, remove the exception throwings, let it be capable of writing (not reading and seeking) and forget about the written data. This way it will work faster and it will use less memory.

 

Bye,

 

Karijn

Karijn
4 posts
www.berserker.nl
Joined
3/3/2006

Re: Two little document enhancements
Posted: 07 Mar 06 10:55 AM (Netherlands)

Oops, now I'm the one that makes bugs.

in WriteString(string text)

we shouldn't add the line

     href = "";

 

just remove it and it should work correctly.

 

Bye again.

 

PS. can somebody test this?

olegt
85 posts
www.xmllab.net
Joined
2/25/2005

Re: Two little document enhancements
Posted: 11 Jul 06 2:24 AM (Israel)
Thanks for your contribution! I'll include these changes into the next version.

Oleg Tkachenko, Microsoft MVP for XML, MCPD
http://www.XmlLab.Net | http://blog.tkachenko.com
  XML Lab Products  nxslt  Two little docu...
Forum Home  Search