Example usage for javax.xml.parsers SAXParser parse

List of usage examples for javax.xml.parsers SAXParser parse

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser parse.

Prototype

public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException 

Source Link

Document

Parse the content given org.xml.sax.InputSource as XML using the specified org.xml.sax.helpers.DefaultHandler .

Usage

From source file:org.codehaus.enunciate.modules.xml.XMLDeploymentModule.java

/**
 * Pretty-prints the specified xml file.
 *
 * @param file The file to pretty-print.
 *//* www.ja  v  a2s .  c  om*/
protected void prettyPrint(File file) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(false);
        SAXParser parser = factory.newSAXParser();
        File prettyFile = enunciate.createTempFile("enunciate", file.getName());
        parser.parse(file, new PrettyPrinter(prettyFile));

        if (file.delete()) {
            enunciate.copyFile(prettyFile, file);
        } else {
            warn("Unable to delete %s.  Skipping pretty-print transformation....", file);
        }
    } catch (Exception e) {
        //fall through... skip pretty printing.
        warn("Unable to pretty-print %s (%s).", file, e.getMessage());
        if (enunciate.isDebug()) {
            e.printStackTrace(System.err);
        }
    }
}

From source file:org.corpus_tools.pepper.cli.PepperStarter.java

private Map<String, String[]> getModuleTable() throws ParserConfigurationException, SAXException, IOException {
    if (this.moduleTable != null) {
        return moduleTable;
    }/*from w  w  w. jav  a2 s  . co m*/
    HashMap<String, String[]> table = new HashMap<String, String[]>();
    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    try {
        saxParser.parse(MODULES_XML_PATH, new ModuleTableReader(table));
    } catch (Exception e) {
        logger.debug("Could not parse modules.xml", e);
    }
    return table;
}

From source file:org.dcm4che.tool.dcm2xml.Dcm2Xml.java

private static Attributes parseXML(String fname) throws Exception {
    Attributes attrs = new Attributes();
    ContentHandlerAdapter ch = new ContentHandlerAdapter(attrs);
    SAXParserFactory f = SAXParserFactory.newInstance();
    SAXParser p = f.newSAXParser();
    p.parse(new File(fname), ch);
    return attrs;
}

From source file:org.dcm4che.tool.xml2dcm.Xml2Dcm.java

private static void parseXML(String fname, ContentHandlerAdapter ch) throws Exception {
    SAXParserFactory f = SAXParserFactory.newInstance();
    SAXParser p = f.newSAXParser();
    if (fname.equals("-")) {
        p.parse(System.in, ch);
    } else {/*from w  w  w.j a  v  a  2s.c o  m*/
        p.parse(new File(fname), ch);
    }
}

From source file:org.dcm4che2.tool.chess3d.Chess3D.java

public void extrude(File xmlFile, int slices) throws IOException, ParserConfigurationException, SAXException {
    DicomObject obj = new BasicDicomObject();
    String fn;/*from  w  w  w .  j  a  va  2  s .  co m*/
    File parent;
    if (xmlFile != null) {
        SAXParserFactory f = SAXParserFactory.newInstance();
        SAXParser p = f.newSAXParser();
        ContentHandlerAdapter ch = new ContentHandlerAdapter(obj);
        p.parse(xmlFile, ch);
        fn = obj.getString(Tag.PatientID);
        if (fn == null)
            fn = xmlFile.getName();
        parent = destDir == null ? xmlFile.getParentFile() : destDir;
    } else {
        fn = "test";
        parent = destDir == null ? null : destDir;
    }
    File oFile;
    String ext;
    int pos = fn.lastIndexOf('.');
    if (pos != -1) {
        ext = fn.substring(pos);//we want '.' too
        fn = fn.substring(0, pos);
    } else {
        ext = "";
    }
    fn += "_";
    obj = prepare(obj);
    prepareLineBuffer();
    int imageSize = lineLength * yRect * rectHeight;
    for (int z = 0; z < zRect; z++) {
        for (int i = 0; i < slices; i++) {
            System.out.print("*");
            chgAttributes(obj, i);
            oFile = new File(parent, fn + z + "_" + (i) + ext);
            DicomOutputStream dos = new DicomOutputStream(oFile);
            dos.writeDicomFile(obj);
            dos.writeHeader(Tag.PixelData, VR.OB, (imageSize + 1) & ~1);
            writePixelData(dos, z);
            if ((imageSize & 1) != 0)
                dos.write(0);
            dos.close();
        }
    }
}

