Example usage for org.apache.commons.digester Digester parse

List of usage examples for org.apache.commons.digester Digester parse

Introduction

In this page you can find the example usage for org.apache.commons.digester Digester parse.

Prototype

public Object parse(URL url) throws IOException, SAXException 

Source Link

Document

Parse the content of the specified URL using this Digester.

Usage

From source file:org.eclipse.wb.internal.core.xml.model.description.ComponentDescriptionHelper.java

private static ComponentDescription getDescriptionEx(EditorContext context, Class<?> componentClass)
        throws Exception {
    ComponentDescription componentDescription = new ComponentDescription(componentClass);
    // prepare description resources, from generic to specific
    LinkedList<ClassResourceInfo> descriptionInfos;
    {//  www. j  ava  2 s . com
        descriptionInfos = Lists.newLinkedList();
        DescriptionHelper.addDescriptionResources(descriptionInfos, context.getLoadingContext(),
                componentClass);
        Assert.isTrueException(!descriptionInfos.isEmpty(), IExceptionConstants.DESCRIPTION_NO_DESCRIPTIONS,
                componentClass.getName());
    }
    // prepare Digester
    Digester digester;
    {
        digester = new Digester();
        digester.setLogger(new NoOpLog());
        addRules(digester, context, componentClass);
    }
    // read descriptions from generic to specific
    for (ClassResourceInfo descriptionInfo : descriptionInfos) {
        ResourceInfo resourceInfo = descriptionInfo.resource;
        // read next description
        {
            //componentDescription.setCurrentClass(descriptionInfo.clazz);
            digester.push(componentDescription);
            // do parse
            InputStream is = resourceInfo.getURL().openStream();
            try {
                digester.parse(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        // clear parts that can not be inherited
        if (descriptionInfo.clazz == componentClass) {
            setDescriptionWithInnerTags(componentDescription, resourceInfo);
        } else {
            componentDescription.clearCreations();
            componentDescription.setDescription(null);
        }
    }
    // set toolkit
    if (componentDescription.getToolkit() == null) {
        for (int i = descriptionInfos.size() - 1; i >= 0; i--) {
            ClassResourceInfo descriptionInfo = descriptionInfos.get(i);
            ToolkitDescription toolkit = descriptionInfo.resource.getToolkit();
            if (toolkit != null) {
                componentDescription.setToolkit(toolkit);
                break;
            }
        }
    }
    // mark for caching presentation
    if (shouldCachePresentation(descriptionInfos.getLast(), componentClass)) {
        componentDescription.setPresentationCached(true);
    }
    // final operations
    setIcon(context, componentDescription, componentClass);
    useDescriptionProcessors(context, componentDescription);
    componentDescription.postProcess();
    // done
    return componentDescription;
}

From source file:org.eclipse.wb.internal.swing.ams.model.property.ConfigurationReader.java

private static List<PropertyGroup> parsePropertyGroups(InputStream input) throws Exception {
    final List<PropertyGroup> groups = Lists.newArrayList();
    // prepare Digester
    Digester digester;
    {//from  w w  w . j  av  a2s. co  m
        digester = new Digester();
        digester.setLogger(new NoOpLog());
        // groups/group
        {
            String pattern = "groups/group";
            digester.addRule(pattern, new Rule() {
                @Override
                public void begin(String namespace, String element, Attributes attributes) throws Exception {
                    // prepare required name
                    String name = attributes.getValue("name");
                    Assert.isNotNull(name, Messages.ConfigurationReader_errGroup_noNameAttribute);
                    // prepare optional description
                    String description = attributes.getValue("description");
                    Assert.isNotNull(name, Messages.ConfigurationReader_errGroup_noDescriptionAttribute);
                    // prepare optional category
                    PropertyCategory category = null;
                    {
                        String categoryString = attributes.getValue("category");
                        if (categoryString != null) {
                            category = PropertyCategory.get(categoryString, null);
                        }
                    }
                    // add group
                    PropertyGroup group = new PropertyGroup(name, description, category);
                    digester.push(group);
                    groups.add(group);
                }

                @Override
                public void end(String namespace, String name) throws Exception {
                    digester.pop();
                    super.end(namespace, name);
                }
            });
        }
        // groups/group/property
        {
            String pattern = "groups/group/property";
            digester.addRule(pattern, new Rule() {
                @Override
                public void begin(String namespace, String name, Attributes attributes) throws Exception {
                    // prepare required name
                    String propertyName = attributes.getValue("name");
                    Assert.isNotNull(propertyName, Messages.ConfigurationReader_errProperty_noNameAttribute);
                    // prepare optional category
                    PropertyCategory category = null;
                    {
                        String categoryString = attributes.getValue("category");
                        if (categoryString != null) {
                            category = PropertyCategory.get(categoryString, null);
                        }
                    }
                    // add property
                    PropertyGroup group = (PropertyGroup) digester.peek();
                    group.addProperty(new PropertyConfiguration(propertyName, category));
                }
            });
        }
        // groups/group/other-properties
        {
            String pattern = "groups/group/other-properties";
            digester.addRule(pattern, new Rule() {
                @Override
                public void begin(String namespace, String name, Attributes attributes) throws Exception {
                    PropertyGroup group = (PropertyGroup) digester.peek();
                    group.addProperty(new PropertyConfiguration(null, null));
                }
            });
        }
    }
    // read XML
    try {
        digester.push(groups);
        digester.parse(input);
    } finally {
        IOUtils.closeQuietly(input);
    }
    // done
    return groups;
}

From source file:org.efaps.jaas.xml.XMLUserLoginModule.java

/**
 * The name of the XML is store in this instance variable. The XML file
 * holds all allowed persons and their related roles and groups.
 *
 * @param _fileName name of the XML file with the user data
 *//*w  ww.j a va2 s. c om*/
@SuppressWarnings("unchecked")
private void readPersons(final String _fileName) {
    try {
        final File file = new File(_fileName);

        final Digester digester = new Digester();
        digester.setValidating(false);
        digester.addObjectCreate("persons", ArrayList.class);
        digester.addObjectCreate("persons/person", XMLPersonPrincipal.class);
        digester.addSetNext("persons/person", "add");

        digester.addCallMethod("persons/person/name", "setName", 1);
        digester.addCallParam("persons/person/name", 0);

        digester.addCallMethod("persons/person/password", "setPassword", 1);
        digester.addCallParam("persons/person/password", 0);

        digester.addCallMethod("persons/person/firstName", "setFirstName", 1);
        digester.addCallParam("persons/person/firstName", 0);

        digester.addCallMethod("persons/person/lastName", "setLastName", 1);
        digester.addCallParam("persons/person/lastName", 0);

        digester.addCallMethod("persons/person/email", "setEmail", 1);
        digester.addCallParam("persons/person/email", 0);

        digester.addCallMethod("persons/person/organisation", "setOrganisation", 1);
        digester.addCallParam("persons/person/organisation", 0);

        digester.addCallMethod("persons/person/url", "setUrl", 1);
        digester.addCallParam("persons/person/url", 0);

        digester.addCallMethod("persons/person/phone", "setPhone", 1);
        digester.addCallParam("persons/person/phone", 0);

        digester.addCallMethod("persons/person/mobile", "setMobile", 1);
        digester.addCallParam("persons/person/mobile", 0);

        digester.addCallMethod("persons/person/fax", "setFax", 1);
        digester.addCallParam("persons/person/fax", 0);

        digester.addCallMethod("persons/person/role", "addRole", 1);
        digester.addCallParam("persons/person/role", 0);

        digester.addCallMethod("persons/person/group", "addGroup", 1);
        digester.addCallParam("persons/person/group", 0);

        final List<XMLPersonPrincipal> personList = (List<XMLPersonPrincipal>) digester.parse(file);
        for (final XMLPersonPrincipal personTmp : personList) {
            this.allPersons.put(personTmp.getName(), personTmp);
        }
    } catch (final IOException e) {
        XMLUserLoginModule.LOG.error("could not open file '" + _fileName + "'", e);
    } catch (final SAXException e) {
        XMLUserLoginModule.LOG.error("could not read file '" + _fileName + "'", e);
    }
}

From source file:org.efaps.maven.run.Plugin.java

/**
 *
 * @param _name name of the plugin/*from  ww  w .j a v  a  2s.  c  om*/
 * @see #name
 */
private Plugin(final URL _url) throws IOException, SAXException, ParserConfigurationException {
    System.out.println("read plugin " + _url);

    Digester digester = new Digester();
    digester.setValidating(false);
    digester.setRules(new ExtendedBaseRules());

    // plugin name
    PluginRule pluginRule = new PluginRule();
    digester.addRule("plugin", pluginRule);
    digester.addCallParam("plugin/goalPrefix", 0);

    // goal
    digester.addRule("plugin/mojos/mojo", new GoalRule());
    digester.addCallParam("plugin/mojos/mojo/goal", 0);
    digester.addCallParam("plugin/mojos/mojo/implementation", 1);
    digester.addCallParam("plugin/mojos/mojo/description", 2);

    // goal parameter
    digester.addCallMethod("plugin/mojos/mojo/parameters/parameter", "addParameter", 6, new Class[] {
            String.class, String.class, String.class, String.class, Boolean.class, Boolean.class });
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/name", 0);
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/alias", 1);
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/type", 2);
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/description", 3);
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/required", 4);
    digester.addCallParam("plugin/mojos/mojo/parameters/parameter/editable", 5);

    // goal configuration (default values)
    DefaultValueRule.init(digester);

    digester.parse(_url.openStream());

    this.name = pluginRule.name;
}

From source file:org.fanhongtao.xml.DigesterUtils.java

public static Object parse(Object obj, InputStream inputStream, URL rulesUrl) throws IOException, SAXException {
    Digester digester = DigesterLoader.createDigester(rulesUrl);
    digester.push(obj);/*  w  w  w.j av a 2  s .  com*/
    digester.setValidating(false);
    return digester.parse(inputStream);
}

From source file:org.freecine.filmscan.Project.java

/**
 Get project object for given directory.
 <p>/* www . ja v a  2s  . c o m*/
 If dir is an existing directory with valid project.xml file in it, laod the 
 project and return it. If there is no project in the directory, return a 
 new empty project.
         
 @param dir Project directory
 @return The project in directory or new project if no project exists there
 */
static public Project getProject(File dir) {
    File projectFile = new File(dir, "project.xml");
    Project ret = null;
    if (projectFile.exists()) {
        Digester d = new Digester();
        d.push("prj_dir_stack", dir);
        d.addRuleSet(new ProjectRuleSet(""));
        try {
            ret = (Project) d.parse(projectFile);
        } catch (IOException e) {
            log.severe("IO error reading " + projectFile.getPath() + ": " + e.getMessage());
        } catch (SAXException e) {
            log.severe("Parse error reading " + projectFile.getPath() + ": " + e.getMessage());
        }
    } else {
        try {
            if (!dir.exists() && !dir.mkdirs()) {
                log.warning("Cannot crete project directory");
                return null;
            }
            ret = new Project(dir);
            ret.save();
        } catch (IOException ex) {
            return null;
        }
    }
    return ret;
}

From source file:org.freecine.filmscan.ScanStrip.java

/**
 Load a strip from file//from  ww  w  . ja v a 2 s  .  c o m
 @param f The xml file that describes the strip
 @return
 */
static public ScanStrip loadStrip(File descFile, File imgFile) {
    ScanStrip strip = null;
    Digester d = new Digester();
    d.addRuleSet(new ScanStripRuleSet(""));
    try {
        strip = (ScanStrip) d.parse(descFile);
        strip.setFile(imgFile);
    } catch (Exception e) {

    }
    return strip;
}

From source file:org.freecine.filmscan.TestProject.java

@Test
public void testProjectSaveLoad() throws IOException, SAXException {
    File prjdir = File.createTempFile("projectest", "");
    prjdir.delete();// w  ww  .  j  ava  2  s.  c o  m
    prjdir.mkdir();
    Project prj = new Project(prjdir);
    ScanStrip sc1 = new ScanStrip();
    ScanStrip sc2 = new ScanStrip();
    for (int n = 0; n < 30; n++) {
        sc1.addPerforation(100, n * 800);
        sc2.addPerforation(100, n * 800);
    }
    prj.addScanStrip(sc1);
    prj.addScanStrip(sc2);

    prj.save();

    // try to load the project

    Digester d = new Digester();
    d.addRuleSet(new ProjectRuleSet(""));
    d.push("prj_dir_stack", prjdir);
    Project prj2 = (Project) d.parse(new File(prjdir, "project.xml"));
    assertEquals(prjdir, prj2.getProjectDir());

}

From source file:org.freecine.filmscan.TestScanStrip.java

@Test
public void testXmlExportImport() throws IOException, TransformerConfigurationException, SAXException {
    ScanStrip sc = new ScanStrip();
    sc.setOrientation(90);//from w w w .  j  a v a2 s  .  c o  m
    for (int n = 100; n < 37000; n += 800) {
        sc.addPerforation(100, n);
    }
    File f = File.createTempFile("scanstrip_test", "xml");

    // Write the strip info to a file
    StreamResult streamResult = new StreamResult(f);
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler hd = tf.newTransformerHandler();
    Transformer serializer = hd.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    hd.setResult(streamResult);
    hd.startDocument();
    sc.writeXml(hd);
    hd.endDocument();

    Digester d = new Digester();
    d.addRuleSet(new ScanStripRuleSet(""));
    ScanStrip parsedSc = (ScanStrip) d.parse(f);
    assert parsedSc.equals(sc);
}

From source file:org.freecine.filmscan.TestScanStrip.java

@Test
public void testLoadStrip() throws IOException, SAXException {
    File stripDesc = new File("/home/harri/s8/tuhkimo/scan/scan_0002.xml");
    Digester d = new Digester();
    d.addRuleSet(new ScanStripRuleSet(""));
    ScanStrip strip = (ScanStrip) d.parse(stripDesc);

    File imgFile = new File(stripDesc.getParentFile(), "scan_0002.tif");
    strip.setFile(imgFile);//from   w  w  w. j a v  a 2  s .c  om

    strip.reserveStripImage();
    RenderedImage img = strip.getFrame(2);
    strip.releaseStripImage();
    img = strip.getFrame(7);
}