📄 filetoelement.java
字号:
package net.aetherial.gis.surface;
import java.io.*;
import net.aetherial.gis.garmin.*;
import org.w3c.dom.*;
import net.aetherial.gis.our.ToMap;
import net.aetherial.gis.util.Encryption;
import net.aetherial.gis.our.FrameOur;
public class FileToElement
extends Thread {
private FrameMain fm = null;
private FrameOur fo = null;
File f = null;
int pos = 0;
String content = "";
private DialogProgress dp = new DialogProgress();
public FileToElement() {
}
public FileToElement(File f) {
dp.show();
}
public FileToElement(File f, FrameMain fm) {
dp.show();
this.f = f;
this.fm = fm;
}
public FileToElement(FrameMain fm) {
this.fm = fm;
}
public void setFile(File f) {
this.f = f;
this.dp.show();
}
public void setDpHide() {
this.dp.hide();
}
public FileToElement(File f, FrameOur fo) {
if (ItemValue.getShowDialogMessage()) {
dp.show();
}
this.f = f;
this.fo = fo;
}
public void run() {
ItemValue.nianBao = !(f.getAbsolutePath().indexOf("非年报") > -1 || f.getAbsolutePath().indexOf("村") > -1);
ItemValue.ganXian = "G|S|X|Y|g|s|x|y".indexOf(""+ f.getName().charAt(0)) > -1;
ItemValue.ganXianName = f.getName().substring(0,f.getName().indexOf("."));
long idTemp = 0;
if (ItemValue.encrypt == true) {
try {
this.readEncryptFile();
}
catch (Exception ex1) {
ItemValue.addError("文件:" + f.getAbsolutePath() + "不能打开。原因是:" + ex1.getMessage() +"。");
System.out.println(f.getAbsolutePath() + "不能打开。");
if (this.fm != null) {
fm.catchReadException(ex1);
}else{
this.setDpHide();
}
return;
}
}
else {
this.readFile();
}
if (content.length() > 2) {
//if (content.indexOf("<gml:name>") != -1) {
// content = content.replaceAll("gml:Point",
// "gml_Point").replaceAll("gml:name",
// "gml_name").replaceAll("gml:coord", "gml_coord")
//
// .replaceAll("gml:LineString", "gml_LineString")
// .replaceAll("gml:number", "gml_number")
// .replaceAll("gml:grade", "gml_grade")
// .replaceAll("gml:type", "gml_type")
// .replaceAll("gml:width", "gml_width")
// .replaceAll("gml:structure", "gml_structure")
// .replaceAll("gml:status", "gml_status");
ByteArrayInputStream in = null;
byte[] buf = null;
try {
buf = content.getBytes("UTF-8");
}
catch (UnsupportedEncodingException ex) {
}
in = new ByteArrayInputStream(buf);
//InputStream in = new ByteArrayInputStream(buf);
FileUseJDom fujd = new FileUseJDom(in);
fujd.getTracks();
try {
fujd.getWaypoints();
}
catch (Exception ex2) {
}
ItemValue.idLeast = fujd.IDleast;
idTemp = fujd.IDlargest;
// }
if (fm != null) {
fm.memoryShow();
}
if (fo != null) {
fo.memoryShow();
}
//fm.insertFileText();
//dif.hide();
//ItemValue.setNewID();
// ItemValue.setIDOrder();
if (idTemp > ItemValue.idLargest) {
ItemValue.idLargest = idTemp;
}
ItemValue.setOldWaypointNum();
ItemValue.setOldTracksNum();
//ToMap.rollMemory();
in = null;
buf = null;
}
dp.hide();
if (this.fo != null) {
if (this.fo.batch) {
ItemValue.showLog(null);
}
else {
ItemValue.showLog(this.fo);
}
}
if (this.fm != null) {
ItemValue.showLog(this.fm);
}
}
void readFile() {
String line;
int countTest = 0;
try {
FileInputStream in = new FileInputStream(f);
int l = Integer.parseInt(f.length() + "");
byte[] b = new byte[l];
int inttemp;
if (l > 0) {
while (true) {
if ( (inttemp = in.read(b)) == -1) {
break;
}
this.content = this.content + (line = new String(b, "UTF-8"));
;
dp.jTextField1.setText(line);
countTest++;
}
}
in.close() ;
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("countTest:" + countTest);
}
void readEncryptFile() throws Exception {
String line;
try {
FileInputStream in = new FileInputStream(f);
int l = Integer.parseInt(f.length() + "");
byte[] b = new byte[l];
int inttemp;
if (l > 0) {
while (true) {
if ( (inttemp = in.read(b)) == -1) {
break;
}
this.content = Encryption.decryptToString(b, Encryption.key);
//this.content = this.content + (line = new String(b, "UTF-8"));
dp.jTextField1.setText("");
}
}
in.close();
}
catch (Exception ex) {
throw ex;
}
}
void printContent() {
System.out.print(content);
}
void stringToElement(String str) {
int trackStart = str.indexOf("<track>");
getWayPoint(str);
if (trackStart != -1) {
str = str.substring(trackStart);
ItemValue.hasTrack = true;
int tracksEnd = 0;
while (true) {
//如果找不到"<track>",跳出循环
if (str.indexOf("<track>") == -1) {
break;
}
//得到航迹的名称
Element e = this.getTracksName(str);
if (e == null) {
break;
}
//找到"</track>",
tracksEnd = str.indexOf("</track>");
//取航迹点
getTracksPoint(e, str.substring(0, tracksEnd));
//截取从(tracksPos+8)-->(末尾)的字符串,作为下一个循环的标准。如果没有直接跳出
if (tracksEnd != -1) {
str = str.substring(tracksEnd + 8);
}
else {
break;
}
//pos = 0;
//System.out.print(str);
}
}
content = "";
// GarminGMLDoc.addXmlTrackPoint(e,0,0,0,"2004");
}
Element getTracksName(String string) {
int tracksPos = string.indexOf("<track>");
if (tracksPos != -1) {
string = string.substring(tracksPos);
this.pos = tracksPos;
System.out.println("tracks item pos:" + tracksPos);
Element e = null;
String trackNameBefore = "<gml:name>",
trackNameAfter = "</gml:name>";
int pos1 = string.indexOf(trackNameBefore);
int pos2 = string.indexOf(trackNameAfter);
if (pos1 == -1) {
return null;
}
if (pos2 == -1) {
return null;
}
System.out.println("Pos info:pos1:" + (pos1 + 10) + ",pos2:" + pos2 +
",pos:" + pos);
String tracksName = string.substring(pos1 + 10, pos2);
//fm.insertTracksToTree(tracksName);
pos = pos + pos2 + 26;
tracksPos = string.indexOf("<track>");
if (tracksPos != -1) {
string = string.substring(tracksPos);
this.pos = tracksPos + pos;
}
ItemValue.tracksNum++;
// e = GarminGMLDoc.addXmlTrack(tracksName, getTracksNumber(string),
// getTracksGrade(string), getTracksType(string),
// getTracksWidth(string),
// getTracksStructure(string),
// getTracksStatus(string), "", "", "", "", "",
// "", "", "", "", "", "", "", "", "", "", "",
// "");
return e;
}
else {
return null;
}
}
String getTracksNumber(String string) {
String trackNumberBefore = "<gml:number>",
trackNumberAfter = "</gml:number>";
int pos1 = string.indexOf(trackNumberBefore);
int pos2 = string.indexOf(trackNumberAfter);
if (pos1 != -1) {
pos = pos + pos2 + 13;
if (pos1 == -1) {
return null;
}
String tracksNumber = string.substring(pos1 + 12, pos2);
return tracksNumber;
}
else {
return "";
}
}
String getTracksGrade(String string) {
String trackGradeBefore = "<gml:grade>",
trackGradeAfter = "</gml:grade>";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -