Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

drefty

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

FileMetaData: simple generic file_info metadata comment

This is a general-purpose default text snippet that
you can use to add metadata to any file. It is based
on YAML.
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : ___filename___
    ###     desc : |
    ###         ___description___
    ###     date : created="___date___"
    ###     last : lastmod="___last___"
    ###     lang : ___lang___
    ###     tags : ___tags___
    ### </region-file_info>

DateTime: Simple date operations in javascript

/*
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfDateTime.js
    ###     desc : |
    ###         Simple date operations in jscript.
    ###         This file is for use with windows scripting host.
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 12:18:57"
    ###     lang : jscript
    ###     tags : jscript javascript date time now month hour year cfDateTime
    ### </region-file_info>
    */

/// begin_: declare and init variables
    var today       = new Date();
    var strYear     = today.getFullYear();
    var iMonth      = today.getMonth() + 1; // +1, we do NOT want zero-based month index
    var iQuarter    = Math.ceil((iMonth / 12) * 4);
    var iDay        = today.getDate();
    var strDateOut  = "";

/// begin_: leading zeropad single-digit numbers
    iMonth = (iMonth < 10)? "0" + iMonth : iMonth;
    iDay = (iDay < 10)? "0" + iDay : iDay;

/// begin_: display output
    strDateOut = strYear+"-"+ iMonth +"-"+iDay + " ";
    WScript.Echo (strDateOut);

MathOps: simple math operations in xslt

cfMathOps.xml:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="cfMathOps.xsl" ?>
<!--
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfMathOps.xml
    ###     desc : xml data file to use with cfMathOps.xsl
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 11:57:41"
    ###     lang    : xslt
    ###     tags    : xml xslt math cfMathOps
    ### </region-file_info>
    -->
<numbers>
  <x>4</x>
  <y>3.2</y>
  <z>11</z>
</numbers>


cfMathOps.xsl
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!--
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfMathOps.xsl
    ###     desc : quick example of math operations using xslt
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 11:57:41"
    ###     lang    : xslt
    ###     tags    : xml xslt math hello cfMathOps
    ### </region-file_info>
    -->

<xsl:output method="html" omit-xml-declaration="yes"/>

<xsl:template match="numbers">
<h2>math operations from xslt</h2>
    <br />  A. 4 + 3.2        = <xsl:value-of select="x + y"/>
    <br />  B. 3.2 - 4        = <xsl:value-of select="y - x"/>
    <br />  C. 4 * 3.2        = <xsl:value-of select="x * y"/>
    <br />  D. 11/3.2         = <xsl:value-of select="z div y"/>
    <br />  E. 4 + 3.2 * 11   = <xsl:value-of select="x+y*z"/>
    <br />  F. (4 + 3.2) * 11 = <xsl:value-of select="(x+y)*z"/>
    <br />  G. 11 mod 4       = <xsl:value-of select="z mod x"/>
    <br />  H. 4 + 3.2 + 11   = <xsl:value-of select="sum(*)"/>
    <br />  I. floor(3.2)     = <xsl:value-of select="floor(y)"/>
    <br />  J. ceiling(3.2)   = <xsl:value-of select="ceiling(y)"/>
    <br />  K. round(3.2)     = <xsl:value-of select="round(y)"/>
    <br />  L. 11 + count(*)  = <xsl:value-of select="11+count(*)"/>
    <br />  M. 11 + "hello"                 = <xsl:value-of select="z + 'hello'"/>
    <br />  N. 3.2 + string-length("3.2")   = <xsl:value-of select="y + string-length(y)"/>
</xsl:template>

</xsl:stylesheet>

ForLoop: a simple for loop in xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : ForLoop: a simple for loop in xslt
    ###     desc : |
    ###         Do a simple for loop in xslt displaying hello world.
    ###         Call this from any source xml file.
    ###         It works independently of the data in the xml.
    ###     date : created="Thu 2005-12-01 11:30:52"
    ###     last : lastmod="Thu 2005-12-01 11:30:57"
    ###     lang    : xslt
    ###     tags    : xml xslt loop for hello_world
    ### </region-file_info>
    -->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

<xsl:template match="/">

<html>
<head><title>Say Hello Ten Times!</title></head>
<body>
    <b>I am going to say hello Ten Times!</b>
<!-- begin_: Send_Loop_To_HTML -->
    <xsl:call-template name="for.loop">
     <xsl:with-param name="i">1</xsl:with-param>
     <xsl:with-param name="count">10</xsl:with-param>
    </xsl:call-template>
