Example usage for org.jdom2.input SAXBuilder SAXBuilder

List of usage examples for org.jdom2.input SAXBuilder SAXBuilder

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder SAXBuilder.

Prototype

public SAXBuilder() 

Source Link

Document

Creates a new JAXP-based SAXBuilder.

Usage

From source file:count_dep.Count_dep.java

public static LinkedList<Event> ReadEvents(File f) throws JDOMException, IOException {
    LinkedList<Event> res = new LinkedList<>();
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<Element> one_document = foo.getChildren();
    for (Element one_document1 : one_document) {
        List<Element> ERE = one_document1.getChildren();
        for (Element e : ERE) {
            if ("event".equals(e.getName())) {
                List<Element> mentions = e.getChildren("event_mention");
                for (Element m : mentions) {
                    Event eve = new Event();
                    Element charseq;
                    Element ldcscpope = m.getChild("ldc_scope");
                    charseq = ldcscpope.getChild("charseq");
                    eve.span = charseq.getText().replace("\n", " ");
                    Element anchor = m.getChild("anchor");
                    charseq = anchor.getChild("charseq");
                    eve.trigger = charseq.getText();
                    if (eve.trigger.equalsIgnoreCase("saturday")) {
                        int a = 0;
                        a = a + 1;//from   w w w .ja va2 s. c  o m
                    }
                    eve.eventtype = e.getAttribute("SUBTYPE").getValue();
                    eve.eventlargetype = e.getAttribute("TYPE").getValue();
                    List<Element> arguments = m.getChildren("event_mention_argument");
                    for (Element argu : arguments) {
                        String argumentstr = argu.getChild("extent").getChild("charseq").getText();
                        if ("U.S".equals(argumentstr) || "U.N".equals(argumentstr)
                                || "Feb".equals(argumentstr)) {
                            argumentstr += ".";
                        }
                        if (argumentstr.equalsIgnoreCase("Basra")) {
                            int a = 0;
                            a = a + 1;
                        }
                        eve.arguments.add(new EventArgument(argumentstr, argu.getAttributeValue("ROLE")));
                    }
                    eve.filename = f.getName();
                    res.add(eve);
                }

            }
        }
    }
    return res;
}

From source file:count_dep.Count_dep.java

public static LinkedList<Event> ReadGrishmanEvents(File f) throws JDOMException, IOException {
    LinkedList<Event> res = new LinkedList<>();
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(f);
    Element foo = doc.getRootElement();
    List<Element> one_document = foo.getChildren();
    for (Element one_document1 : one_document) {
        List<Element> ERE = one_document1.getChildren();
        for (Element e : ERE) {
            if ("event".equals(e.getName())) {
                List<Element> mentions = e.getChildren("event_mention");
                for (Element m : mentions) {
                    Event eve = new Event();
                    eve.filename = f.getName();
                    Element charseq;
                    Element anchor = m.getChild("anchor");
                    charseq = anchor.getChild("charseq");
                    eve.span = m.getChild("extent").getChild("charseq").getText();
                    eve.trigger = charseq.getText();
                    eve.eventtype = e.getAttribute("SUBTYPE").getValue();
                    List<Element> arguments = m.getChildren("event_mention_argument");
                    for (Element argu : arguments) {
                        eve.arguments//from  w  w  w.j  av a 2  s.c  om
                                .add(new EventArgument(argu.getChild("extent").getChild("charseq").getText(),
                                        argu.getAttributeValue("ROLE")));
                    }
                    //   eve.filename = f.getName();
                    res.add(eve);
                }

            }

        }
    }
    return res;
}

From source file:count_dep.CRF.java

private void transfer_into_inputfiles() throws JDOMException, IOException {
    Properties props = new Properties();
    props.put("annotators", "tokenize, ssplit, pos, lemma");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    File corpus = new File("D:\\LDC2006D06\\LDC2006D06\\Data\\LDC2006T06_Original\\data\\English\\nw\\fp1");
    File[] listFiles = corpus.listFiles();
    for (File f : listFiles) {
        if (f.getName().endsWith(".sgm")) {
            PrintStream ps = new PrintStream(new FileOutputStream("D:\\ACEAlan\\UIUCNERInput\\" + f.getName()));
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(f);
            Element foo = doc.getRootElement();
            String text = foo.getChild("BODY").getChild("TEXT").getText();
            Annotation document = new Annotation(text);
            pipeline.annotate(document);
            List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
            for (CoreMap cm : sentences) {
                String str = cm.toString();
                String str2 = str.replace('\n', ' ');
                ps.println(str2);//from   w w  w .  j a v  a  2s . co  m
            }
            ps.close();
        }
    }
}

From source file:count_dep.Miscellaneous.java

