<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1723</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1723" rel="self"/>
  <title>zetafish</title>
  <updated>Sat Dec 10 13:15:12 -0800 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor598203</id>
    <published>2011-12-10T13:15:12-08:00</published>
    <title>[Java] On Long if ladder</title>
    <content type="html">&lt;p&gt;You want to load a schema from a certain location if the systemId matches a known schema. The location of the schema is always in the /xsd folder. So a simple refactoring is to declare the known schemas and then check the systemId against the known schemas and if it matches you fetch the schema from the location&lt;/p&gt;

&lt;pre&gt;## [java]
    public InputSource resolveEntity(String publicId, String systemId) {
        List&amp;lt;String&amp;gt; knownSchemas = Arrays.asList(
                &amp;quot;documents.xsd&amp;quot;,
                &amp;quot;asset.xsd&amp;quot;,
                &amp;quot;xml.xsd&amp;quot;,
                &amp;quot;XInclude.xsd&amp;quot;,
                &amp;quot;story.xsd&amp;quot;,
                &amp;quot;article-body.xsd&amp;quot;,
                &amp;quot;article.xsd&amp;quot;);
        
        for (String schema : knownSchemas) {
            if (systemId.endsWith(schema)) {
                InputStream in = getClass().getResourceAsStream(&amp;quot;/xsd/&amp;quot; + schema);
                return new InputSource(in);
            }
        }
        return null;
    }
&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1949-long-if-ladder/refactors/598203" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor598202</id>
    <published>2011-12-10T13:13:48-08:00</published>
    <title>[Java] On Long if ladder</title>
    <content type="html">&lt;p&gt;You want to load a schema from a certain location if the systemId matches a known schema. The location of the schema is always in the /xsd folder. So a simple refactoring is to declare the known schemas and then check the systemId against the known schemas and if it matches you fetch the schema from the location&lt;/p&gt;

&lt;pre&gt;    public InputSource resolveEntity(String publicId, String systemId) {
        List&amp;lt;String&amp;gt; knownSchemas = Arrays.asList(
                &amp;quot;documents.xsd&amp;quot;,
                &amp;quot;asset.xsd&amp;quot;,
                &amp;quot;xml.xsd&amp;quot;,
                &amp;quot;XInclude.xsd&amp;quot;,
                &amp;quot;story.xsd&amp;quot;,
                &amp;quot;article-body.xsd&amp;quot;,
                &amp;quot;article.xsd&amp;quot;);
        
        for (String schema : knownSchemas) {
            if (systemId.endsWith(schema)) {
                InputStream in = getClass().getResourceAsStream(&amp;quot;/xsd/&amp;quot; + schema);
                return new InputSource(in);
            }
        }
        return null;
    }
&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1949-long-if-ladder/refactors/598202" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor598201</id>
    <published>2011-12-10T13:13:42-08:00</published>
    <title>[Java] On Long if ladder</title>
    <content type="html">&lt;p&gt;You want to load a schema from a certain location if the systemId matches a known schema. The location of the schema is always in the /xsd folder. So a simple refactoring is to declare the known schemas and then check the systemId against the known schemas and if it matches you fetch the schema from the location&lt;/p&gt;

&lt;pre&gt;    public InputSource resolveEntity(String publicId, String systemId) {
        List&amp;lt;String&amp;gt; knownSchemas = Arrays.asList(
                &amp;quot;documents.xsd&amp;quot;,
                &amp;quot;asset.xsd&amp;quot;,
                &amp;quot;xml.xsd&amp;quot;,
                &amp;quot;XInclude.xsd&amp;quot;,
                &amp;quot;story.xsd&amp;quot;,
                &amp;quot;article-body.xsd&amp;quot;,
                &amp;quot;article.xsd&amp;quot;);
        
        for (String schema : knownSchemas) {
            if (systemId.endsWith(schema)) {
                InputStream in = getClass().getResourceAsStream(&amp;quot;/xsd/&amp;quot; + schema);
                return new InputSource(in);
            }
        }
        return null;
    }