From source file:org.dcm4che2.tool.dcmof.DcmOF.java

private DicomObject loadXML(File f) throws Exception {
    DicomObject dcmobj = new BasicDicomObject();
    SAXParser p = SAXParserFactory.newInstance().newSAXParser();
    ContentHandlerAdapter ch = new ContentHandlerAdapter(dcmobj);
    p.parse(f, ch);
    return dcmobj;
}

From source file:org.dcm4che2.tool.dcmups.DcmUPS.java

private static DicomObject loadXML(File f) throws Exception {
    DicomObject dcmobj = new BasicDicomObject();
    SAXParser p = SAXParserFactory.newInstance().newSAXParser();
    ContentHandlerAdapter ch = new ContentHandlerAdapter(dcmobj);
    p.parse(f, ch);
    return dcmobj;
}

From source file:org.dcm4che2.tool.xml2dcm.Xml2Dcm.java

private static void parseXML(String xmlFile, DicomObject dcmobj, String baseDir)
        throws FactoryConfigurationError, ParserConfigurationException, SAXException, IOException {
    SAXParserFactory f = SAXParserFactory.newInstance();
    SAXParser p = f.newSAXParser();
    ContentHandlerAdapter ch = new ContentHandlerAdapter(dcmobj);
    if (xmlFile != null) {
        p.parse(new File(xmlFile), ch);
    } else if (baseDir != null) {
        String uri = "file:" + new File(baseDir, "STDIN").getAbsolutePath();
        if (File.separatorChar == '\\') {
            uri = uri.replace('\\', '/');
        }/*from   w  w w  .  j a v a2s  . co  m*/
        p.parse(System.in, ch, uri);
    } else {
        p.parse(System.in, ch);
    }
}

From source file:org.dspace.testing.PubMedToImport.java

public static void main(String args[]) {
    Options options = new Options();

    options.addOption(new Option("s", "source", true, "Source xml"));
    options.addOption(new Option("o", "output", true, "Output directory"));

    try {//from   w  ww.j  a v  a 2  s  . c o m
        CommandLine cli = new PosixParser().parse(options, args);

        String source = cli.getOptionValue("s");
        String output = cli.getOptionValue("o");

        if (!new File(source).exists()) {
            throw new IllegalArgumentException("Source file does not exist");
        }

        outputDir = new File(output);
        if (outputDir.exists()) {
            if (outputDir.list().length > 0) {
                throw new IllegalStateException("Output directory must be empty");
            }
        } else {
            if (!outputDir.mkdirs()) {
                throw new IllegalStateException("Unable to create output directory");
            }
        }

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        saxParser.parse(source, new PubMedHandler());

    } catch (Exception e) {

    }
}

From source file:org.easyxml.parser.EasySAXParser.java

/**
 * /*from   w w  w  .  j av  a2  s . co  m*/
 * Parse XML as an InputSource. If external schema is referred, then it
 * would fail to locate the DTD file.
 * 
 * @param is
 *            - InputSource to be parsed.
 * 
 * @return org.easyxml.xml.Document instance if it is parsed successfully,
 *         otherwise null.
 */
public static Document parse(InputSource is) {

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        saxParserFactory.setValidating(true);
        saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema", true);
        SAXParser saxParser = saxParserFactory.newSAXParser();
        EasySAXParser handler = new EasySAXParser();
        saxParser.parse(is, handler);
        return handler.getDocument();
    } catch (SAXException | IOException | ParserConfigurationException e) {
        e.printStackTrace();
    }

    return null;
}