Thursday 27 August 2015

Monitor log files in real time

use the tail unix command for which there are windows equivalents

Links

http://tailforwin32.sourceforge.net/index.php

http://unxutils.sourceforge.net

Example command line

@D:\Logs\tail -f D:\Squid\var\logs\access.log

Thursday 6 August 2015

Return all variables and functions from an xquery module using xslt

Issue

Require an XML listing of all variables and functions used in an xquery module

Resolution

The following XSLT is a quick and dirty method to extract all the variables and functions from an xquery module into an XML representation which can then be transformed to the required format

The transformation is XSLT2 and can be invoked by use of saxon using the following command line, where thisXSLT.xsl is the code below:

java -jar saxon.jar -it:main -xsl:thisXSLT.xsl -o:result.xml "csvFile=myfile.csv"

XSLT

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">

 
 <xsl:param name="xquery" as="xs:string" >utils.xq</xsl:param>
 
 <xsl:output indent="yes"/>

 <xsl:variable name="input-text" 
              as="xs:string" 
              select="unparsed-text($xquery, 'utf-8')"/>

 <xsl:template match="/" name="main">
  <xquery>
  <variables>
   <xsl:analyze-string select="$input-text" 
        regex="(declare)\s+(variable)\s+\$((\c)+:(\c)+)(\s+as\s+.+)?\s+(:=)"> 
     <xsl:matching-substring>
      <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
        <variable>
         <name datatype="{if (matches(regex-group(6), 'as ')) then substring-after(regex-group(6), 'as ') else 'undefined'}"><xsl:value-of select="normalize-space(regex-group(3))"/></name>
         <!--<xsl:value-of select="."/>-->
        </variable>
     </xsl:matching-substring>
     <xsl:non-matching-substring>
      <xsl:message>
      Non-matching line "<xsl:value-of select="."/>"
      </xsl:message>
     </xsl:non-matching-substring>
    </xsl:analyze-string>
  </variables>
  <functions>
   <xsl:variable name="regexFnStart">\{</xsl:variable>
   <xsl:analyze-string select="$input-text" 
       regex="(declare)\s*(function)\s+((\c)+)\(((.)*)\)(\s+as\s+.+)?\s+({$regexFnStart})"> 
    <xsl:matching-substring>
     <xsl:variable name="arguments" as="xs:string*" select="tokenize(normalize-space(regex-group(5)),',')"/>
       <function>
        <name datatype="{if (matches(regex-group(7), 'as ')) then substring-after(regex-group(7), 'as ') else 'undefined'}"><xsl:value-of select="normalize-space(regex-group(3))"/></name>
        <xsl:for-each select="$arguments">
         <xsl:variable name="datatype" as="xs:string?" select="if (matches(., ' as ')) then substring-after(., ' as ') else ''"/>
         <argument datatype="{if (matches($datatype,'\)$') and not(matches($datatype,'\(\)$'))) then replace($datatype, '\)$' ,'') else $datatype}">
          <xsl:value-of select="normalize-space(if (matches(., ' as ')) then substring-before(., ' as ') else .)"/>
         </argument>
        </xsl:for-each>
       <!--<xsl:value-of select="."/>--></function>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
     <xsl:message>
     Non-matching line "<xsl:value-of select="."/>"
     </xsl:message>
    </xsl:non-matching-substring>
   </xsl:analyze-string>
  </functions>
  </xquery>
 </xsl:template>