private void MergeACE() throws FileNotFoundException, JDOMException, IOException {
    String[] corpusfolders = {//from w  ww  .  j  av  a 2 s. c o  m
            "D:\\LDC2006D06\\LDC2006D06\\Data\\LDC2006T06_Original\\data\\English\\bn\\fp1\\",
            "D:\\LDC2006D06\\LDC2006D06\\Data\\LDC2006T06_Original\\data\\English\\nw\\fp1\\",
            "D:\\LDC2006D06\\LDC2006D06\\Data\\LDC2006T06_Original\\data\\English\\wl\\fp1\\" };
    PrintStream ps = new PrintStream(new FileOutputStream("D:\\wordvec\\ACE.txt"));
    for (int i = 0; i < corpusfolders.length; i++) {
        File folder = new File(corpusfolders[i]);
        File[] listFiles = folder.listFiles();
        for (File f : listFiles) {
            if (f.getName().contains(".sgm")) {
                SAXBuilder builder = new SAXBuilder();
                Document doc = builder.build(f);
                Element foo = doc.getRootElement();
                String text = foo.getChild("BODY").getChild("TEXT").getText();
                // int titleend = text.indexOf("\n\n");
                // text = text.substring(titleend + 1);
                ps.println(text);
            }
        }
    }
    ps.close();
}

From source file:cz.cesnet.shongo.connector.device.AdobeConnectConnector.java

@Override
public void importRoomSettings(String roomId, String settings) throws CommandException {
    SAXBuilder saxBuilder = new SAXBuilder();
    Document document;//ww  w.  j  ava  2s  .  c  o m
    try {
        document = saxBuilder.build(new StringReader(settings));
    } catch (Exception exception) {
        throw new CommandException(exception.getMessage(), exception);
    }

    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    String xmlString = outputter.outputString(document);

    RequestAttributeList attributes = new RequestAttributeList();

    attributes.add("sco-id", roomId);
    //        attributes.add("date-begin", document.getRootElement().getChild("sco").getChild("date-begin").getText());
    //        attributes.add("date-end", document.getRootElement().getChild("sco").getChild("date-end").getText());
    if (document.getRootElement().getChild("sco").getChild("description") != null) {
        attributes.add("description",
                document.getRootElement().getChild("sco").getChild("description").getText());
    }
    attributes.add("url-path", document.getRootElement().getChild("sco").getChild("url-path").getText());
    attributes.add("name", document.getRootElement().getChild("sco").getChild("name").getText());

    execApi("sco-update", attributes);
}

From source file:cz.cesnet.shongo.connector.device.AdobeConnectConnector.java

/**
 * Performs the action to log into Adobe Connect server. Stores the breezeseession ID.
 *//*from  www  .  ja  v  a2s.  c  o  m*/
protected void login() throws CommandException {
    if (this.connectionSession != null) {
        logout();
    }

    RequestAttributeList loginAttributes = new RequestAttributeList();
    loginAttributes.add("login", this.login);
    loginAttributes.add("password", this.password);

    URLConnection connection;
    try {
        String loginUrl = getActionUrl("login", loginAttributes);
        connection = new URL(loginUrl).openConnection();
        connection.connect();

        InputStream resultStream = connection.getInputStream();
        Document resultDocument = new SAXBuilder().build(resultStream);
        if (this.isError(resultDocument)) {
            throw new CommandException("Login to server " + deviceAddress + " failed");
        } else {
            logger.debug(String.format("Login to server %s succeeded", deviceAddress));
        }
    } catch (Exception exception) {
        throw new CommandException(exception.getMessage(), exception);
    }

    String connectionSessionString = connection.getHeaderField("Set-Cookie");
    StringTokenizer st = new StringTokenizer(connectionSessionString, "=");
    String sessionName = null;
    if (st.countTokens() > 1) {
        sessionName = st.nextToken();
    }

    if (sessionName != null && (sessionName.equals("JSESSIONID") || sessionName.equals("BREEZESESSION"))) {
        String connectionSessionNext = st.nextToken();
        int separatorIndex = connectionSessionNext.indexOf(';');
        this.connectionSession = connectionSessionNext.substring(0, separatorIndex);
        this.meetingsFolderId = this.getMeetingsFolderId();
    }

    if (connectionSession == null) {
        throw new CommandException("Could not log in to Adobe Connect server: " + deviceAddress);
    }
    this.connectionState = ConnectionState.LOOSELY_CONNECTED;

    this.capacityCheckThread = new Thread() {
        private Logger logger = LoggerFactory.getLogger(AdobeConnectConnector.class);

        @Override
        public void run() {
            setCapacityChecking(true);
            logger.info("Checking of rooms capacity - starting...");
            while (capacityCheckThread != null && isConnected()) {
                try {
                    Thread.sleep(capacityCheckTimeout);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    continue;
                }

                try {
                    checkAllRoomsCapacity();
                } catch (Exception exception) {
                    logger.warn("Capacity check failed", exception);
                }
            }
            logger.info("Checking of rooms capacity - exiting...");
            setCapacityChecking(false);
        }
    };
    this.capacityCheckThread.setName(Thread.currentThread().getName() + "-capacities");
    synchronized (this) {
        if (!this.capacityChecking) {
            this.capacityCheckThread.start();
        }
    }
}

