Skip to content

Commit a7ec59e

Browse files
committed
- Fixed bug in the HTML Parser
- Added new method to return the size of a JAR entry - Fixed bug inserting/updating database records with byte arrays - Added more detailed error messages whenever a SQLException exception is thrown when updating a Recordset - Fixed bug parsing dates - Added new Generator class git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@693 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 95c925d commit a7ec59e

File tree

7 files changed

+205
-19
lines changed

7 files changed

+205
-19
lines changed

src/javaxt/html/Element.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
public class Element {
55

66
private String tagName;
7-
//private String html;
7+
private String tagHTML;
88

99
protected String innerHTML;
1010
protected String outerHTML;
@@ -21,9 +21,10 @@ public class Element {
2121
/**
2222
* @param html HTML used to define a tag (e.g. <div id="1">)
2323
*/
24-
protected Element(String html){
25-
26-
tag = html;
24+
protected Element(String tagHTML){
25+
this.tagHTML = tagHTML;
26+
27+
tag = tagHTML;
2728

2829
if (tag.startsWith("</")){
2930
isStartTag = false;
@@ -71,6 +72,10 @@ public boolean isEndTag(){
7172
public String getName(){
7273
return tagName;
7374
}
75+
76+
protected String getTag(){
77+
return tagHTML;
78+
}
7479

7580
public String getInnerHTML(){
7681
return innerHTML;

src/javaxt/html/Parser.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ else if(!Tag.isStartTag() && Tag.isEndTag()){
297297
foundEnd = (numEndTags>numStartTags);
298298
}
299299
else{
300+
//System.out.println(numEndTags + " vs " + numStartTags);
300301
foundEnd = (numEndTags>=numStartTags);
301302
}
302303

@@ -318,8 +319,18 @@ else if(!Tag.isStartTag() && Tag.isEndTag()){
318319
tag = new StringBuffer();
319320
}
320321

321-
322+
322323
}
324+
325+
326+
//Last ditch effort!
327+
if (Element!=null){
328+
if (Element.getOuterHTML()==null){
329+
Element.outerHTML = Element.getTag();
330+
}
331+
return Element;
332+
}
333+
323334
return null;
324335
}
325336

src/javaxt/http/Request.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private void initHeaders(){
154154
this.setHeader("Accept-Encoding", "gzip,deflate");
155155
this.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
156156
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.10)"); //windows xp
157-
this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"); //windows 7
157+
this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0"); //windows 7
158158
//this.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); //windows 8
159159
}
160160

src/javaxt/io/Jar.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ public Entry getEntry(String Entry){
279279
* (e.g. "Jar.class").
280280
*/
281281
public Entry getEntry(String Package, String Entry){
282-
282+
283+
ZipInputStream in = null;
283284
try{
284285

285286
if (file.isDirectory()){
@@ -299,7 +300,7 @@ public Entry getEntry(String Package, String Entry){
299300

300301

301302
//Find entry in the jar file
302-
ZipInputStream in = new ZipInputStream(new FileInputStream(file));
303+
in = new ZipInputStream(new FileInputStream(file));
303304
ZipEntry zipEntry = null;
304305
while((zipEntry = in.getNextEntry())!=null){
305306
if (zipEntry.getName().equalsIgnoreCase(Entry)){
@@ -313,6 +314,10 @@ public Entry getEntry(String Package, String Entry){
313314
}
314315
}
315316
catch(Exception e){
317+
if (in!=null){
318+
try{ in.close(); }
319+
catch(Exception ex){}
320+
}
316321
//e.printStackTrace();
317322
}
318323

@@ -495,12 +500,22 @@ public java.io.File getFile(){
495500
return fileEntry;
496501
}
497502

503+
public long getSize(){
504+
if (fileEntry==null){
505+
return zipEntry.getSize();
506+
}
507+
else{
508+
return fileEntry.length();
509+
}
510+
}
511+
498512

499513
public byte[] getBytes(){
514+
ZipFile zip = null;
500515
try{
501-
ZipFile zip = new ZipFile(file);
516+
502517
if (fileEntry==null){
503-
518+
zip = new ZipFile(file);
504519
java.io.DataInputStream is = new java.io.DataInputStream(zip.getInputStream(zipEntry));
505520

506521
int bufferSize = 1024;
@@ -516,16 +531,14 @@ public byte[] getBytes(){
516531
return bas.toByteArray();
517532
}
518533
else{
519-
byte[] b = new byte[(int)fileEntry.length()];
520-
java.io.DataInputStream is = new java.io.DataInputStream(new FileInputStream(fileEntry));
521-
is.readFully(b, 0, b.length);
522-
is.close();
523-
zip.close();
524-
return b;
534+
return new javaxt.io.File(fileEntry).getBytes().toByteArray();
525535
}
526536
}
527537
catch(Exception e){
528-
e.printStackTrace();
538+
if (zip!=null){
539+
try{ zip.close(); }
540+
catch(Exception ex){}
541+
}
529542
return null;
530543
}
531544
}

src/javaxt/sql/Recordset.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,28 @@ else if (FieldType.indexOf("map")>=0) //PostgreSQL HStore
729729

730730
//Update
731731
if (batchSize==1){
732-
stmt.executeUpdate();
732+
try{
733+
stmt.executeUpdate();
734+
}
735+
catch(SQLException e){
736+
737+
StringBuffer err = new StringBuffer();
738+
err.append("Error executing update:\n");
739+
err.append(sql.toString());
740+
err.append("\n");
741+
//err.append("\n Values:\n");
742+
for (int i=0; i<fields.size(); i++) {
743+
if (i>0) err.append("\n");
744+
Field field = fields.get(i);
745+
err.append(" - " + field.getName() + ": ");
746+
String val = field.getValue().toString();
747+
if (val!=null && val.length()>100) val = val.substring(0, 100) + "...";
748+
err.append(val);
749+
}
750+
751+
e.setNextException(new SQLException(err.toString()));
752+
throw e;
753+
}
733754

734755

735756
if (InsertOnUpdate){
@@ -778,7 +799,8 @@ private String getQ(Field field){
778799

779800

780801
//Special case for geometry types
781-
String packageName = value.getClass().getPackage().getName();
802+
java.lang.Package _package = value.getClass().getPackage();
803+
String packageName = _package==null ? "" : _package.getName();
782804
if (packageName.startsWith("javaxt.geospatial.geometry")){
783805
String STGeomFromText = getSTGeomFromText(field);
784806
field.Value = new Value(value.toString());

src/javaxt/utils/Date.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ public class Date implements Comparable {
7979
"M/d/yy h:mm:ss a", // 6/7/1976 1:02:09 PM
8080
"M/d/yy h:mm a", // 6/7/1976 1:02 PM
8181

82+
"MM/dd/yy HH:mm:ss Z", // 06/07/1976 13:02:09 America/New_York
83+
"MM/dd/yy HH:mm:ss", // 06/07/1976 13:02:09
84+
"MM/dd/yy HH:mm Z", // 06/07/1976 13:02 America/New_York
85+
"MM/dd/yy HH:mm", // 06/07/1976 13:02
86+
87+
"MM/dd/yyyy HH:mm:ss Z", // 06/07/1976 13:02:09 America/New_York
8288
"MM/dd/yyyy HH:mm:ss", // 06/07/1976 13:02:09
89+
"MM/dd/yyyy HH:mm Z", // 06/07/1976 13:02 America/New_York
8390
"MM/dd/yyyy HH:mm", // 06/07/1976 13:02
8491

8592
"M/d/yy", // 6/7/76
@@ -1181,6 +1188,9 @@ public static HashMap<String, String> getTimeZones(){
11811188
for (String[] pair : kvp) {
11821189
timezones.put(pair[0].toUpperCase(), pair[2]);
11831190
timezones.put(pair[1].toUpperCase(), pair[2]);
1191+
1192+
String nk = pair[0].toUpperCase().substring(pair[0].indexOf(" ")+1);
1193+
if (!timezones.containsKey(nk)) timezones.put(nk, pair[2]);
11841194
}
11851195
}
11861196
}

src/javaxt/utils/Generator.java

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package javaxt.utils;
2+
import java.util.Iterator;
3+
import java.util.NoSuchElementException;
4+
5+
//******************************************************************************
6+
//** Generator
7+
//******************************************************************************
8+
/**
9+
* A custom iterator that yields its values one at a time. This is a great
10+
* alternative to arrays or lists when dealing with large data. By yielding
11+
* one entry at a time, this iterator can help avoid out of memory exceptions.
12+
*
13+
* Subclasses must define a method called {@link #run()} and may call
14+
* {@link yield(T)} to return values one at a time.
15+
*
16+
* @author Adrian Kuhn &lt;akuhn(at)iam.unibe.ch&gt;
17+
* @see http://smallwiki.unibe.ch/adriankuhn/yield4java/
18+
*
19+
******************************************************************************/
20+
21+
public abstract class Generator<T> implements Iterable<T> {
22+
23+
public abstract void run();
24+
25+
public Iterator<T> iterator() {
26+
return new Iter();
27+
}
28+
29+
private static final Object DONE = new Object();
30+
private static final Object EMPTY = new Object();
31+
private Object drop = EMPTY;
32+
private Thread th = null;
33+
34+
private synchronized Object take() {
35+
while (drop == EMPTY) {
36+
try {
37+
wait();
38+
} catch (InterruptedException ex) {
39+
Thread.currentThread().interrupt();
40+
}
41+
}
42+
Object temp = drop;
43+
if (drop != DONE)
44+
drop = EMPTY;
45+
notifyAll();
46+
return temp;
47+
}
48+
49+
private synchronized void put(Object value) {
50+
if (drop == DONE)
51+
throw new IllegalStateException();
52+
if (drop != EMPTY)
53+
throw new IllegalStateException();
54+
drop = value;
55+
notifyAll();
56+
while (drop != EMPTY) {
57+
try {
58+
wait();
59+
} catch (InterruptedException ex) {
60+
Thread.currentThread().interrupt();
61+
}
62+
}
63+
}
64+
65+
protected void yield(T value) {
66+
put(value);
67+
}
68+
69+
public synchronized void done() {
70+
if (drop == DONE)
71+
throw new IllegalStateException();
72+
if (drop != EMPTY)
73+
throw new IllegalStateException();
74+
drop = DONE;
75+
notifyAll();
76+
}
77+
78+
private class Iter implements Iterator<T>, Runnable {
79+
80+
private Object next = EMPTY;
81+
82+
public Iter() {
83+
if (th != null)
84+
throw new IllegalStateException("Can not run coroutine twice");
85+
th = new Thread(this);
86+
th.setDaemon(true);
87+
th.start();
88+
}
89+
90+
public void run() {
91+
Generator.this.run();
92+
done();
93+
}
94+
95+
public boolean hasNext() {
96+
if (next == EMPTY)
97+
next = take();
98+
return next != DONE;
99+
}
100+
101+
@SuppressWarnings("unchecked")
102+
public T next() {
103+
if (next == EMPTY)
104+
next = take();
105+
if (next == DONE)
106+
throw new NoSuchElementException();
107+
Object temp = next;
108+
next = EMPTY;
109+
return (T) temp;
110+
}
111+
112+
public void remove() {
113+
throw new UnsupportedOperationException();
114+
115+
}
116+
117+
@SuppressWarnings("deprecation")
118+
@Override
119+
protected void finalize() throws Throwable {
120+
th.stop(); // let's commit suicide
121+
}
122+
123+
}
124+
125+
}

0 commit comments

Comments
 (0)