</body>
</html>

</xsl:template>


<!--begin_: Define_The_Output_Loop -->
  <xsl:template name="for.loop">

   <xsl:param name="i"      />
   <xsl:param name="count"  />

   <!--begin_: Line_by_Line_Output -->
   <xsl:if test="$i &lt;= $count">
      <br /> <b><xsl:value-of select="$i" />.</b>Hello world!
   </xsl:if>

   <!--begin_: RepeatTheLoopUntilFinished-->
   <xsl:if test="$i &lt;= $count">
      <xsl:call-template name="for.loop">
          <xsl:with-param name="i">
              <xsl:value-of select="$i + 1"/>
          </xsl:with-param>
          <xsl:with-param name="count">
              <xsl:value-of select="$count"/>
          </xsl:with-param>
      </xsl:call-template>
   </xsl:if>

  </xsl:template>
</xsl:stylesheet>

XMLPrettyPrint: simple xml pretty print in perl

### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name    : XMLPrettyPrint: simple xml pretty print in perl
    ###     desc    : use perl with XML::Twig library to print indented xml
    ###     date    : created="Thu 2005-12-01 11:08:15"
    ###     last    : lastmod="Thu 2005-12-01 11:22:34"
    ###     lang    : perl
    ###     tags    : perl xml indent formatted pretty string cfPrettyPrint
    ### </region-file_info>

### begin_: init perl
    use strict;
    use warnings;
    use XML::Twig;

### begin_: init vars
    my  $sXML  = join "", (<DATA>);

    ### init params
    my  $params = [qw(none nsgmls nice indented record record_c)];
    my  $sPrettyFormat  = $params->[3] || 'none';

### begin_: process
    my  $twig= new XML::Twig;
    $twig->set_indent(" "x4);
    $twig->parse( $sXML );
    $twig->set_pretty_print( $sPrettyFormat );
    $sXML      = $twig->sprint;

### begin_: output
    print $sXML;

### begin_: sample data
    1;
    __END__
<table><tr age="35" >
<fname>Homer</fname>
<lname>Simpson</lname></tr>
<tr age="33" >
<fname>Barney</fname>
<lname>Rubble</lname></tr>
<tr age="29" >
<fname>Betty</fname>
<lname>Rubble</lname></tr></table>

TryCatch: simple try catch block in perl

### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name    : TryCatch: try catch block
    ###     desc    : a simple try-catch block example in perl
    ###     date    : created="Thu 2005-12-01 10:58:09"
    ###     last    : lastmod="Thu 2005-12-01 10:58:13"
    ###     tags    : try catch finally error perl cfTryCatch exception
    ### </region-file_info>

### begin_: init perl
    use strict;
    use warnings;

### begin_: try-catch block
    print "begin \n";
    eval{
        ### try block
        print Non_Existent_Function();
    };
    if ($@){
        ### catch block
        print "Failed \n";
    };
    print "end \n";

DateTime: generic date and time script in perl

### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : DateTime.pl
    ###     desc : DateTime: generic date and time script in perl
    ###     date : created="Thu 2005-12-01 10:04:52"
    ###     last : lastmod="Thu 2005-12-01 10:04:59"
    ### </region-file_info>

### begin_: initialize perl (optional)
    use strict;
    use warnings;

### begin_: initialize DateTime values
    my %dttime = ();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

### begin_: initialize DateTime number formats
    $dttime{year }  = sprintf "%04d",($year + 1900);  ## four digits to specify the year
    $dttime{mon  }  = sprintf "%02d",($mon + 1);      ## zeropad months
    $dttime{mday }  = sprintf "%02d",$mday;           ## zeropad day of the month
    $dttime{wday }  = sprintf "%02d",$wday + 1;       ## zeropad day of week; sunday = 1;
    $dttime{yday }  = sprintf "%02d",$yday;           ## zeropad nth day of the year
    $dttime{hour }  = sprintf "%02d",$hour;           ## zeropad hour
    $dttime{min  }  = sprintf "%02d",$min;            ## zeropad minutes
    $dttime{sec  }  = sprintf "%02d",$sec;            ## zeropad seconds
    $dttime{isdst}  = $isdst;

### begin_: xnpDate print iso8601 version date
    print "$dttime{year}-$dttime{mon}-$dttime{mday}\n";

### begin_: xnpNow show system time
    print "$dttime{year}-$dttime{mon}-$dttime{mday} $dttime{hour}:$dttime{min}:$dttime{sec} \n";



« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS