URLConnection con = url.openConnection(); con.connect(); InputStream urlfs; urlfs = con.getInputStream(); byte[] buf = new byte[4096]; UniversalDetector detector = new UniversalDetector(null); int nread; while ((nread = urlfs.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), encoding)); String str; while ((str = in.readLine()) != null) { string = string + str; } in.close();
string = string.replace(" ", " "); /* . , . */ string = string + " ";
int count = 0; for (char c : string.toCharArray()) if (c == ' ') count++; loop = (count-1);
/* ". ". . */ String[] authorandtitle = parsedstring[2].split("\\."); AUTHOR = authorandtitle[0]; TITLE = authorandtitle[1]; /* , . . . , */ for(int i = 4; i <= loop; i++){ if((i % 2) ==0 ){ CHAPTER[i] = parsedstring[i]; HEADER[i] = parsedstring[i]; }else{ PARAG[i] = parsedstring[i]; } }
Publication epub = new Publication(); epub.addDCMetadata("title", TITLE); epub.addDCMetadata("creator", AUTHOR); epub.addDCMetadata("language", "ru-RU");
DataSource dataSource = new FileDataSource(new File(cover)); BitmapImageResource imageResource = epub.createBitmapImageResource( "OPS/images/cover.jpg", "image/jpeg", dataSource); DataSource coverdata = new StringDataSource("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Cover</title>\n<style type=\"text/css\"> img { max-width: 100%; } </style>\n</head>\n<body>\n<div id=\"cover-image\">\n<img src=\"images/cover.jpg\" alt=\"Title\"/>\n</div>\n</body>\n</html>"); Resource coverres = epub.createResource("OPS/cover.xhtml", "xhtml", coverdata); epub.addToSpine(coverres);
NCXResource toc = epub.getTOC(); TOCEntry rootTOCEntry = toc.getRootTOCEntry(); for(int i = 4; i <= loop; i++){ if((i % 2) ==0 ){ /* .*/ OPSResource main = epub.createOPSResource("OPS/"+i+".html"); epub.addToSpine(main); /* . */ mainDoc = main.getDocument(); /* .*/ TOCEntry mainTOCEntry = toc.createTOCEntry(CHAPTER[i], mainDoc .getRootXRef()); rootTOCEntry.add(mainTOCEntry); body = mainDoc.getBody(); /* . */ Element h1 = mainDoc.createElement("h1"); h1.add(HEADER[i]); body.add(h1); }else{ /* . */ Element paragraph = mainDoc.createElement("p"); paragraph.add(PARAG[i]); body.add(paragraph); } }
OCFContainerWriter writer = new OCFContainerWriter( new FileOutputStream(output)); epub.serialize(writer);
Source: https://habr.com/ru/post/127677/
All Articles