mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
Source: Restore broken commit
This commit is contained in:
71
400_xowa/src/gplx/xowa/htmls/doms/Xoh_dom_.java
Normal file
71
400_xowa/src/gplx/xowa/htmls/doms/Xoh_dom_.java
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.htmls.doms; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
|
||||
import gplx.langs.htmls.*;
|
||||
public class Xoh_dom_ {
|
||||
private static final byte[] Lt_bry = Bry_.new_a7("<"), Space_bry = Bry_.new_a7(" ");
|
||||
public static byte[] Query_val_by_where(Xoh_find rv, byte[] src, byte[] where_nde, byte[] where_key, byte[] where_val, byte[] query_key, int bgn) {
|
||||
int src_len = src.length;
|
||||
where_nde = Bry_.Add(Lt_bry, where_nde, Space_bry);
|
||||
while (true) {
|
||||
boolean where_val_found = Select_tag(rv, src, where_nde, where_key, bgn, src_len);
|
||||
if (where_val_found) {
|
||||
int tag_bgn = rv.Tag_bgn();
|
||||
int tag_end = rv.Tag_end();
|
||||
boolean where_val_match = Bry_.Match(src, rv.Val_bgn(), rv.Val_end(), where_val);
|
||||
if (where_val_match) {
|
||||
boolean query_val_found = Find_atr_val_in_tag(rv, src, query_key, tag_bgn, tag_end);
|
||||
if (query_val_found) {
|
||||
return Bry_.Mid(src, rv.Val_bgn(), rv.Val_end());
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
else
|
||||
bgn = tag_end + 1;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static boolean Select_tag(Xoh_find rv, byte[] src, byte[] nde, byte[] key, int rng_bgn, int rng_end) {
|
||||
int tag_bgn = Bry_find_.Find_fwd(src, nde, rng_bgn, rng_end); if (tag_bgn == Bry_find_.Not_found) return false;
|
||||
int tag_end = Bry_find_.Find_fwd(src, Byte_ascii.Gt, tag_bgn, rng_end); if (tag_end == Bry_find_.Not_found) return false;
|
||||
int key_bgn = Bry_find_.Find_fwd(src, key, tag_bgn, tag_end); if (key_bgn == Bry_find_.Not_found) return false;
|
||||
int key_end = key_bgn + key.length;
|
||||
int val_bgn = Bry_find_.Find_fwd(src, Byte_ascii.Quote, key_end, tag_end); if (val_bgn == Bry_find_.Not_found) return false;
|
||||
++val_bgn;
|
||||
int val_end = Bry_find_.Find_fwd(src, Byte_ascii.Quote, val_bgn, tag_end); if (val_end == Bry_find_.Not_found) return false;
|
||||
rv.Set_all(tag_bgn, tag_end, key_bgn, key_end, val_bgn, val_end);
|
||||
return true;
|
||||
}
|
||||
public static boolean Find_atr_val_in_tag(Xoh_find rv, byte[] src, byte[] key, int tag_bgn, int tag_end) {
|
||||
int key_bgn = Bry_find_.Find_fwd(src, key, tag_bgn, tag_end); if (key_bgn == Bry_find_.Not_found) return false;
|
||||
int key_end = key_bgn + key.length;
|
||||
int val_bgn = Bry_find_.Find_fwd(src, Byte_ascii.Quote, key_end, tag_end); if (val_bgn == Bry_find_.Not_found) return false;
|
||||
++val_bgn;
|
||||
int val_end = Bry_find_.Find_fwd(src, Byte_ascii.Quote, val_bgn, tag_end); if (val_end == Bry_find_.Not_found) return false;
|
||||
rv.Set_all(tag_bgn, tag_end, key_bgn, key_end, val_bgn, val_end);
|
||||
return true;
|
||||
}
|
||||
public static String Title_by_href(byte[] href, byte[] html_src) {
|
||||
byte[] xowa_title = Xoh_dom_.Query_val_by_where(dom_find, html_src, Gfh_tag_.Bry__a, Gfh_atr_.Bry__href, href, gplx.xowa.htmls.Xoh_consts.Atr_xowa_title_bry, 0);
|
||||
return String_.new_u8(xowa_title);
|
||||
} private static final Xoh_find dom_find = new Xoh_find();
|
||||
}
|
||||
45
400_xowa/src/gplx/xowa/htmls/doms/Xoh_dom_tst.java
Normal file
45
400_xowa/src/gplx/xowa/htmls/doms/Xoh_dom_tst.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.htmls.doms; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
|
||||
import org.junit.*;
|
||||
public class Xoh_dom_tst {
|
||||
@Test public void Find_atr() {
|
||||
String src = "a <nde0 atr0=\"val0\" atr1=\"val1\"/> b <nde1 atr0=\"val3\" atr1=\"val4\"/> c";
|
||||
tst_Find(src, "nde0", "atr0", "val0", "atr1", "val1"); // match nde0
|
||||
tst_Find(src, "nde1", "atr0", "val3", "atr1", "val4"); // match nde1
|
||||
tst_Find(src, "nde0", "atr0", "val5", "atr1", null); // wrong val
|
||||
tst_Find(src, "nde0", "atr2", "val0", "atr1", null); // wrong key
|
||||
tst_Find(src, "nde2", "atr0", "val0", "atr1", null); // wrong nde
|
||||
}
|
||||
@Test public void Title_by_href() {// PURPOSE: handle content-editable=n and finding file directly for download
|
||||
Title_by_href_tst
|
||||
( "/wiki/File:Bazille,_Fr%C3%A9d%C3%A9ric_%7E_Le_Petit_Jardinier_%28The_Little_Gardener%29,_c1866-67_oil_on_canvas_Museum_of_Fine_Arts,_Houston.jpg"
|
||||
, "<a href=\"lure\" xowa_title=\"wrong\"></a><a href=\"/wiki/File:Bazille,_Fr%C3%A9d%C3%A9ric_%7E_Le_Petit_Jardinier_%28The_Little_Gardener%29,_c1866-67_oil_on_canvas_Museum_of_Fine_Arts,_Houston.jpg\" xowa_title=\"find_me\"></a>"
|
||||
, "find_me"
|
||||
);
|
||||
}
|
||||
private void tst_Find(String src, String where_nde, String where_key, String where_val, String select_key, String expd) {
|
||||
Xoh_find rv = new Xoh_find();
|
||||
byte[] actl = Xoh_dom_.Query_val_by_where(rv, Bry_.new_u8(src), Bry_.new_u8(where_nde), Bry_.new_u8(where_key), Bry_.new_u8(where_val), Bry_.new_u8(select_key), 0);
|
||||
Tfds.Eq(expd, String_.new_u8(actl));
|
||||
}
|
||||
private void Title_by_href_tst(String href, String html_src, String expd) {
|
||||
String actl = Xoh_dom_.Title_by_href(Bry_.new_u8(href), Bry_.new_u8(html_src));
|
||||
Tfds.Eq(expd, actl);
|
||||
}
|
||||
}
|
||||
29
400_xowa/src/gplx/xowa/htmls/doms/Xoh_find.java
Normal file
29
400_xowa/src/gplx/xowa/htmls/doms/Xoh_find.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package gplx.xowa.htmls.doms; import gplx.*; import gplx.xowa.*; import gplx.xowa.htmls.*;
|
||||
public class Xoh_find {
|
||||
public int Tag_bgn() {return tag_bgn;} public Xoh_find Tag_bgn_(int v) {tag_bgn = v; return this;} private int tag_bgn;
|
||||
public int Tag_end() {return tag_end;} public Xoh_find Tag_end_(int v) {tag_end = v; return this;} private int tag_end;
|
||||
public int Key_bgn() {return key_bgn;} public Xoh_find Key_bgn_(int v) {key_bgn = v; return this;} private int key_bgn;
|
||||
public int Key_end() {return key_end;} public Xoh_find Key_end_(int v) {key_end = v; return this;} private int key_end;
|
||||
public int Val_bgn() {return val_bgn;} public Xoh_find Val_bgn_(int v) {val_bgn = v; return this;} private int val_bgn;
|
||||
public int Val_end() {return val_end;} public Xoh_find Val_end_(int v) {val_end = v; return this;} private int val_end;
|
||||
public void Set_all(int tag_bgn, int tag_end, int key_bgn, int key_end, int val_bgn, int val_end) {
|
||||
this.tag_bgn = tag_bgn; this.tag_end = tag_end; this.key_bgn = key_bgn; this.key_end = key_end; this.val_bgn = val_bgn; this.val_end = val_end;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user