&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1949-long-if-ladder/refactors/598201" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor369962</id>
    <published>2009-11-23T14:53:19-08:00</published>
    <title>[C] On LPT garland</title>
    <content type="html">&lt;p&gt;The methods with the single loop basically run up or run down powers of 2. Iteration can be put in a seperate function. Only difference between the functions is how to get to the bits. The functions that combine in/out up/down also have similar pattern. Variation is to do a throwOff and to use the ~mask on the out/down pass.&lt;/p&gt;

&lt;pre&gt;
typedef short int (* BITFUN)(short int, short int);

short int toggle(short int i, short int mask) { return i ^ mask ;}
short int rise(short int i, short int mask)   { return i - (1 ^ mask); }
short int loop(short int i, short int mask)   { return (i + 128/i) ^ mask; }
short int walk1(short int i, short int mask)  { return i ^ mask; }
short int walk2(short int i, short int mask)  { return 128/i ^ mask; }

typedef struct comdef {
  int bits;
  BITFUN bitfun[];
} COMDEF;

COMDEF TOGGLE = { 8, { toggle, NULL } };
COMDEF RISE   = { 9, { rise, NULL } };
COMDEF LOOP   = { 4, { loop, NULL } };
COMDEF WALK   = { 4, { walk1, walk2, NULL } };

#define WITH_THROW  1
#define WITH_REVERSE 1

void emit(BITFUN* f, short int i, short int mask) {
  while (f) {
    short int bits = (**f)(i, mask);
    outb(PORT, bits);
    usleep(DELAY);
  }
}	
		
void up(COMDEF* def, short int mask) {
  for (i = 0 ; i &amp;lt; def-&amp;gt;bits ; ++i) {
    emit(def-&amp;gt;bitfun, 1 &amp;lt;&amp;lt; i, mask);
  }
}

void down(COMDEF* def, short int mask) {
  for (i = def-&amp;gt;bits - 1 ; i &amp;gt;= 0 ; --i) {
    emit(def-&amp;gt;bitfun, 1 &amp;lt;&amp;lt; i, mask);
  }
}

void combine(COMDEF* def, short int with, short int mask) {
  up(def, mask);
  if (WITH_THROW &amp;amp; with) {
    throwOff();
  }
  down(def, WITH_REVERSE &amp;amp; with ? ~mask : mask);
}

void serialToggle(short int mask)        { up( &amp;amp;TOGGLE, mask ); }
void serialToggleReverse(short int mask) { down( &amp;amp;TOGGLE, mask ); }

void serialRiseUp(short int mask)        { up( &amp;amp;RISE, mask ); }
void serialRiseDown(short int mask)      { down( &amp;amp;RISE, mask ); }

void centerLoopIn(short int mask)        { up( &amp;amp;LOOP, mask ); }
void centerLoopOut(short int mask)       { down( &amp;amp;LOOP, mask ); }

void centerWalkIn(short int mask)        { up( &amp;amp;WALK, mask ); }
void centerWalkOut(short int mask)       { down( &amp;amp;WALK, mask ); }

void serialLoop(short int mask)          { combine( &amp;amp;TOGGLE, WITH_THROW, mask ); }
void serialRise(short int mask)          { combine( &amp;amp;RISE, 0, mask ); }
void serialRiseReverse(short int mask)   { combine( &amp;amp;RISE, WITH_REVERSE, mask ); }
void centerLoop(short int mask)          { combine( &amp;amp;LOOP, WITH_THROW, mask ); }
void centerWalk(short int mask)          { combine( &amp;amp;WALK, WITH_THROW, mask ); }
&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1105-lpt-garland/refactors/369962" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor340502</id>
    <published>2009-10-12T07:56:46-07:00</published>
    <title>[Python] On filter by indentation level</title>
    <content type="html">&lt;p&gt;The count_leading_spaces could be made a one-line with the help of a regex.&lt;/p&gt;

