Youtube embed video html code without branding and controls (seekbar, play/pause and volume)

Youtube embedded player can be adjusted a lot using the parameters available here https://developers.google.com/youtube/player_parameters. Some parameters are available on both HTML5 and flash players and some are not.

Check out https://developers.google.com/youtube/youtube_player_demo, for all availabel configuration and play with it.

For example you can hide the controllers in embedded video, by setting parameter controls to 0

1
2
3
4
<iframe
src="http://www.youtube.com/embed/p09QlQQazo0?controls=0"
width="640" height="360">
</iframe>


Default video quality can be set using vq.
1
2
3
<iframe src="http://www.youtube.com/embed/p09QlQQazo0?vq=hd720" style="border: 5px solid #cccccc; overflow: hidden;" width="640" height="360" frameborder="0"
allowfullscreen></iframe>


Youtube allows you to have its logo to be modest, by setting modestbranding to 1.
1
2
<iframe src="http://www.youtube.com/embed/p09QlQQazo0?modestbranding=1" width="640" height="360" frameborder="0" allowfullscreen></iframe>


We can also set all of these values together
1
2
3
4
<iframe src="http://www.youtube.com/embed/p09QlQQazo0?rel=0&amp;autoplay=0&amp;showinfo=0&amp;modestbranding=1&amp;controls=0&amp;vq=hd720"
style="border: 5px solid #cccccc; overflow: hidden;" width="640" height="360"
frameborder="0" allowfullscreen></iframe>

Java :: Get last date of a month

In Java we can use the Calendar class to get day_of_month, day_of_week etc.
Last day of a month varies depending on the Month and on leap year we have extra day in February.
So to figure out the last day of any given month in a year, we write some code which is apparently simple.
Calendar object allows us to manipulate days, go forward or backward on the Calendar, add days, hours, minutes or seconds to any given time etc.
We will use these capabilities of Calendar class to get our last_of_month.

The idea here is to get the first day of next month and then reduce one day from it, which gives us the last day of the month relative to input date.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* The trick is to add a month, set the date as 1st and then reduce a date.
*
* @param calendar
*/
private static void setCalendarToLastDayOfMonth(Calendar calendar) {
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -1);
}

But, Java Calendar has a better way of doing this, by using getActualMaximum() method which is far more convenient.

1
2
3
4
5
6
7
8
private static void setCalendarToLastDayOfMonth(Calendar calendar) {
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, lastDay);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}

A sample calendar util class which gives you few convenient methods is given below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright (C) 2013 Deepu Mohan Puthrote. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.text.ParseException;
public class CalendarUtil {
public static void main(String[] args) throws Exception {
String string = "January 2, 2010";
CalendarUtil calendarUtil = new CalendarUtil();
System.out.println(calendarUtil.getLastDateOfMonth(string));
System.out.println(calendarUtil.getLastDateOfMonth());
}
public Date getLastDateOfMonth() {
return getLastDateOfMonth(new Date());
}
public Date getLastDateOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getLastDateOfMonth(calendar);
}
public Date getLastDateOfMonth(Calendar calendar) {
setCalendarToLastDayOfMonth(calendar);
return calendar.getTime();
}
public Date getLastDateOfMonth(String date) throws ParseException {
return getLastDateOfMonth(date, "MMMM d, yyyy");
}
public Date getLastDateOfMonth(String date, String format) throws ParseException {
return getLastDateOfMonth(date, format, Locale.US);
}
public Date getLastDateOfMonth(String stringDate, String format, Locale locale) throws ParseException {
Date date = new SimpleDateFormat(format, locale).parse(stringDate);
return getLastDateOfMonth(date);
}
private static void setCalendarToLastDayOfMonth(Calendar calendar) {
int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, lastDay);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
}

###References

How to change extension of multiple files (bash script)

