Tuesday, October 30, 2012

HowTo fix laptop broken hinge

My old laptop right hinge broke off the other day and it was very hard to move it. Unfortunately there is some time until I can get a replacement so I had to do something in the meanwhile.
In the service they told me they can't fix it, that I need to find the part and then it can be replaced. So I was short of reasonable options for fixing.

The good thing was that right hinge with the monitor cables was working and I fortunately grasped that if I remove the right hinge I can close and open lid if I'm careful. The service man was kind enough to remove it and I was somehow happy.

But! Situation was still not perfect. If I did a bad move I could break the left hinge and end up with a machine in pieces. So it was time to see what I have in the bin... a broken pen... how could I throw it in the bin?! so useful!

Here's what I did:

I noticed the pen can be put inside the place of the removed hinge. The black parts in front and back are fitting ideally into the places the old hinge was expanding into the lid. So I needed to only cut the pen and use some glue to fix the black parts into it. Above is what I did. Here is how it ended up, you can also see the damaged place of the lid:

It's not very solid but good enough for the purpose.

P.S. I know, I need to clean it sometimes :-)

Friday, October 5, 2012

element namespace inheritance with XSLT

Ok, here's the task - an XML with namespaces, these namespaces change over time but rarely significantly enough to require changes to the XSL, and we want to have the XSL in such a way that these changes to namespace do not require us to go update the XSLs with the new ones. If we are not careful we can end up with the well known empty/blank namespace declarations like xmlns="" and these break XML validation

Let's take an example:

<?xml version='1.0' encoding='UTF-8'?>

<server xmlns="urn:jboss:domain:1.3">
   <extensions>
      <extension module="org.jboss.as.clustering.infinispan"/>
   </extensions>
</server>

We want to add an element "system-properties" next to extensions. So here we got this XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" indent="yes"/>

      <!-- Append <system-properties/> element. -->
      <xsl:template match="//*[local-name()='extensions'">
         <xsl:variable name="thisns" select="namespace-uri()"/>
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
         <xsl:element name="system-properties" namespace="{$thisns}"/>
      </xsl:template>

      <!-- Copy everything else untouched. -->
      <xsl:template match="@*|node()">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:template>

</xsl:stylesheet>

Now this transforms the initial XML into:

<?xml version="1.0" encoding="UTF-8"?>
<server xmlns="urn:jboss:domain:1.3">
   <extensions>
      <extension module="org.jboss.as.clustering.infinispan"/>
   </extensions>
   <system-properties/>
</server>

What is special about this is that <system-properties/> does not have `xmlns=""'. If you change XSL template to <system-properties/> instead of specifying NS with <xsl:element name="system-properties" namespace="{$thisns}"/> then you will see that empty namespace decaration added in output XML by any namespace aware XSLT processor like xalan, saxon, xsltproc, etc.

Now the magic bit so one doesn't need to hardcode the namespace URI is <xsl:variable name="thisns" select="namespace::*[name()='']"/>. This puts our matched element namespace into a variable we can later use to create the elements we want. Be careful to avoid copying or creating elements by just inlining them into the templete like a raw <system-properties/>, otherwise they will be considered no namespace instead of inheriting parent namespace. I wish there was an option to have elements inherit namespace but until then, we are stuck with complications like this one.

Credits to: http://stackoverflow.com/a/123005/520567

command line XSLT 2.0

I had troubles with with XSLT 2.0 transformations done through a system and found myself lacking a command line XSLT 2.0 tool to debug what's going on. I found saxon but it is a piece of code I can't install everywhere and I'm not sure how well does it work.

Thanks to this post I crafted a script with no external dependencies to output transformed document with first param the XSL, second param the XML to transform and the rest of params are key=value pairs if needed. Hope it helps somebody. UPDATE: xalan also has command line support

example: ./xsltJava.sh mystyle.xsl myxml.xml key1=hello


#!/bin/bash

set -e

XSL=`readlink -f "$1"`
XML=`readlink -f "$2"`
shift ; shift

TMPDIR=""
cleanup () {
   set +e
   popd > /dev/null
   [[ "$TMPDIR" ]] && rm -rf "$TMPDIR"
}

TMPDIR=`mktemp -d`
pushd "$TMPDIR" > /dev/null
trap cleanup EXIT

if [ -f "$XSL" ]; then
   cp "$XSL" mytran.xsl
   java com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile mytran.xsl
else
   echo stylesheet does not exist
   exit 1
fi

if [ -f "$XML" ]; then
   java com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform "$XML" mytran "$@"
else
   echo no file to transform
   exit 1
fi