Tuesday, April 21, 2009

HtmlUnit and .net AJAX

Task:
Create robot for web-system based on .net with AJAX.

Problem:
After login system writes that session is expired.

Solution:
Use following code to create WebClient instance:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
webClient.setThrowExceptionOnScriptError(false);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});


I haven't found out why does it work (whether solution is in firefox2 engine or in script error processing or in refresh handler) but it works!

Thursday, February 5, 2009

Start JBoss from Eclipse: Timeout error

Task:
Start JBoss 5.0 from Eclipse.

Problem:
Alert appears:
Server JBoss v5.0 at localhost was unable to start within 50 seconds. If the server requires more time, try increasing the timeout in the server editor.
But... there are not any timeout fields in servers editor in "Window -> Preferences"

Solution:
Find your server in "Servers" tab (among "Problems", "Search", "Console" so on), double click on it and just change timeout in this editor!
P.S. Don't forget to press "Ctrl+S" there....

Monday, December 22, 2008

POI: How to get sheet name in XLS file

Task:
Get sheet name in XLS file using Apache POI

Problem:
I tried to find getter of name among HSSFSheet methods... but...

Solution:
Use method HSSFWorkbook.getSheetName(int sheetId)

Example:

InputStream in = new BufferedInputStream(new FileInputStream("mybook.xls"));
...
HSSFWorkbook wb = new HSSFWorkbook(in);
for (int sheetId = 0; sheetId < wb.getNumberOfSheets(); sheetId++) {
 HSSFSheet sheet = wb.getSheetAt(sheetId);
 System.out.println(wb.getSheetName(sheetId));
 ...
}
// Don't forget to Close input stream in finally section ;-)

Sunday, December 21, 2008

JS: FileName from file select field to text field

Task:
There are file select and text input field on page.
When text field is empty and file is selected, text field should be filled in with name of file without extension and absolute path.

Solution:
Fields

<input type="file" name="myfile" onchange="fileChanged(this, this.form['name'])" />
<input type="text" name="name" />

Onchange JavaScript:

function fileChanged(fileSelectField, textField) {
 fileName = fileSelectField.value;
 if (textField.value==null || textField.value == "") {
  // Find start position
  var start = fileName.lastIndexOf('/');
  if (fileName.lastIndexOf('\\') > start) {
   start = fileName.lastIndexOf('\\');
  }
  // We should skip slash. It also works if no slashes found
  start = start + 1;
  // Find end position
  var end = fileName.lastIndexOf(".");
  if (end < start) {
   end=fileName.length;
  }
  // Set value into field
  textField.value=fileName.substr(start, end-start);
 }
}

Wednesday, December 10, 2008

FOP: table-layout="auto" warning

Task:
Generate table using XSL-FO and FOP 0.9:
<fo:table> etc...

Problem:
Warning in console appears:
Warning(Unknown location): fo:table, table-layout="auto" is currently not supported by FOP

Solution:
It's because default value of table-layout is "auto". It means width of table should be caltulated automatically by generator. But FOP 0.9 can't do it. So, we should turn off this function and define width manually:
<fo:table table-layout="fixed" width="100%" >

Thursday, December 4, 2008

FOP: Table of Contents

I needed this function but I haven't found black-on-white howto... So...

Task:
Table of contents is needed (in beginning, in the end of among other parts of document). Size of each chapter and number of chapters are unknown and can't be preliminary defined.

Solution:
There are different possibilities, even to insert hyperlinks in document (like <a name="..."> in HTML). But I need printable document so I need to get pages numbers only.

1) Table of contents


<fo:table>
 <fo:table-column/>
 <fo:table-column/>
 <fo:table-body>
  <xsl:for-each select="mydocument/chapters/chapter">
   <fo:table-row>
    <fo:table-cell>
     <fo:block margin-bottom="0.8cm">
      <xsl:value-of select="@subject"/>
     </fo:block>
    </fo:table-cell>
    <fo:table-cell>
     <fo:block margin-bottom="0.8cm">
      <fo:page-number-citation>
       <xsl:attribute name="ref-id">chapter<xsl:value-of select="@id"/></xsl:attribute>
      </fo:page-number-citation>
     </fo:block>
    </fo:table-cell>
   </fo:table-row>
  </xsl:for-each>
 </fo:table-body>
</fo:table>


2) Iterate over all chapters (id of chapter is an attribute):


<xsl:for-each select="mydocument/chapters/chapter">
 <fo:block>
  <xsl:attribute name="id">chapter<xsl:value-of select="@id" /></xsl:attribute>
 </fo:block>
  ...
</xsl:for-each>

Wednesday, December 3, 2008

XSL: Get position() of parent node

Task:
Get position() function of parent node. It was needed to represent xml structure like following (i had to check whether parent element is first, but I got interested in position() problem):

Element1Subelement1

Subelement2
Element2Subelement3

Subelement4


Problem:
parent::position() doesn't work because position is not property of element. In FOP an error appears:
Error: A node test that matches either NCName:* or QName was expected.

Solution:
Use count(parent::*/preceding-sibling::*)+1 code instead of parent::position()
By the way, if you wanna get position of parent of parent... :)
Use
count(parent::*parent::*/preceding-sibling::*)+1