Git replacing the editor for commit messages

The man page says

ENVIRONMENT AND CONFIGURATION VARIABLES
 
The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

So in order to make nano our default editor we can do

git config --global core.editor "nano"

--global means this will become a default setting for all of git (leaving it out would only affect the current git repo you are running the command from)

Visual Studio 2012 and Razor 1.0 (opening an older project in 2012)

VisualStudio 2012 assumes we are using the latest and greatest (version 2) when it comes to the Razer view engine. However when we are opening a project that was created using version 2010 then we were using 1.0 and that is the reason that 2012 is telling us about all the errors it believes we have with our project.

The solution is to edit the web.config file and under “appSettings” tell VisualStudio that this project is using version 1.0

<add key="webpages:Version" value="1.0" />

vb.NET inserting a file to a Binary field in the Database

In order to store Binary data in a table, all that is needed is to upload the bytes from the file, one way to do this is to use File.ReadAllBytes

Dim fileName As String = "C:\testfile.txt"
 
dbCommand = New SqlCommand("UPDATE FileTable SET BinaryFile=@BinaryFile WHERE FileId = @FileId", dbConnection)
dbCommand.Parameters.AddWithValue("@BinaryFile", File.ReadAllBytes(Filnamn))
dbCommand.Parameters.AddWithValue("@FileId", FileId)
 
dbCommand.ExecuteNonQuery()

.NET and matching element name with wildcard using XPath

This is similar to matching a name space with a wildcard.

'This is the node we are searching in
Dim someXmlNode As XmlNode = FetchXmlNode() 
Dim xPathExpr As String = String.Empty
 
'find all nodes with names that starts with nisse_
xPathExpr = ".//*[starts-with(name(), 'nisse_')]"
 
'loop through all elements that matches our XPath
For Each elementWithNisse As XmlNode In someXmlNode.SelectNodes(xPathExpr)
 
    AndHereAMiracleHappens(elementWithNisse)
 
Next

.NET and matching namespace with wildcard using XPath

.NET framework’s XslCompiledTransform only supports XPath and XSLT 1.0.

With XPath 1.0 ‘*’ is allowed to select all elements and nisse:* is allowed to select all elements in the namespace bound to the prefix ‘nisse’ but *:nisse to select ‘nisse’ elements in all
namespaces is not allowed in XPath 1.0, see http://www.w3.org/TR/xpath#node-tests.

And here is an example in vb.net where InnerText is read from the nameOfElementToFind in any name space

' This is the xmlnode the search is done with
Dim node As XmlNode = FetchSomeNode()
 
Dim innerTextFromNode As String = String.Empty
Dim xPathToSearchFor As String = String.Empty
 
'Will find all direct decendants nameOfElementToFind in any name space
xPathToSearchFor = "*[local-name() 'nameOfElementToFind']" 
 
'Will find all nameOfElementToFind in any name space and anywhere under this node
xPathToSearchFor = ".//*[local-name() 'nameOfElementToFind']" 
 
'Will find all nameOfElementToFind in any name space and anywhere in the document the node belongs to
xPathToSearchFor = "//*[local-name() 'nameOfElementToFind']"
 
If (node IsNot Nothing) Then
	innerTextFromNode = node.selectSingleNode(xPathToSearchIn).InnerText
End If

Java: Getting the Document from a Node or Element

When we for instance wishes to add a new element we need to pass along the document. If we have a element or a node we can get the document that way. Simply call getOwnerDocument and we are good.

Element element = //code to get a element
Node node = //code to get a node
 
//document from Node
Document document = node.getOwnerDocument();
 
//document from a Element
Document document = element.getOwnerDocument();

Java: Adding a Element under an Element (or Converting a Element to a Node)

When using Document a Element can only be appended with a Node, but what if we have a Element ?

Easy, Element extends Node so just pass the Element along and all is well

import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
Document doc;
Element newElement;
Element oldElement; //code to find that
newElement = doc.createElement("tag");
oldElement.appendChild(newElement);

Java: Creating a enum and converting from a string to the Enum

Here is a simple example of how create a Enum and how to convert from a String to the Enum (the common reason for doing this would be is we at some point have for OurEnum.toString() and now wishes to get back to the OurEnum)

First we need to create the Enum, and this needs to be in its own file

public enum OurEnum { ENUM_A, ENUM_B, ENUM_C }

When we then later on (for instace have saved the toString value in a xml file) wish to

String ourEnumAsAString = "ENUM_A"; //replace with code to read the value
OurEnum ourEnum = OurEnum.valueOf(ourEnumAsAString);

Android resize a bitmap

Here is a snippet to scale down a bitmap – the original bitmap still needs to fit on memory though

InputStream bitmap = mContext.getAssets().open("image.file");
Bitmap scaledBitmap= Bitmap.createScaledBitmap(BitmapFactory.decodeStream(bitmap), 120, 120, false);
bitmap.