Search in sources :

Example 1 with ElementFilter

use of org.jdom2.filter.ElementFilter in project gocd by gocd.

the class GoConfigMigrationIntegrationTest method shouldRemoveAllLuauConfigurationFromConfig.

@Test
public void shouldRemoveAllLuauConfigurationFromConfig() throws Exception {
    String configString = "<cruise schemaVersion='66'>" + "<server siteUrl='https://hostname'>" + "<security>" + "      <luau url='https://luau.url.com' clientKey='0d010cf97ec505ee3788a9b5b8cf71d482c394ae88d32f0333' authState='authorized' />" + "      <ldap uri='ldap' managerDn='managerDn' encryptedManagerPassword='+XhtUNvVAxJdHGF4qQGnWw==' searchFilter='(sAMAccountName={0})'>" + "        <bases>" + "          <base value='ou=Enterprise,ou=Principal,dc=corporate,dc=thoughtworks,dc=com' />" + "        </bases>" + "      </ldap>" + "      <roles>" + "         <role name='luau-role'><groups><luauGroup>luau-group</luauGroup></groups></role>" + "         <role name='ldap-role'><users><user>some-user</user></users></role>" + "</roles>" + "</security>" + "</server>" + "</cruise>";
    String migratedContent = migrateXmlString(configString, 66);
    Document document = new SAXBuilder().build(new StringReader(migratedContent));
    assertThat(document.getDescendants(new ElementFilter("luau")).hasNext(), is(false));
    assertThat(document.getDescendants(new ElementFilter("groups")).hasNext(), is(false));
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) ElementFilter(org.jdom2.filter.ElementFilter) StringReader(java.io.StringReader) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Document(org.jdom2.Document) Test(org.junit.Test)

Example 2 with ElementFilter

use of org.jdom2.filter.ElementFilter in project JMRI by JMRI.

the class ConsistFile method readFile.

/**
     * Read all consists from a file.
     *
     * @param fileName path to file
     * @throws org.jdom2.JDOMException if unable to parse consists
     * @throws java.io.IOException     if unable to read file
     */
@SuppressWarnings("unchecked")
public void readFile(String fileName) throws JDOMException, IOException {
    if (checkFile(fileName)) {
        Element root = rootFromName(fileName);
        Element roster;
        if (root == null) {
            log.warn("consist file could not be read");
            return;
        }
        roster = root.getChild("roster");
        if (roster == null) {
            log.debug("consist file does not contain a roster entry");
            return;
        }
        Iterator<Element> consistIterator = root.getDescendants(new ElementFilter("consist"));
        try {
            Element consist;
            do {
                consist = consistIterator.next();
                consistFromXml(consist);
            } while (consistIterator.hasNext());
        } catch (NoSuchElementException nde) {
            log.debug("end of consist list");
        }
    } else {
        log.info("Consist file does not exist.  One will be created if necessary.");
    }
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with ElementFilter

use of org.jdom2.filter.ElementFilter in project JMRI by JMRI.

the class NameCheckAction method actionPerformed.

@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
    if (fci == null) {
        fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
    }
    // request the filename from an open dialog
    fci.rescanCurrentDirectory();
    int retVal = fci.showOpenDialog(_who);
    // handle selection or cancel
    if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = fci.getSelectedFile();
        if (log.isDebugEnabled()) {
            log.debug("located file " + file + " for XML processing");
        }
        // handle the file (later should be outside this thread?)
        try {
            Element root = readFile(file);
            if (log.isDebugEnabled()) {
                log.debug("parsing complete");
            }
            // check to see if there's a decoder element
            if (root.getChild("decoder") == null) {
                log.warn("Does not appear to be a decoder file");
                return;
            }
            Iterator<Element> iter = root.getChild("decoder").getChild("variables").getDescendants(new ElementFilter("variable"));
            jmri.jmrit.symbolicprog.NameFile nfile = jmri.jmrit.symbolicprog.NameFile.instance();
            String warnings = "";
            while (iter.hasNext()) {
                Element varElement = iter.next();
                // for each variable, see if can find in names file
                Attribute labelAttr = varElement.getAttribute("label");
                String label = null;
                if (labelAttr != null) {
                    label = labelAttr.getValue();
                }
                Attribute itemAttr = varElement.getAttribute("item");
                String item = null;
                if (itemAttr != null) {
                    item = itemAttr.getValue();
                }
                if (log.isDebugEnabled()) {
                    log.debug("Variable called \"" + ((label != null) ? label : "<none>") + "\" \"" + ((item != null) ? item : "<none>"));
                }
                if (!(label == null ? false : nfile.checkName(label)) && !(item == null ? false : nfile.checkName(item))) {
                    log.warn("Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? label : "<none>") + "\"");
                    warnings += "Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? item : "<none>") + "\"\n";
                }
            }
            if (!warnings.equals("")) {
                JOptionPane.showMessageDialog(_who, warnings);
            } else {
                JOptionPane.showMessageDialog(_who, "No mismatched items found");
            }
        } catch (HeadlessException | IOException | JDOMException ex) {
            JOptionPane.showMessageDialog(_who, "Error parsing decoder file: " + ex);
        }
    } else {
        log.debug("XmlFileCheckAction cancelled in open dialog");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) ElementFilter(org.jdom2.filter.ElementFilter) File(java.io.File) XmlFile(jmri.jmrit.XmlFile)

Example 4 with ElementFilter

use of org.jdom2.filter.ElementFilter in project JMRI by JMRI.

the class ConsistFile method consistFromXml.

/**
     * Load a Consist from the consist elements in the file.
     *
     * @param consist a JDOM element containing a consist
     */
