@ -1,4 +1,5 @@
import java.io.IOException ;
import java.util.HashSet ;
import java.util.Iterator ;
import net.yacy.cora.protocol.RequestHeader ;
@ -6,8 +7,8 @@ import net.yacy.document.parser.html.CharacterCoding;
import net.yacy.kelondro.blob.Tables ;
import net.yacy.kelondro.index.RowSpaceExceededException ;
import net.yacy.kelondro.logging.Log ;
import de.anomic.data.YMarkIndex ;
import de.anomic.data.YMarkTables ;
import de.anomic.data.YMarksXBELImporter ;
import de.anomic.data.userDB ;
import de.anomic.search.Switchboard ;
import de.anomic.server.serverObjects ;
@ -17,16 +18,15 @@ public class get_xbel {
public static final String ROOT = "root" ;
public static final String SOURCE = "source" ;
static Switchboard sb ;
static serverObjects prop ;
static String bmk_user ;
public static serverObjects respond ( final RequestHeader header , final serverObjects post , final serverSwitch env ) {
sb = ( Switchboard ) env ;
prop = new serverObjects ( ) ;
final Switchboard sb = ( Switchboard ) env ;
final serverObjects prop = new serverObjects ( ) ;
final HashSet < String > alias = new HashSet < String > ( ) ;
final StringBuilder buffer = new StringBuilder ( 250 ) ;
final userDB . Entry user = sb . userDB . getUser ( header ) ;
final boolean isAdmin = ( sb . verifyAuthentication ( header , true ) ) ;
final boolean isAuthUser = user ! = null & & user . hasRight ( userDB . Entry . BOOKMARK_RIGHT ) ;
final String bmk_user ;
if ( isAdmin | | isAuthUser ) {
bmk_user = ( isAuthUser ? user . getUserName ( ) : YMarkTables . USER_ADMIN ) ;
@ -34,6 +34,7 @@ public class get_xbel {
String root = YMarkTables . FOLDERS_ROOT ;
String [ ] foldername = null ;
// TODO: better handling of query
if ( post ! = null ) {
if ( post . containsKey ( ROOT ) ) {
if ( post . get ( ROOT ) . equals ( SOURCE ) | | post . get ( ROOT ) . equals ( YMarkTables . FOLDERS_ROOT ) ) {
@ -42,37 +43,110 @@ public class get_xbel {
root = post . get ( ROOT ) ;
} else {
root = "" ;
// root = YMarkTables.FOLDERS_ROOT + post.get(ROOT);
}
}
} else {
root = "" ;
}
Iterator < String > it = null ;
final int root_depth = root . split ( YMarkTables . FOLDERS_SEPARATOR ) . length ;
Iterator < String > fit = null ;
Iterator < String > bit = null ;
int count = 0 ;
int n = YMarkIndex . getFolderDepth ( root ) ;
int n = root_depth ;
try {
it = sb . tables . bookmarks . folders . getFolders ( bmk_user , root ) ;
f it = sb . tables . bookmarks . folders . getFolders ( bmk_user , root ) ;
} catch ( IOException e ) {
Log . logException ( e ) ;
}
while ( it . hasNext ( ) ) {
String folder = it . next ( ) ;
Log . logInfo ( YMarkTables . BOOKMARKS_LOG , "root: " + root + " root_deph: " + root_depth ) ;
while ( fit . hasNext ( ) ) {
String folder = fit . next ( ) ;
foldername = folder . split ( YMarkTables . FOLDERS_SEPARATOR ) ;
Log . logInfo ( YMarkTables . BOOKMARKS_LOG , "folder: " + folder + " getFolderDepth(folder): " + YMarkIndex . getFolderDepth ( folder ) + " n: " + n ) ;
if ( count > 0 & & YMarkIndex . getFolderDepth ( folder ) < = n ) {
Log . logInfo ( YMarkTables . BOOKMARKS_LOG , "folder: " + folder + " n: "+ n + " folder_length: " + foldername . length ) ;
if ( n ! = root_depth & & foldername . length < = n ) {
prop . put ( "xbel_" + count + "_elements" , "</folder>" ) ;
count + + ;
}
if ( YMarkIndex . getFolderDepth ( folder ) > = n ) {
n = YMarkIndex . getFolderDepth ( folder ) ;
prop . put ( "xbel_" + count + "_elements" , "<folder id=\"f:" + new String ( YMarkTables . getKeyId ( foldername [ n ] ) ) + "\">" ) ;
if ( foldername . length > = n ) {
n = foldername . length ;
if ( n ! = root_depth ) {
prop . put ( "xbel_" + count + "_elements" , "<folder id=\"f:" + new String ( YMarkTables . getKeyId ( foldername [ n - 1 ] ) ) + "\">" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "<title>" + CharacterCoding . unicode2xml ( foldername [ n ] , true ) + "</title>" ) ;
prop . put ( "xbel_" + count + "_elements" , "<title>" + CharacterCoding . unicode2xml ( foldername [ n - 1 ] , true ) + "</title>" ) ;
count + + ;
}
try {
count = putBookmarks ( folder , count ) ;
bit = sb . tables . bookmarks . folders . getBookmarks ( bmk_user , folder ) . iterator ( ) ;
Tables . Row bmk_row = null ;
String urlHash ;
while ( bit . hasNext ( ) ) {
urlHash = new String ( bit . next ( ) ) ;
if ( alias . contains ( urlHash ) ) {
buffer . setLength ( 0 ) ;
buffer . append ( YMarksXBELImporter . XBEL . ALIAS . startTag ( true ) ) ;
buffer . append ( " ref=\"b:" ) ;
buffer . append ( urlHash ) ;
buffer . append ( "\"/>" ) ;
prop . put ( "xbel_" + count + "_elements" , buffer . toString ( ) ) ;
count + + ;
} else {
alias . add ( urlHash ) ;
bmk_row = sb . tables . select ( YMarkTables . TABLES . BOOKMARKS . tablename ( bmk_user ) , urlHash . getBytes ( ) ) ;
if ( bmk_row ! = null ) {
buffer . setLength ( 0 ) ;
buffer . append ( YMarksXBELImporter . XBEL . BOOKMARK . startTag ( true ) ) ;
buffer . append ( " id=\"b:" ) ;
buffer . append ( urlHash ) ;
buffer . append ( YMarkTables . BOOKMARK . URL . xbel ( ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . URL . key ( ) , YMarkTables . BOOKMARK . URL . deflt ( ) ) , true ) ) ;
buffer . append ( YMarkTables . BOOKMARK . DATE_ADDED . xbel ( ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_ADDED . key ( ) ) ) , true ) ) ;
buffer . append ( YMarkTables . BOOKMARK . DATE_MODIFIED . xbel ( ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_MODIFIED . key ( ) ) ) , true ) ) ;
buffer . append ( YMarkTables . BOOKMARK . DATE_VISITED . xbel ( ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_VISITED . key ( ) ) ) , true ) ) ;
buffer . append ( YMarkTables . BOOKMARK . TAGS . xbel ( ) ) ;
buffer . append ( bmk_row . get ( YMarkTables . BOOKMARK . TAGS . key ( ) , YMarkTables . BOOKMARK . TAGS . deflt ( ) ) ) ;
buffer . append ( YMarkTables . BOOKMARK . PUBLIC . xbel ( ) ) ;
buffer . append ( bmk_row . get ( YMarkTables . BOOKMARK . PUBLIC . key ( ) , YMarkTables . BOOKMARK . PUBLIC . deflt ( ) ) ) ;
buffer . append ( YMarkTables . BOOKMARK . VISITS . xbel ( ) ) ;
buffer . append ( bmk_row . get ( YMarkTables . BOOKMARK . VISITS . key ( ) , YMarkTables . BOOKMARK . VISITS . deflt ( ) ) ) ;
buffer . append ( "\"\n>" ) ;
prop . put ( "xbel_" + count + "_elements" , buffer . toString ( ) ) ;
count + + ;
buffer . setLength ( 0 ) ;
buffer . append ( YMarksXBELImporter . XBEL . TITLE . startTag ( false ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . TITLE . key ( ) , YMarkTables . BOOKMARK . TITLE . deflt ( ) ) , true ) ) ;
buffer . append ( YMarksXBELImporter . XBEL . TITLE . endTag ( false ) ) ;
prop . put ( "xbel_" + count + "_elements" , buffer . toString ( ) ) ;
count + + ;
buffer . setLength ( 0 ) ;
buffer . append ( YMarksXBELImporter . XBEL . DESC . startTag ( false ) ) ;
buffer . append ( CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . DESC . key ( ) , YMarkTables . BOOKMARK . DESC . deflt ( ) ) , true ) ) ;
buffer . append ( YMarksXBELImporter . XBEL . DESC . endTag ( false ) ) ;
prop . put ( "xbel_" + count + "_elements" , buffer . toString ( ) ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , YMarksXBELImporter . XBEL . BOOKMARK . endTag ( false ) ) ;
count + + ;
}
}
}
} catch ( IOException e ) {
Log . logException ( e ) ;
continue ;
@ -82,54 +156,20 @@ public class get_xbel {
}
}
}
while ( n > = YMarkIndex . getFolderDepth ( root ) ) {
prop . put ( "xbel_" + count + "_elements" , "</folder>" ) ;
while ( n > root_depth ) {
prop . put ( "xbel_" + count + "_elements" , YMarksXBELImporter . XBEL . FOLDER . endTag ( false ) ) ;
count + + ;
n - - ;
}
prop . put ( "user" , bmk_user . substring ( 0 , 1 ) . toUpperCase ( ) + bmk_user . substring ( 1 ) ) ;
prop . put ( "xbel" , count ) ;
} else {
prop . put ( YMarkTables . USER_AUTHENTICATE , YMarkTables . USER_AUTHENTICATE_MSG ) ;
}
// return rewrite properties
return prop ;
}
public static int putBookmarks ( final String folder , int count ) throws IOException , RowSpaceExceededException {
final Iterator < String > bit = sb . tables . bookmarks . folders . getBookmarks ( bmk_user , folder ) . iterator ( ) ;
Tables . Row bmk_row = null ;
String urlHash ;
while ( bit . hasNext ( ) ) {
urlHash = new String ( bit . next ( ) ) ;
bmk_row = sb . tables . select ( YMarkTables . TABLES . BOOKMARKS . tablename ( bmk_user ) , urlHash . getBytes ( ) ) ;
if ( bmk_row ! = null ) {
prop . put ( "xbel_" + count + "_elements" , "<bookmark id=\"b:" + urlHash
+ "\" href=\"" + CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . URL . key ( ) , YMarkTables . BOOKMARK . URL . deflt ( ) ) , true )
+ "\" added=\"" + CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_ADDED . key ( ) ) ) , true )
+ "\" modified=\"" + CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_MODIFIED . key ( ) ) ) , true )
+ "\" visited=\"" + CharacterCoding . unicode2xml ( YMarkTables . getISO8601 ( bmk_row . get ( YMarkTables . BOOKMARK . DATE_VISITED . key ( ) ) ) , true )
+ "\">" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "<title>"
+ CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . TITLE . key ( ) , YMarkTables . BOOKMARK . TITLE . deflt ( ) ) , true )
+ "</title>" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "<info>" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "<metadata owner=\"YaCy\""
+ " tags=\"" + bmk_row . get ( YMarkTables . BOOKMARK . TAGS . key ( ) , YMarkTables . BOOKMARK . TAGS . deflt ( ) ) + "\""
+ " public=\"" + bmk_row . get ( YMarkTables . BOOKMARK . PUBLIC . key ( ) , YMarkTables . BOOKMARK . PUBLIC . deflt ( ) ) + "\""
+ "/>" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "</info>" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "<desc>"
+ CharacterCoding . unicode2xml ( bmk_row . get ( YMarkTables . BOOKMARK . DESC . key ( ) , YMarkTables . BOOKMARK . DESC . deflt ( ) ) , true )
+ "</desc>" ) ;
count + + ;
prop . put ( "xbel_" + count + "_elements" , "</bookmark>" ) ;
count + + ;
}
}
return count ;
}
}