From source file:cz.cesnet.shongo.connector.device.AdobeConnectConnector.java

/**
 * Execute command on Adobe Connect server and returns XML response. Throws CommandException when some error on Adobe Connect server occured or some parser error occured.
 *
 * @param action     name of Adobe Connect action
 * @param attributes attributes of action
 * @return XML action response/*  w ww .  ja v  a 2 s.com*/
 * @throws CommandException
 */
protected Element execApi(String action, RequestAttributeList attributes) throws CommandException {
    try {
        if (this.connectionSession == null) {
            if (action.equals("logout")) {
                return null;
            } else {
                login();
            }
        }

        String actionUrl = getActionUrl(action, attributes);
        logger.debug(String.format("Calling action %s on %s", actionUrl, deviceAddress));

        int retryCount = 5;
        while (retryCount > 0) {
            // Read result from url
            Document result;
            try {
                // Read result
                InputStream resultStream = execApi(actionUrl, requestTimeout);
                result = new SAXBuilder().build(resultStream);
            } catch (IOException exception) {
                if (isRequestApiRetryPossible(exception)) {
                    retryCount--;
                    logger.warn("{}: Trying again...", exception.getMessage());
                    continue;
                } else {
                    throw exception;
                }
            }

            // Check for error and reconnect if login is needed
            if (isError(result)) {
                if (isLoginNeeded(result)) {
                    retryCount--;
                    logger.debug(String.format("Reconnecting to server %s", deviceAddress));
                    this.connectionState = ConnectionState.RECONNECTING;
                    connectionSession = null;
                    login();
                    continue;
                }
                throw new RequestFailedCommandException(actionUrl, result);
            } else {
                logger.debug(String.format("Command %s succeeded on %s", action, deviceAddress));
                return result.getRootElement();
            }
        }
        throw new CommandException(String.format("Command %s failed.", action));
    } catch (IOException e) {
        throw new RuntimeException("Command issuing error", e);
    } catch (JDOMParseException e) {
        throw new RuntimeException("Command result parsing error", e);
    } catch (JDOMException e) {
        throw new RuntimeException("Error initializing parser", e);
    } catch (RequestFailedCommandException exception) {
        logger.warn(String.format("Command %s has failed on %s: %s", action, deviceAddress, exception));
        throw exception;
    }
}

From source file:cz.lbenda.dbapp.rc.SessionConfiguration.java

License:Apache License

public static void loadFromString(final String document) {
    SAXBuilder builder = new SAXBuilder();
    try {/*w  w w  .  j a va 2 s .  c  o  m*/
        loadFromDocument(builder.build(new StringReader(document)));
    } catch (JDOMException e) {
        LOG.error("The string isn't parsable: " + document, e);
        throw new RuntimeException("The string isn't parsable: " + document, e);
    } catch (IOException e) {
        LOG.error("The string can't be opend as StringReader: " + document, e);
        throw new RuntimeException("The string cant be opend as StringReader: " + document, e);
    }
}

From source file:cz.lbenda.dbapp.rc.SessionConfiguration.java

License:Apache License

private void loadExtendedConfigurationFromFile() {
    SAXBuilder builder = new SAXBuilder();
    try {/*from   www.  ja  v  a 2  s.c o  m*/
        File file = new File(extendedConfigurationPath);
        Document document = builder.build(new FileReader(file));
        Element root = document.getRootElement();
        loadSchemas(root.getChild("schemas"));
        tableOfKeysSQLFromElement(root.getChild("tableOfKeySQLs"));
        tableDescriptionExtensionsFromElement(root.getChild("tableDescriptionExtensions"));
    } catch (JDOMException e) {
        LOG.error("The file isn't parsable: " + extendedConfigurationPath, e);
        throw new RuntimeException("The file isn't parsable: " + extendedConfigurationPath, e);
    } catch (IOException e) {
        LOG.error("The file can't be opend as StringReader: " + extendedConfigurationPath, e);
        throw new RuntimeException("The file cant be opend as StringReader: " + extendedConfigurationPath, e);
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.Settings.java

License:Apache License

/**
 * Sets properties desired for MathML normalization purpose
 *
 * @return initialized SAXBuilder instance
 *//*from w w  w  . j  a va 2s  . c  om*/
public static SAXBuilder setupSAXBuilder() {
    final SAXBuilder builder = new SAXBuilder();
    builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    //builder.setFeature("http://xml.org/sax/features/external-general-entities", true);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            if (systemId.endsWith("dtd")) {
                String dtdLocation = Settings.getProperty(Settings.MATHMLDTD);
                return new InputSource(Settings.class.getResourceAsStream(dtdLocation));
            }
            return null;
        }
    });

    return builder;
}