@SuppressWarnings("unchecked")
private void consistFromXml(Element consist) {
    Attribute type, cnumber, isCLong, cID;
    Consist newConsist;
    // Read the consist address from the file and create the
    // consisit in memory if it doesn't exist already.
    cnumber = consist.getAttribute("consistNumber");
    isCLong = consist.getAttribute("longAddress");
    DccLocoAddress consistAddress;
    if (isCLong != null) {
        log.debug("adding consist {} with longAddress set to {}.", cnumber, isCLong.getValue());
        try {
            int number = Integer.parseInt(cnumber.getValue());
            consistAddress = new DccLocoAddress(number, isCLong.getValue().equals("yes"));
        } catch (NumberFormatException e) {
            log.debug("Consist number not an integer");
            return;
        }
    } else {
        log.debug("adding consist {} with default long address setting.", cnumber);
        consistAddress = new DccLocoAddress(Integer.parseInt(cnumber.getValue()), false);
    }
    newConsist = consistMan.getConsist(consistAddress);
    if (!(newConsist.getConsistList().isEmpty())) {
        log.debug("Consist {} is not empty.  Using version in memory.", consistAddress.toString());
        return;
    }
    // read and set the consist type
    type = consist.getAttribute("type");
    if (type != null) {
        // use the value read from the file
        newConsist.setConsistType((type.getValue().equals("CSAC")) ? Consist.CS_CONSIST : Consist.ADVANCED_CONSIST);
    } else {
        // use the default (DAC)
        newConsist.setConsistType(Consist.ADVANCED_CONSIST);
    }
    // Read the consist ID from the file;
    cID = consist.getAttribute("id");
    if (cID != null) {
        // use the value read from the file
        newConsist.setConsistID(cID.getValue());
    }
    // read each child of locomotive in the consist from the file
    // and restore it's information to memory.
    Iterator<Element> childIterator = consist.getDescendants(new ElementFilter("loco"));
    try {
        Element e;
        do {
            e = childIterator.next();
            Attribute number, isLong, direction, position, rosterId;
            number = e.getAttribute("dccLocoAddress");
            isLong = e.getAttribute("longAddress");
            direction = e.getAttribute("locoDir");
            position = e.getAttribute("locoName");
            rosterId = e.getAttribute("locoRosterId");
            log.debug("adding Loco {}", number);
            // Use restore so we DO NOT cause send any commands
            // to the command station as we recreate the consist.
            DccLocoAddress address;
            if (isLong != null && direction != null) {
                // use the values from the file
                log.debug("using direction from file {}", direction.getValue());
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
                newConsist.restore(address, direction.getValue().equals("normal"));
            } else if (isLong == null && direction != null) {
                // use the direction from the file
                // but set as long address
                log.debug("using direction from file {}", direction.getValue());
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
                newConsist.restore(address, direction.getValue().equals("normal"));
            } else if (isLong != null && direction == null) {
                // use the default direction
                // but the long/short value from the file
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), isLong.getValue().equals("yes"));
                newConsist.restore(address, true);
            } else {
                // use the default values long address
                // and normal direction
                address = new DccLocoAddress(Integer.parseInt(number.getValue()), true);
                newConsist.restore(address, true);
            }
            if (position != null && !position.getValue().equals("mid")) {
                if (position.getValue().equals("lead")) {
                    newConsist.setPosition(address, Consist.POSITION_LEAD);
                } else if (position.getValue().equals("rear")) {
                    newConsist.setPosition(address, Consist.POSITION_TRAIL);
                }
            } else {
                Attribute midNumber = e.getAttribute("locoMidNumber");
                if (midNumber != null) {
                    int pos = Integer.parseInt(midNumber.getValue());
                    newConsist.setPosition(address, pos);
                }
            }
            if (rosterId != null) {
                newConsist.setRosterId(address, rosterId.getValue());
            }
        } while (true);
    } catch (NoSuchElementException nse) {
        log.debug("end of loco list");
    }
}
Also used : Attribute(org.jdom2.Attribute) Consist(jmri.Consist) ElementFilter(org.jdom2.filter.ElementFilter) Element(org.jdom2.Element) NoSuchElementException(java.util.NoSuchElementException) DccLocoAddress(jmri.DccLocoAddress)

Example 5 with ElementFilter

use of org.jdom2.filter.ElementFilter in project JMRI by JMRI.

the class DuplicateTest method check.

@SuppressWarnings("unchecked")
boolean check(File file) throws JDOMException, IOException {
    Element root = readFile(file);
    // check to see if there's a decoder element
    if (root.getChild("decoder") == null) {
        log.warn("Does not appear to be a decoder file");
        return false;
    }
    String family = root.getChild("decoder").getChild("family").getAttributeValue("name") + "][";
    Iterator<Element> iter = root.getChild("decoder").getChild("family").getDescendants(new ElementFilter("model"));
    boolean failed = false;
    while (iter.hasNext()) {
        String model = iter.next().getAttributeValue("model");
        if (models.contains(family + model)) {
            System.err.println("found duplicate for " + family + model);
            failed = true;
        }
        models.add(family + model);
    }
    return failed;
}
Also used : Element(org.jdom2.Element) ElementFilter(org.jdom2.filter.ElementFilter)

Aggregations

ElementFilter (org.jdom2.filter.ElementFilter)5 Element (org.jdom2.Element)4 NoSuchElementException (java.util.NoSuchElementException)2 Attribute (org.jdom2.Attribute)2 HeadlessException (java.awt.HeadlessException)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Consist (jmri.Consist)1 DccLocoAddress (jmri.DccLocoAddress)1 XmlFile (jmri.jmrit.XmlFile)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Document (org.jdom2.Document)1 JDOMException (org.jdom2.JDOMException)1 SAXBuilder (org.jdom2.input.SAXBuilder)1 Test (org.junit.Test)1