Friday, January 21, 2011

The best way to validate XML in a unit test?

I have a class with a ToString method that produces XML. I want to unit test it to ensure it is producing valid xml. I have a DTD to validate the XML against.

Should I include the DTD as a string within the unit test to avoid a dependency on it, or is there a smarter way to do this?

  • If your program validates the XML against the DTD during normal execution, then you should just get the DTD from wherever your program will get it.

    If not and the DTD is extremely short (only a few lines), then storing it as a string in your code is probably okay.

    Otherwise, I'd put it in an external file and have your unit test read it from that file.

  • I've used XmlUnit in the past and found it to be useful.

    It can be used to validate XML against a schema or compare your XML against a string. It is clever enough to understand XML's parsing rules. For example it knows that "<e1/>" is equivalent to "<e1></e1>" and can be configured to ignore or include whitespace.

    Vin : Thanks for the XmlUnit link, sounds interesting and helpful
    From Wheelie

HTTP Errors with .Net 3.5 SP1

I have a ASP.Net website that is failing on AJAX postbacks (both with ASP.Net AJAX and a 3rd part control) in IE. FireFox works fine. If I install the website on another machine without .Net 3.5 SP1, it works as expected.

When it fails, Fiddler shows that I'm getting a 405 "Method Not Allowed". The form seems to be posting to pages other than page I'm viewing.

The form's action is "#" for the page on the broken website (with SP1). The form's action is "Default.aspx" for the same page on a website without SP1.

Any ideas?

  • Check out the following Microsoft Knowledge base article:

    http://support.microsoft.com/kb/216493

    If you're using IIS4 or IIS5 this may be the problem.

    From Bork Blatt
  • SP1 changes the HtmlForm control so that it honors the action attribute, where previous versions ignored it. It sounds like you have something like this on the broken pages:

    <form runat="server" action="#">
    

    Remove the action, and it should be fine:

    <form runat="server">
    

    More info here: http://forums.asp.net/t/1305800.aspx

    From Dave Ward

SpecialCells in VSTO

I'm trying to use the SpecialCells method in a VSTO project using c# against the 3.5 framework and Excel2007.

Here's my code:

Excel.Worksheet myWs = (Excel.Worksheet)ModelWb.Worksheets[1];

Range myRange = myWs.get_Range("A7", "A800");

//Range rAccounts = myRange.SpecialCells(XlCellType.xlCellTypeConstants, XlSpecialCellsValue.xlTextValues);

Range rAccounts = myWs.Cells.SpecialCells(XlCellType.xlCellTypeConstants, XlSpecialCellsValue.xlTextValues);

When I run this, it throws an exception... System.Exception._COMPlusExceptionCode with a value of -532459699

