Category: Uncategorized

  • BigCommerce Mod : Custom product tabs

    This is a simple mod that allow us to have custom product tabs in BigCommerce system, there is no limit on how many tabs you can have. Our final result will be this.

    Mod

    This mod requires modification to only one file /Panels/ProductTabs.html

    Step 1

    Add current JavaScript to the existing script tag

    	/**
    	 * Overrides  parts of /javascript/product.functions.js#GenerateProductTabs() function
    	 * as it does not support related products	 
    	*/
    	$(document).ready(function()
    	{
    			var id = 'RelatedProducts';
    			var TabName = 'Related Products';
    			 var ProductTab = '
  • '+TabName+'
  • '; $('#ProductTabsList').append(ProductTab); });

    Step 2

    Here we actually add content of our tab.

    	
    

    One thing to not is that the div id ‘RelatedProducts’ relates to javascript id ‘RelatedProducts’ so if you change that you need to change the div id.

    That is, if we wanted to add additional tabs we simply would use an array.

  • BigCommerce Mod : Show brand logo image on Product Page

    Showing brand logos on product pages in BigCommerce sites is harder than it should be. Here is a quick mod that will let us do that without need of using the API.
    Here is the desired result

    Code

    This works by parsing you /brands page and then comparing current brand name to the one from parsed page.

    Lets edit Panels/ProductPanels.html and change the current brand code into this

    %%LNG_Brand%%:

    %%GLOBAL_BrandName%%
  • Calculate A to N power matrix using matrix diagonalization.

    This is matlab function to calculate sum of A to N power using matrix diagonalization, it assumes that matrix is a square matrix.

    function [x] = powersum(A, m)
    % Greg Bugaj
     % Y = powersum(A, n) gives an sum of  matrices to N power, if matrix size
    % is less than 2 then we simply return the input matrix
    % Input : A - an nxn matrix
    %         n - How many matrices to sum
    %Output x - summed matrix to N power.
    % P is our eigenvector, d is the diagonal to verify( inv(p)*A*p )
    x = A;
    [p d] = eigs(A);
    for i = 2 : m
        % x = x+(A^i); -- Just raise to power and sum 
        % note b * inv(A) is same as b/p
        x = x +  ((p * d^i) / p);
    end
    
  • Handling ENTER in TextBox, ASP.NET

    Well here is my way on how to handle the ENTER key on the TextBox in asp.net so it execute my method call instead the default form action.
    I have a textbox and a linkbutton that invokes the search, but I also would like to hook up to that linkbutton when I press ENTER on the TextBox so I would perform the search, but by default it would invoke the form submit which is not what we want. So here is a quick solution using javascript, I know there are other one but this one is short and it works well.

    
    
    		 
    		    
      
    	  
    
    
  • Proper way to read InputStream to byte array

    There are many ways to accomplish this but this one does not use any external dependencies like Apache commons.

    Two common pitfalls that I see are that people forget to flush the ByteArrayOutputStream and they call ‘baos.write(buffer)’ instead of ‘baos.write(buffer, 0, read)’ without actually clearing the buffer, which causes the last write to append previous bytes if the read returned less than what has been read from the input stream.

    	private String extract(InputStream inputStream) throws IOException {	
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();				
    		byte[] buffer = new byte[1024];
    		int read = 0;
    		while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
    			baos.write(buffer, 0, read);
    		}		
    		baos.flush();		
    		return  new String(baos.toByteArray(), "UTF-8");
    	}
    
  • Flash Scope and Flash variables in Spring MVC

    Sometimes its nice to be able to transfer object between request or for flash messages without need for a whole session. Here we will use session as our store mechanism for our flash scope. This concept here can be extended into implementing a ‘Conversation Scope’ but that is a whole different animal.

    Example Usage

     @FlashAttribute(TABNAME_ATTRIB)
     public String getTabName() {
       return tabObject;
     }
    
    
  • Pass Enum values by reference in Java

    Title might be little misleading since in java we pass always by value, but we can mimic passing by reference.

    /**
     * This class allows us to pass values by reference ranter than value.
     * Not directly as java just passes reference of the object by value but indirecly 
     * @author Greg
     *
     * @param 
     */
    public class IndirectReference {
    	public E ref;
    
    	public IndirectReference(E ref) {
    		this.ref = ref;
    	}
    
    	public void set(E ref) {
    		this.ref = ref;
    	}
    }
    

    Example usage

    Just a snippet copied from my interpreter

    	IndirectReference signal = new IndirectReference(ControlSignal.NOOP);
    	if(signal.ref == ControlSignal.BREAK){
    	    break;
    	}	
    
  • Regex to remove DOCTYPE prolog

    While using HTML Tidy I needed to remove the DOCTYPE prolog to prevent
    ‘org.xml.sax.SAXParseException: Already seen doctype.’ exception.

    Regex is quite simple, only catch is that we need to make sure we include the \n\r in our selecton and make it not greedy.

     convertedData = convertedData.replaceAll("", "");	
    

    This will consume multiline as well as single declarations

    /*		
    	
    */
    
  • ANTLR Operator precedence grammar

    This is snipper of my ANTLR grammar for parsing expressions with operator precedence,
    By default the expressions are evaluated left to right which in some cases may produce undesired results, in order to fix that use of left and right parenthesis is required.

    For example this two expressions will be evaluated differently

    3 > 2 > 1
    3 > (2 > 1)

    Precedence


    &&
    ||
    < > <= >=
    ^
    */
    +-

    Grammar

    According to ANTRL author currently we need a new rule for each precedence , which make is really messy but its not too bad after you get a hand of it. This grammar includes rewrite rules to generate our Abstract Syntax Tree(AST)

    expression 
    	: subExpr -> ^(EXPR subExpr)
    	;
    
    subExpr : logicalAndExp (addSubtractOp^ logicalAndExp)*
    	;	
    	
    logicalAndExp
    	: logicalOrExp (multiplyDivideOp^  logicalOrExp)*	 
    	;
    
    logicalOrExp
    	: comparatorExp (CARET^  comparatorExp)* 	
    	;
    	
    comparatorExp
    	: powExp (comparatorOp^  powExp)* 	
    	;
    		
    powExp 	: multExp (BARBAR^   multExp)*  
    	;
    
    multExp	
    	:  expressionAtom (AMPAMP^ expressionAtom)*
    	;
    
    expressionAtom
    	: 
    	|   NUMBER
    	|  ( LPAREN! subExpr^ RPAREN! ) 
    	|   VARNAME
    	|   function 
    	;
    
    
    addSubtractOp 
    	:	PLUS
    	|       MINUS
    	;    
    	
    multiplyDivideOp 
    	:	STAR
    	|       SLASH
    	;    
    
    comparatorOp 
    	:	GT
    	|       LT
    	| 	GTE
    	|	LTE
    	|	NEQ
    	;    
    	
    

    Abstract Syntax Trees

    As stated before following two expression produce two different AST, ignore the semantics of the operators as they are only to show proper AST construction.

    3 > 2 > 1
    3 > (2 > 1)



    Here is a expression parsed left to right

    set @me = 1+2+3*2

    Here is a more complex expression that shows precedence and parenthesis operations

    set @me = 1*(2*3 + 3*2)/5+1

    I think that for next post I will show on how to evaluate given expressions, also I am always looking for suggestions and comments

  • Custom port is not allowed or the host is not registered with this consumer key -yahoo developer network

    To work around this error
    Custom port is not allowed or the host is not registered with this consumer key.
    Register the app as Desktop/Client application instead of web app.