The following is the script I use in Macbook (Mountain Lion). It should work on most /*nix shell like bash, csh, ksh etc.
However, I have tested it only in bash.

Script to change extension for multiple filesview raw
1
2
3
4
#!/bin/bash
for f in *.$1; do
[[ -f "$f" ]] && mv -v "$f" "${f%$1}$2"
done

Save this file as “chext”, make it executable chmod +x chext and add to your $PATH.
Now you can use it like this:
Usage
1
2
cd path-to-dir
chext oldextension newextension

Example
1
chext JPG jpg

###Reference:

JavaMail :: javax.mail.NoSuchProviderException: smtp

When you are using Java’s mailapi.jar, it also expects supporting libraries like smtp.jar, imap.jar, pop3.jar, gimap.jar, dsn.jar. If you are using Java EE platform, then it is already included. But if you are getting the following error, you might have to include additional libraries, from Java mail API.

1
2
3
4
5
6
7
8
Exception in thread "main" javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:746)
at javax.mail.Session.getTransport(Session.java:685)
at javax.mail.Session.getTransport(Session.java:628)
at javax.mail.Session.getTransport(Session.java:608)
at javax.mail.Session.getTransport(Session.java:663)
at javax.mail.Transport.send0(Transport.java:154)
at javax.mail.Transport.send(Transport.java:80)

There are several forums and discussions happening on this same issue. Surprisingly, this is a small class path issue.

To solve this error, make sure you have both mailapi.jar and smtp.jar in your classpath. Also, make sure you don’t have duplicate jar files in your class path.

Reading the FAQ on Oracle’s documentation will give you a better idea. http://www.oracle.com/technetwork/java/javamail/faq/index.html

References

  1. http://www.oracle.com/technetwork/java/javamail/javamail-138606.html
  2. http://www.coderanch.com/t/601387/java/java/smtp-error/
  3. http://stackoverflow.com/questions/2980408/problem-with-java-mail-no-provider-for-smtp/
  4. http://stackoverflow.com/questions/16861553/javax-mail-nosuchproviderexception-no-provider-for-smtps/
  5. https://answers.atlassian.com/questions/167299/unable-to-send-email-and-error-as-javax-mail-nosuchproviderexception/

Javascript :: Ace Editor Positioning with Bootstrap3

Ace editor is a fantastic option for live source code editor on the web. One problem you come across when using Ace Editor is with the default absolute positioning. Also it doesn’t fit inside a container div by default. Here is how I overcame these issues.

The example from ace website uses absolute positioning by default, as show below. But luckily, ace can work perfectly with relative positioning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
</head>
<body>
<div id="editor">function foo(items) {
var x = "All this is syntax highlighted";
return x;
}
</div>
<script src="/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/javascript");
</script>
</body>
</html>

In order to put ace-editor inside a container div, we can give relative positioning to the editor and remove the positioning attribs, like top, right, bottom and left.

1
2
3
#editor {
position: relative;
}

We also need to give height, otherwise editor won’t show up.

1
2
3
4
#editor {
position: relative;
height: 300px;
}

That said, when using Twitter Bootstrap, you can absolutely forget about relative positioning of ace. But we still need to set height, as ace does not fill. In the following jsfiddle, I have used Bootstrap’s panel component for showing the editor.

Full Screen Result is available here. http://jsfiddle.net/deepumohanp/U5JtP/embedded/result/

Fiddle here: http://jsfiddle.net/deepumohanp/U5JtP/

References

  1. How do I make a textarea an ace editor?
  2. Ace doesn’t fit inside a container div

Blog Update :: Bootstrap awesomeness

Yesterday, I spend some to change the blog template using Bootstrap.

Thanks to Blogger’s new template editor. Now it is much easier to understand the templates.

The template was built on top of my older template Simple by Joshua Peterson. The process was simple and straight forward.

Blogger templates are mostly collection of widgets. I introduced few new divs with Bootstrap classes and removed few divs from older template. Bootstrap documentation has everything clearly explained along with examples.

I haven’t tested this new template on all screens yet.

Javascript :: Pass function as parameter

Javascript allows you to pass functions as parameter to another function and that makes Javascript super awesome!. Great things can be achieved from this power.
Quote from Mozilla developer website:

In JavaScript, functions are first-class objects, i.e. they are objects and can be manipulated and passed around just like any other object.

Every Javascript function is a Function object. Hence we can pass them as arguments, like any other object.

###How to do that?
Passing function as parameter is no different from passing any other object when it comes to syntax.
I am gonna try and explain that with an example (hopefully, easy to understand)

###The example problem
Bob wants to get sum of values [1, 2, 3, 4, 5, 6, 7, 8, 9]
So we give him the following solution

1
2
3
4
5
6
7
8
// find sum of values
var total = function(values) {
var sum = 0;
values.forEach( function(value) {
sum += value;
});
return sum;
};

Bob is happy with the function. But now he wants to get sum of all even numbers in the list of values.
So we copy-paste the above function, rename it and make slight changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// find sum of values
var total = function(values) {
var sum = 0;
values.forEach( function(value) {
sum += value;
});
return sum;
};
// find sum of even numbers in list of values
var totalEven = function(values) {
var sum = 0;
values.forEach( function(value) {
if( value % 2 === 0 ) sum += value;
});
return sum;
};
```
Bob is happy again. But he wants more. Now he need sum of all odd numbers from the same list.
As usual, we copy-paste, rename and make slight changes.
``` javascript
// find sum of values
var total = function(values) {
var sum = 0;
values.forEach( function(value) {
sum += value;
});
return sum;
};
// find sum of even numbers in list of values
var totalEven = function(values) {
var sum = 0;
values.forEach( function(value) {
if( value % 2 === 0 ) sum += value;
});
return sum;
};
// find sum of odd numbers in list of values
var totalOdd = function(values) {
var sum = 0;
values.forEach( function(value) {
if( value % 2 !== 0 ) sum += value;
});
return sum;
};
```
###The problem with that approach
* Lot of duplicate code.
* Cluttered and ugly source code.
* Source file becomes unnecessarily lengthy.
* Not applying re-usabilty principle.
* Have to create separate function for each additional request.
###Better code with passing function as argument
We can improve the above code a lot by passing the selection functionality as argument. In the above example, the basic operation is summation. Then, we need some kind of selection of values to sum. This selection can be anything, like even numbers, odd numbers etc. This is what we will pass an argument.
``` javascript
var totalSelectValues = function(values, selector) {
var sum = 0;
values.forEach(function(value) {
if(selector(value)) sum+= 0;
});
return sum;
};

The selector, is function passed as parameter. Here we are expecting it to return a boolean value.

1
2
3
4
5
6
7
8
9
10
var values = [1,2,3,4,5,6,7,8,9];
// even numbers
totalSelectValues(values, function(value) { return value % 2 === 0});
// odd numbers
totalSelectValues(values, function(value) { return value % 2 !== 0});
// greater than 5
totalSelectValues(values, function(value) { return value > 5 });

Awesome! We just created functions on the fly and passed it as parameters, didn’t even bother to name it! Neat eh? Also we have reduced the code base by far. Just a single function and no ugly repetition.

###But that is not enough
We also need to make sure our little function works perfectly even if the second argument is not send.

var totalSelectValues = function(values, selector) {
    // handle undefined selector
    if (typeof selector == 'undefined' ) {
        selector = function() {return true;};
    }
    var sum = 0;
    values.forEach(function(value) {
        if(selector(value)) sum+= 0;
    });
    return sum;
};

Now that should work.
The complete example is available in the following jsFiddle. http://jsfiddle.net/deepumohanp/s2DBJ/

###References

  1. I got the example inspiration from Dr. Venkat Subramaniam’s talk an Scala.

  2. http://www.joelonsoftware.com/items/2006/08/01.html

  3. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope
  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function