&lt;pre&gt;def count_leading_spaces(input_str):
    return re.match('\s*', input_str).end()&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1041-filter-by-indentation-level/refactors/340502" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor338459</id>
    <published>2009-10-09T14:15:01-07:00</published>
    <title>[Java] On Multiple XSLT for Schematron validation</title>
    <content type="html">&lt;p&gt;Compiling the schematron file only has to happen once. Also generalized the compile stages.&lt;/p&gt;

&lt;pre&gt;    // NOTE: Compiling the 'input.sch' is needed only once.
    private final Transformer transformer = compileTransformer();
    
    
    // NOTE: sycnhronized because transformers are not thread safe.
    private synchronized void schematronTransformation()
            throws TransformerException
    {
        Source source = new StreamSource(&amp;quot;input.xml&amp;quot;);
        Result result = new StreamResult(&amp;quot;output.xml&amp;quot;);
        transformer.transform(source, result);
    }

    private static Transformer compileTransformer()
            throws TransformerException
    {
        TransformerFactory tf = TransformerFactory.newInstance();
        String[] stages = {
                &amp;quot;Schematron/iso_dsdl_include.xsl&amp;quot;,
                &amp;quot;Schematron/iso_abstract_expand.xsl&amp;quot;,
                &amp;quot;Schematron/iso_svrl_for_xslt2.xsl&amp;quot; };

        Node node = null;
        Source source = new StreamSource(&amp;quot;input.sch&amp;quot;);
        for (String stage : stages)
        {
            Source xsl = new StreamSource(stage);
            Transformer t = tf.newTransformer(xsl);
            DOMResult result = new DOMResult();
            t.transform(source, result);
            
            source = new DOMSource(node = result.getNode());
        }
        
        return tf.newTransformer(new DOMSource(node));
    }

    private static Node transform(Transformer t, Source source)
            throws TransformerException
    {
        DOMResult y = new DOMResult();
        t.transform(source, y);
        return y.getNode();
    }&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1064-multiple-xslt-for-schematron-validation/refactors/338459" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor334145</id>
    <published>2009-10-06T07:37:59-07:00</published>
    <title>[ActionScript] On Can anyone please refactor this java method for me, it's driving me crazy!</title>
    <content type="html">&lt;p&gt;This method is too big, it tries to do too many things. I would start by splitting the huge method into managable parts. I aim to keep my bigger methods no longer than a single page. Also right at the start you catch an exception and set the su to null and continue with the method but checking everywhere if su is not null. Seems simpler to not catch at all and let the exception be handled somewhere higher.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1059-can-anyone-please-refactor-this-java-method-for-me-it-s-driving-me-crazy/refactors/334145" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor317952</id>
    <published>2009-09-24T23:10:15-07:00</published>
    <title>[Ruby] On Cleanly write checks in a method for a dice game</title>
    <content type="html">&lt;p&gt;Hey sohoo, nice tutorial problems on ruby there. Here a regex solution.&lt;/p&gt;

&lt;pre&gt;S = [    [/111/, 1000],
         [/222/,  200],
         [/333/,  300],
         [/444/,  400],
         [/555/,  500],
         [/666/,  600],
         [/1/,    100],
         [/5/,     50]
    ]

def re_score(s)
  p, n = S.find{ |x| s =~ x.first }
  p.nil? ? 0 : n + re_score($` + $')
end

def score(dice)
  re_score dice.sort.collect{|n|n.to_s}.reduce(:+)
end
&lt;/pre&gt;</content>
    <author>
      <name>zetafish</name>
      <email>zetafish@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1044-cleanly-write-checks-in-a-method-for-a-dice-game/refactors/317952" rel="alternate"/>
  </entry>
</feed>