Note that I get the same exception if I switch(uncomment one and comment the other) the above Range rAccounts line.

  • I figured it out... the worksheet was protected!

    myWs.Unprotect(Properties.Settings.Default.PasswordSheet);
    

    fixes it...for those playing along at home...don't forget to protect the sheet when you're done.

    myWs.Protect(Properties.Settings.Default.PasswordSheet, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    
    From w4ik

JavaScript culture sensitive currency formatting

How can i format currency related data in a manner that is culture aware in JavaScript?

  • Dojo has a currency formatter that's locale aware.

    If you don't want to include Dojo in your project just for this function, then perhaps you can localize the currency in your back-end?

    georgebrock : I believe the Dojo currecy formatting is based on XML data from the Unicode Common Locale Data Repository (http://unicode.org/cldr). While Dojo will do the hard work for you, if you don't want to use it you can get the raw data from there.
    From sherbang
  • Since I'm using ASP.NET 3.5 is there an equivalent in Microsoft's Ajax Framework?

  • there is a Number.localeFormat function but I'm not sure it's what your after

    http://msdn.microsoft.com/en-gb/library/bb310813.aspx

    Jan Willem B : this is an ASP.NET extension function, not native JS

create and stream large XML document in C++

I have some code that creates a fairly large xml DOM and writes it off to a file (up to 50-100MB) . It basically creates the DOM and then calls a toString on it and writes it out with ofstream. Is there a way to get streaming output of the generated dom so that it doesn't create the whole structure in memory all at once and then copy it, etc? I will not modify any node after i create it so it can write it out and free up the memory right away. I could write my own xml class that does the xml construction but ... i don't think that's a good idea since i'll probably miss something when it comes down to escaping etc.

  • Anytime you're working with large files like this it's probably best to use a memory-mapped file. Create your DOM elements in mapped memory and then commit.

    From James D
  • I would recommend GenX as a streaming XML writer, I use this in Programmer's Notepad and it works a treat, you can see examples of use in the source code. Extremely fast, and it produces good UTF-8 XML. Memory usage while you use it should remain roughly constant.

  • Ok, turns out libxml2 has a streaming API:

    http://xmlsoft.org/examples/testWriter.c

    It's a little old style (very C-ish) but you can write your wrapper around it.

    From
  • Look under keyword "C++ XML writer;" XML writers generate XML to file without building the entire DOM in memory so they don't need to use very much memory at all. You didn't mention platform, but Microsoft XmlLite has IXmlWriter.

    From Ben Bryant

Thursday, January 20, 2011

How can I start Fedora Directory Service with SELinux enabled?

I just did a fresh base install of fedora 12, and did a yum install 389-ds. I went through the included setup script (setup-ds-admin.pl) and everything started fine and was working normally. I could access the directory server and login using the directory manager account created during the setup.

After a reboot I tried starting the dirsrv service using the following command:

[root@test-ds ~]# /etc/init.d/dirsrv-admin start
Starting dirsrv-admin:                                  [  OK  ]
[root@test-ds ~]# /etc/init.d/dirsrv start
Starting dirsrv: 
test-ds...
[26/Feb/2010:14:59:11 -0500] dse - The entry cn=config in file
/etc/dirsrv/slapd-test-ds/dse.ldif is invalid, error code 53
(DSA is unwilling to perform) - nsslapd-errorlog-mode: Failed to chmod
error log file to 600: errno 1 (Operation not permitted)

[26/Feb/2010:14:59:11 -0500] dse - Could not load config file [dse.ldif]
[26/Feb/2010:14:59:11 -0500] dse - Please edit the file to correct the
reported problems and then restart the server.
                                                           [FAILED]
  *** Warning: 1 instance(s) failed to start

If I turn off SELinux with "setenforce 0", it can start without any issue. There are no entries generated in /var/log/audit/audit.log like I'd normally see for an SELinux error but it's infinitely repeatable turning SELinux on and off with setenforce.

  • This is a known bug.

    Try updating your SELinux Policy to minimum 3.6.32-59.

    TrueDuality : Thanks that was it! I tried searching for known bugs but I didn't stumble across this one because I assumed it would still be marked as 'open'. Oh well, Thanks again!
    From Studer

How to convert a really big HTML file to PDF in Windows

We have a few really large HTML files (60-100 MB) that we cannot convert to PDF with any reliability.

Adobe Acrobat 9 crashes - hits the 2GB limit for applications.

Open Office converts, but removes some of the anchors ().

ActivePDF webgrabber crashes.

Is using a 64 bit situation an option for this type of thing?

I see a bunch of options out there, but can they do better than Adobe Acrobat 9 itself?

  • You could try FoxIt's PDF creator. It's only $30 and they have a trial so you can see if it will do the job. Their reader is way better than Adobe's in my opinion so I would imagine that their writer is based on the same engine.

    Other free options that you could try are pdfcreator or pdfill.

    PeterStrange : Thanks, I tried it and after 20 hours it did the job from IE. However, no links or named destinations were created so that is a no go.
  • Depending on the use case and if you could even display the HTML code in a viewer, you might think about PrimoPDF. "Print-to-PDF" technologies might not necessarily be ideal but could lessen the size burden.

    From Mikey B
  • http://sourceforge.net/projects/pdfcreator/ for free

    From raerek
  • Know anyone with a Mac? if so then get them to open it with Preview and print to a PDF.

    From Chopper3
  • Why are the HTML files so large - are they files you obtain from a third party, or are they generated by something inside you organisation? Could you write a script to split the HTML files up in to sections? Do they have links to images, could your script reduce the quality of the images to reduce the file size?

    PeterStrange : It's just a large document. I could split it up but then the interlinking would be a problem. No images to speak of. :(
    David Hicks : Write a script to convert the HTML to PDF yourself, using something like Python and ReportLab?
  • I've printed pretty lengthy web pages to PDF using PDF995. Certainly not as large as you're talking about, but it worked very smoothly for me.

    PeterStrange : Thanks, I'm trying this one next
    From hometoast
  • The only way I could solve this problem was to convert pieces of HTML to PDF, then merge them in Adobe Acrobat 9.

    Thanks for your suggestions. Really interesting learning about all these neat tools that are out there.

  • I know this might be slightly heretical.... could you: copy and paste it into word then use word to fashion the html document. Then use word again to save the document as pdf? Just an out of the box idea.

    PeterStrange : No go. Too big for Word, apparently!
    From lilott8
  • If you are having this problem, try using HTMLTidy to clean up the HTML. That got the size down by a lot and made things easier to work with!

  • I wonder if you could use Winnovative's HTML to PDF converter?

    From JMan
  • Have you tried wkhtmltopdf? Its a command line utility that is super easy to use wkhtmltopdf:

    Install wkhtmltopdf
    Go to Start -> Run -> cmd

    cd %Program Files%\wkhtmltopdf                     [press enter]
    wkhtmltopdf.exe http://www.google.com google.pdf   [press enter]
    

    viola. google.com saved to google.pdf.

    If I remember correctly it does fairly well with its PDF compression