xmlhelper.java

来自「jdo开发实例,一个功能全面的oa系统」· Java 代码 · 共 342 行 · 第 1/2 页

JAVA
342
字号
                                mergeToXML.appendChild(n);                        }                        return;                } else {                        mergeToXML.appendChild(copyElem);                }        }        /*        public static Document tidyHTML(String url) throws XMLHelperException {                return tidyHTML(convertStringToURL(url));        }        public static Document tidyHTML(URL url) throws XMLHelperException {                try {                        URLConnection inConnection = url.openConnection();                        if (inConnection.getContentType().startsWith("text/xml") ||                                inConnection.getContentType().startsWith("text/xhtml")) {                                // All ready an XML source                                return parseXMLFromURL(url);                        } else if (inConnection.getContentType().startsWith("text/html")) {                                // An HTML source                                InputStream is = inConnection.getInputStream();                                // Clean the input stream                                ByteArrayOutputStream out = new ByteArrayOutputStream();                                int totalBytes = 0;                                byte[] buffer = new byte[16384];                                while (true) {                                        int bytesRead = is.read(buffer, 0, buffer.length);                                        if (bytesRead < 0) break;                                        // Remove binary bellow space except tab and newline                                        for (int i=0; i < bytesRead; i++) {                                                byte b = buffer[i];                                                if (b < 32 && b!= 10 && b != 13 && b != 9) b = 32;                                                buffer[i] = b;                                        }                                        out.write(buffer, 0, bytesRead);                                        totalBytes += bytesRead;                                }                                is.close();                                out.close();                                String outContent = out.toString();                                InputStream in = new ByteArrayInputStream(out.toByteArray());                                org.w3c.tidy.TagTable tags = org.w3c.tidy.TagTable.getDefaultTagTable();                                tags.defineBlockTag("script");                                tags.defineBlockTag("nowrap");                                Tidy tidy = new Tidy();                                //tidy.setMakeClean(true);                                tidy.setShowWarnings(false);                                tidy.setXmlOut(true);                                tidy.setXmlPi(false);                                tidy.setDocType("omit");                                //tidy.setQuoteNbsp(true);                                //tidy.setQuoteAmpersand(true);                                tidy.setXHTML(false);                                tidy.setRawOut(true);                                tidy.setNumEntities(true);                                tidy.setQuiet(true);                                tidy.setFixComments(true);                                tidy.setIndentContent(true);                                tidy.setCharEncoding(org.w3c.tidy.Configuration.ASCII);                                ByteArrayOutputStream baos = new ByteArrayOutputStream();                                //Document resultDoc = tidy.parseDOM(in, null);                                //if (result == null) System.err.println("Null sucker");                                //tidy.pprint(resultDoc, baos);                                org.w3c.tidy.Node tNode = tidy.parse(in, baos);                                String result = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n" +                                                                baos.toString();                                // Strip the DOCTYPE and script elements                                int startIndex = 0;                                int endIndex = 0;                                if ((startIndex = result.indexOf("<!DOCTYPE")) >= 0) {                                        endIndex = result.indexOf(">",startIndex);                                        result = result.substring(0,startIndex) +                                                         result.substring(endIndex + 1, result.length());                                }                                while ((startIndex = result.indexOf("<script")) >= 0) {                                        endIndex = result.indexOf("</script>");                                        result = result.substring(0,startIndex) +                                                         result.substring(endIndex + 9, result.length());                                }                                in.close();                                baos.close();                                return parseXMLFromString(result);                        } else {                                throw new XMLHelperException("Unable to tidy content type: " +                                                                                         inConnection.getContentType());                        }                } catch (IOException ioe) {                        throw new XMLHelperException("Unable to perform input/output", ioe);                }        }        */        public static void main(String args[]) {                if (args.length < 3) {                        printUsage();                        System.exit(0);                }                File xml_in  = new File(args[0]);                File xsl_in  = new File(args[1]);                File xml_out = new File(args[2]);                Date timestamps[] = new Date[5];                String encoding = null;                if (args.length > 3 && !args[3].equals("report")) encoding = args[3];                try {                        timestamps[0] = new Date();                        Document xml = parseXMLFromFile(xml_in);                        if (encoding != null) {                                xml.getDocumentElement().setAttribute("locale", encoding);                        }                        timestamps[1] = new Date();                        Document xsl = parseXMLFromFile(xsl_in);                        timestamps[2] = new Date();                        Document result = transformXML(xml, xsl);                        timestamps[3] = new Date();                        outputXMLToFile(result, xml_out.getAbsolutePath());                        timestamps[4] = new Date();                } catch (Exception ex) {                        System.err.println("An Error was encountered: " + ex.getMessage());                        System.exit(0);                }                if ((args.length > 3 && args[3].equals("report")) || (args.length > 4 && args[4].equals("report"))) {                        System.out.println("Time to parse XML input:        " + getTimeDiff(timestamps[0], timestamps[1]));                        System.out.println("Time to parse XSL input:        " + getTimeDiff(timestamps[1], timestamps[2]));                        System.out.println("Time to perform transformation: " + getTimeDiff(timestamps[2], timestamps[3]));                        System.out.println("Time to output XML/HTML:        " + getTimeDiff(timestamps[3], timestamps[4]));                        System.out.println("Total elapsed time:             " + getTimeDiff(timestamps[0], timestamps[4]));                }        }        private static void printUsage() {                System.out.println("XMLHelper Usage:");                System.out.println("\t~> java XMLHelper xml_input_file xsl_input_file output_file [i18n encoding] ['report']");                System.out.println("\tEx: ~> java XMLHelper XML/user_interests.xml XSL/user_interests_xsl_only.xsl result.html report");        }        private static String getTimeDiff(Date date1, Date date2) {                long ts1 = date1.getTime();                long ts2 = date2.getTime();                long diff = ts2 - ts1;                double d = (double)diff / 1000.0;                return String.valueOf(d) + " seconds";        }        private static URL convertStringToURL(String url) throws XMLHelperException {                try {                        return new URL(url);                } catch (MalformedURLException murle) {                        throw new XMLHelperException(url + " is not a well formed URL", murle);                }        }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?