Skip to content

Instantly share code, notes, and snippets.

@happz
Created January 11, 2014 19:59
Show Gist options
  • Save happz/8375949 to your computer and use it in GitHub Desktop.
Save happz/8375949 to your computer and use it in GitHub Desktop.
/*
Expect source file, one record per line:
name;category;description;size;price;picture link;picture title
Note: record entries can not contain semicolons... Maybe better separator?
Also replace getNextRecord with different implementation based on your data source, this one does use file.
*/
public class LiferayPortlet extends ... {
// Oddelovac casti v souboru
protected static final String RECORD_SEPARATOR = ";"
// HTML sablona pro vystup, casti {...} se nahradi udaji ze souboru
protected static final String RECORD_TEMPLATE = "
<div id=\"item\">
<div id=\"item_content\">
<div id="name">{NAME}</div>
<div id="category" >{CATEGORY}</div>
<div id="description">{DESCRIPTION}</div>
<div id="size">{SIZE}</div>
<div id="price">{PRICE}</div>
<div id="picture"><img src="{PICTURE_LINK}" alt="{PICTURE_TITLE}" /></div>
</div>
</div>
";
protected BufferedReader sourceReader;
public LiferayPortlet(String sourceFile) {
// Rovnou otevrit soubor s clanky
this.sourceReader = new BufferedReader(new FileReader(sourceFile));
}
// Vrati dalsi zaznam ze souboru nebo null
// V pripade jineho zdroje dat nez soubor staci upravit
protected String[] getNextRecord() {
String line = sourceReader.readLine();
if (line == null) {
// End of file
sourceReader.close();
return null;
}
return line.split(RECORD_SEPARATOR);
}
// Vezme dalsi zaznam (je-li), vezme sablonu, nahradi v ni patricne daty ze zaznamu a vrati vysledne HTML
public getHTMLForNextRecord() {
String[] record = getNextRecord();
if (record == null)
return null;
String output = RECORD_TEMPLATE;
output = output.replace("\{NAME\}", record[0]);
output = output.replace("\{CATEGORY\}", record[1]);
output = output.replace("\{DESCRIPTION\}", record[2]);
output = output.replace("\{SIZE\}", record[3]);
output = output.replace("\{PRICE\}", record[4]);
output = output.replace("\{PICTURE_LINK\}", record[5]);
output = output.replace("\{PICTURE_TITLE\}", record[5]);
return output;
}
/*
* Just to show how to call it...
*/
public static void main(String[] args) {
// Spustit s cestou k nejakemu souboru s daty
if (args.length != 2) {
System.out.println("Usage: program <source file>");
return;
}
// Vytvori se novy objekt LiferayPortlet (tahle trida) a preda se mu cesta k datum
LiferayPortlet portlet = new LiferayPortlet(args[1]);
String record;
// Dokud record != null (v pripade konce souboru) vypise HTML pro kazdy zaznam
while((record = this.getHTMLForNextRecord()) != null) {
System.out.println("Next record:");
System.out.println(record);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment