Saturday, November 19, 2011

.NET XSLT Extending Functionalities

I needed to match strings using regular expression.  In XPath 2.0, there's a function called fn:matches.  XSLT in .NET does not support XPath 2.0.  To work around this limitation, I had to implement my own XSLT extension.

There are two ways to do this:

1.  Create the extension class XPathFunctionExtension to implement the logic.

    XsltArgumentList args = new XsltArgumentList();
    args.AddExtensionObject("urn:XPathFunctionExtension", new XPathFunctionExtension());

   In the XSL stylesheet,  add the namespace xmlns:ext="urn:XPathFunctionExtension"

   Use the function, ext:Matches(text, pattern)

2.  The second way is through scripting to implement the logic right on the the XSL stylesheet.

   <xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt"
   xmlns:ext="urn:XPathFunctionExtension"

  <msxsl:script language="C#" implements-prefix="ext">
    <![CDATA[
        public bool Matches(string input, string pattern)
        {
            return Regex.IsMatch(input, pattern);
        }
      ]]>
  </msxsl:script>

That's it!  Gotta love regular expressions!! :)

Oh yeah, remember to enable scripting:  new XsltSettings(true, true);
The second argument is the "enableScript" and it needs to be true;
Otherwise, you would get this error:  Execution of scripts was prohibited. Use the XsltSettings.EnableScript property to enable it.

Friday, November 18, 2011

Apache POI-XSSF

Set background color of a cell:

CellStyle result = workbook.createCellStyle();
result.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
result.setFillPattern(CellStyle.SOLID_FOREGROUND);


Merge some cells together:


/**
 * Merge the cells into one
 *
 * @param sheet
 * @param rowNum
 * @param fromCellNum 0-based index
 * @param toCellNum 0-based index
 */
private static void mergeCells(Sheet sheet, int rowNum, 
                                     int fromCellNum, int toCellNum) {
    sheet.addMergedRegion(
              new CellRangeAddress(rowNum, rowNum, fromCellNum, toCellNum));
}


Set a comment:

/**
 * Set a comment on a cell.
 *
 * @param text
 * @param cell
 */
private static void setComment(String text, Cell cell) {
    // Numbers on Mac OS hands the comment much, much better than Excel.  
    // Ugh to Excel!
       
    Sheet sheet = cell.getSheet();
    Row row = cell.getRow();
    Workbook wb = sheet.getWorkbook();
       
    CreationHelper createHelper = wb.getCreationHelper();

    Drawing drawing = sheet.createDrawingPatriarch();
       
    // count the number of lines in the comment.  
    // use this to decide how many rows down to anchor the comment box.
    // Note: there may be a better way to do it.  just playing around for now.
    int lineCount = getLineCount(text);
       
    ClientAnchor anchor = createHelper.createClientAnchor();
       
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex()+10);
    anchor.setRow1(row.getRowNum());
    anchor.setRow2(row.getRowNum()+lineCount);
        
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = createHelper.createRichTextString(StringUtils.strip(text));

    // without the font set, the comment would not appear at all!
    Font font = wb.createFont();
    font.setFontName( "Arial" );
       
    str.applyFont(font);

    comment.setString(str);
       
    cell.setCellComment(comment);
}

Thursday, November 3, 2011

Gmail New Layout - Chat Box

As you may be aware, Gmail is piloting the new layout and going to push it out soon.  I tried it, and at first look, I didn't like it.  Everything got so big, taking too much real estate, especially if you had a small screen.

After a few tweaks, I got it to how I like it.  On the right-hand side, there's a gear icon; under it, there's a "Display Density" setting.  I chose "Compact" which made it small again.

I'm not sure how many people actually use the integrated chat box.  I know I don't.  I wanted to completely remove it before, but I know I can't.  With the new layout, on the lower left-hand side, there's a icon for "Chat" (would only appear if you had disabled the "Right-side chat" labs feature.) and next to it "Gadgets."  Clicking on the "Chat" would minimize the chat box.  Clicking on "Gadgets" would minimize the other gadgets.

Now, I have the simple and clean layout that I like.  If you don't like the default colors, you can go change the theme.  Sounds like a given, but sometimes, people forget about it.