Example usage for org.dom4j.io SAXReader setEncoding

List of usage examples for org.dom4j.io SAXReader setEncoding

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader setEncoding.

Prototype

public void setEncoding(String encoding) 

Source Link

Document

Sets encoding used for InputSource (null means system default encoding)

Usage

From source file:Tools.java

public static Map<String, String> regAppLicense(String appLicense) {
    if (null == appLicense || "".equals(appLicense))
        throw new RuntimeException("License");
    BASE64Decoder decoder = new BASE64Decoder();
    try {//from  w w  w.  j a  v  a  2 s  .com
        Map<String, String> ret = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        reader.setEncoding("UTF-8");
        Document doc = reader.read(new ByteArrayInputStream(decoder.decodeBuffer(appLicense)));
        Element root = doc.getRootElement();
        String app = root.elementTextTrim("models");
        ret.put("app", app);
        String content = root.elementTextTrim("content");
        String appPath = FileSystemWrapper.instance().getRealPath(app);
        File fApp = new File(appPath);
        if (!fApp.exists())
            throw new RuntimeException("?\"" + app + "\"?");
        File fAppLicenseDir = new File(appPath + "/license");
        fAppLicenseDir.mkdirs();
        FileOutputStream fo = null;
        try {
            fo = new FileOutputStream(appPath + "/license/license");
            fo.write(content.getBytes());
            fo.flush();
        } finally {
            if (null != fo)
                fo.close();
        }
        String developer = root.elementTextTrim("developer");
        ret.put("developer", developer);
        String validDate = root.elementTextTrim("valid-date");
        ret.put("valid-date", !"0".equals(validDate) ? validDate : "??");
        String userCount = root.elementTextTrim("user-count");
        ret.put("user-count", !"0".equals(userCount) ? userCount : "??");

        return ret;
    } catch (IOException e) {
        throw new RuntimeException("License", e);
    } catch (DocumentException e) {
        throw new RuntimeException("License", e);
    }
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Builds the document XML model up based the given reader of XML data.
 * /*from ww  w . ja v  a  2s  .  c  om*/
 * @param in
 *            the input stream used to build the xml document
 * @throws java.io.IOException
 *             thrown when an error occurs reading the input stream.
 */
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        log.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:cn.com.iscs.base.util.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 * // w  w w  .  ja  v a 2s. c  om
 * @param in
 *          the input stream used to build the xml document
 * @throws java.io.IOException
 *           thrown when an error occurs reading the input stream.
 */
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        System.err.print("Error reading XML properties");
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.adobe.ac.maven.ncss.NcssReportMojo.java

License:Apache License

private Document loadDocument(final File file, final String encoding) throws DocumentException {
    final SAXReader saxReader = new SAXReader();
    if (encoding != null) {
        saxReader.setEncoding(encoding);
        getLog().debug("Loading xml file with encoding : " + encoding);
    }//w ww . j  ava  2  s  .c  om
    return saxReader.read(file);
}

From source file:com.jaspersoft.jasperserver.export.ImporterImpl.java

License:Open Source License

protected Document readIndexDocument() {
    InputStream indexInput = getIndexInput();
    boolean close = true;
    try {/*from ww  w. ja v a 2  s  .  co  m*/
        SAXReader reader = new SAXReader();
        reader.setEncoding(getCharacterEncoding());
        Document document = reader.read(indexInput);

        close = false;
        indexInput.close();

        return document;
    } catch (IOException e) {
        log.error(e);
        throw new JSExceptionWrapper(e);
    } catch (DocumentException e) {
        log.error(e);
        throw new JSExceptionWrapper(e);
    } finally {
        if (close) {
            try {
                indexInput.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
}

From source file:com.jeeframework.util.xml.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 *
 * @param in the input stream used to build the xml document
 * @throws IOException thrown when an error occurs reading the input stream.
 *///from  ww w.j a  v  a 2s .  c  om
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        Log.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.mgmtp.perfload.core.common.xml.Dom4jReader.java

License:Apache License

/**
 * Creates a DOM4J documents from the specified {@link InputSource} and validates it against the
 * given schema./*from  w  w w .j av  a2  s.  co  m*/
 * 
 * @param xmlSource
 *            the source for the XML document
 * @param schemaSource
 *            the source for the schema
 * @param xIncludeAware
 *            specifies whether XIncludes should be supported
 * @return the DOM4J document
 */
public static Document loadDocument(final InputSource xmlSource, final Source schemaSource,
        final boolean xIncludeAware, final String encoding)
        throws ParserConfigurationException, SAXException, DocumentException {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true); // turn on validation
    factory.setNamespaceAware(true);

    //      factory.setFeature("http://apache.org/xml/features/validation/schema", true);
    if (xIncludeAware) {
        // allow XIncludes
        factory.setFeature("http://apache.org/xml/features/xinclude", true);
        factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
    }

    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    factory.setSchema(schemaFactory.newSchema(schemaSource));

    SAXParser parser = factory.newSAXParser();
    SAXReader reader = new SAXReader(parser.getXMLReader());
    reader.setEncoding(encoding);

    return reader.read(xmlSource);
}

From source file:com.oubeichen.gefexp.ShapesEditor.java

License:Open Source License

@Override
protected void setInput(IEditorInput input) {
    super.setInput(input);
    try {//from  w w w .j a  va 2s .  c  om
        IFile file = ((IFileEditorInput) input).getFile();
        if (!file.getName().contains(".raw.obsp")) {//XXX.raw.obsp
            ObjectInputStream in = new ObjectInputStream(file.getContents());
            diagram = (ShapesDiagram) in.readObject();
            in.close();
        } else {
            diagram = new ShapesDiagram();

            /*InputStreamReader isr = new InputStreamReader(file.getContents());
            BufferedReader reader = new BufferedReader(isr);
            String tempString, name;
            ArrayList<String> shapename = new ArrayList<String>();
            int shapenum, connnum;
            reader.readLine();//"Shape:"
            tempString = reader.readLine();//shape number
            try{
               shapenum = Integer.parseInt(tempString);
            }catch(NumberFormatException ex){
               shapenum = 0;
            }
            for(int i = 0;i < shapenum;i++){
               int height, width, x, y;
                name = reader.readLine();//shape name
                shapename.add(name);
                reader.readLine();//"height:"
                tempString = reader.readLine();//shape height
                try{
                   height = Integer.parseInt(tempString);
                }catch(NumberFormatException ex){
                   height = 0;
                }
                reader.readLine();//"width:"
                tempString = reader.readLine();//shape width
                try{
                   width = Integer.parseInt(tempString);
                }catch(NumberFormatException ex){
                   width = 0;
                }
                reader.readLine();//"location:"
                tempString = reader.readLine();//shape location
                try{
                   x = Integer.parseInt(tempString.split(" ")[0]);
                }catch(NumberFormatException ex){
                   x = 0;
                }
                try{
                   y = Integer.parseInt(tempString.split(" ")[1]);
                }catch(NumberFormatException ex){
                   y = 0;
                }
                reader.readLine();//empty line
                //add to the diagram
                Shape sp;
                if(name.contains("Ellipse")){
                   sp = new EllipticalShape();
                }else if(name.contains("Triangle")){
                   sp = new TriangularShape();
                }else if(name.contains("Rectangle")){
                   sp = new RectangularShape();
                }else{
                   System.err.println("Shape unsupported!");
                   sp = new EllipticalShape();
                }
                sp.setSize(new Dimension(width, height));
                sp.setLocation(new Point(x, y));
                diagram.addChild(sp);
            }
                    
            reader.readLine();//"Connections:"
            tempString = reader.readLine();//connection num
            try{
               connnum = Integer.parseInt(tempString);
            }catch(NumberFormatException ex){
               connnum = 0;
            }
                    
            for(int i = 0;i < connnum;i++)
            {
               String source = reader.readLine();
               String target = reader.readLine();
               reader.readLine();//empty line
               int sourceindex = shapename.indexOf(source);
               int targetindex = shapename.indexOf(target);
               if(sourceindex == -1 || targetindex == -1)
               {
                  System.err.println("Cannot find this shape!");
                  continue;
               }
               Shape sourceshape = (Shape) diagram.getChildren().get(sourceindex);
               Shape targetshape = (Shape) diagram.getChildren().get(targetindex);
               Connection conn = new Connection(sourceshape, targetshape);
            }
                    
            reader.close();*/

            SAXReader reader = new SAXReader();
            Document document;
            try {
                reader.setEncoding("UTF-8");
                document = reader.read(file.getContents());//
            } catch (DocumentException ex) {
                System.err.println("Cannot read input file!");
                ex.printStackTrace();
                throw new CoreException(null);
            }
            Element root = document.getRootElement();
            Element shaperoot = root.element("shapes");
            List list = shaperoot.elements("shape");
            Iterator it = list.iterator();
            ArrayList<String> shapename = new ArrayList<String>();
            while (it.hasNext()) {
                Element shapeElm = (Element) it.next();
                int height, width, x, y;
                String name = shapeElm.elementText("name");
                try {
                    height = Integer.parseInt(shapeElm.elementText("height"));
                    width = Integer.parseInt(shapeElm.elementText("width"));
                    x = Integer.parseInt(shapeElm.elementText("locx"));
                    y = Integer.parseInt(shapeElm.elementText("locy"));
                    Shape sp;
                    if (name.contains("Ellipse")) {
                        sp = new EllipticalShape();
                    } else if (name.contains("Triangle")) {
                        sp = new TriangularShape();
                    } else if (name.contains("Rectangle")) {
                        sp = new RectangularShape();
                    } else {
                        System.err.println("Shape unsupported!");
                        sp = new EllipticalShape();
                    }
                    diagram.addChild(sp);
                    sp.setSize(new Dimension(width, height));
                    sp.setLocation(new Point(x, y));
                    shapename.add(name);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            Element connroot = root.element("connections");
            list = connroot.elements("connection");
            it = list.iterator();
            while (it.hasNext()) {
                Element connElm = (Element) it.next();
                String source = connElm.elementText("source");
                String target = connElm.elementText("target");
                int sourceindex = shapename.indexOf(source);
                int targetindex = shapename.indexOf(target);
                if (sourceindex == -1 || targetindex == -1) {
                    System.err.println("Cannot find this shape!");
                    continue;
                }
                Shape sourceshape = (Shape) diagram.getChildren().get(sourceindex);
                Shape targetshape = (Shape) diagram.getChildren().get(targetindex);
                new Connection(sourceshape, targetshape);
            }
        }
        setPartName(file.getName());
    } catch (CoreException e) {
        handleLoadException(e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.surenpi.autotest.suite.parser.XmlSuiteParser.java

License:Apache License

/**
 * ??/* ww w.j  a v a 2  s.  c  om*/
 * @param suiteInputStream ??
 * @return 
 * @throws DocumentException
 */
public Suite parse(InputStream suiteInputStream) throws DocumentException {
    SAXReader reader = new SAXReader();
    reader.setEncoding("utf-8");

    Document document = reader.read(suiteInputStream);

    simpleNamespaceContext.addNamespace("ns", NS_URI);

    XPath xpath = new DefaultXPath("/ns:suite");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element suiteEle = (Element) xpath.selectSingleNode(document);
    if (suiteEle == null) {
        suiteEle = document.getRootElement();
        //         throw new RuntimeException("Can not found suite config.");
    }

    Suite suite = new Suite();
    String xmlConfPath = suiteEle.attributeValue("pageConfig");
    String pagePackage = suiteEle.attributeValue("pagePackage", "");
    String rows = suiteEle.attributeValue("rows", "1");
    String lackLines = suiteEle.attributeValue("lackLines", "nearby");
    String errorLines = suiteEle.attributeValue("errorLines", "stop");
    String afterSleep = suiteEle.attributeValue("afterSleep", "0");

    suite.setXmlConfPath(xmlConfPath);
    suite.setPagePackage(pagePackage);
    suite.setRows(rows);
    suite.setLackLines(lackLines);
    suite.setErrorLines(errorLines);
    suite.setAfterSleep(Long.parseLong(afterSleep));

    pagesParse(document, suite);

    return suite;
}

From source file:com.weibo.wesync.notify.xml.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 * @param in the input stream used to build the xml document
 * @throws java.io.IOException thrown when an error occurs reading the input stream.
 *//*  ww  w.java 2  s  .c om*/
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        xmlReader.setEncoding("UTF-8");
        document = xmlReader.read(in);
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}