Search in sources :

Example 11 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testComplexAttributesStuff.

@Test
public void testComplexAttributesStuff() throws Exception {
    CoreParser parser = new CoreParser();
    String xml = readResource("core-subsystem.xml");
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName("urn:jboss:domain:core:1.0", "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    Assert.assertEquals(2, operations.size());
    Assert.assertEquals(2, operations.get(1).get("listeners").asList().size());
    ModelNode subsystem = opsToModel(operations);
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 12 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testGroups.

@Test
public void testGroups() throws Exception {
    MyParser parser = new AttributeGroupParser();
    String xml = readResource("groups-subsystem.xml");
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    ModelNode subsystem = opsToModel(operations);
    assertEquals("bar", subsystem.get("resource", "foo", "cluster-attr1").asString());
    assertEquals("baz", subsystem.get("resource", "foo", "cluster-attr2").asString());
    assertEquals("alice", subsystem.get("resource", "foo", "security-my-attr1").asString());
    assertEquals("bob", subsystem.get("resource", "foo", "security-my-attr2").asString());
    assertEquals("val", subsystem.get("resource", "foo", "props", "prop").asString());
    assertEquals("val", subsystem.get("resource", "foo", "wrapped-properties", "prop").asString());
    assertEquals("bar2", subsystem.get("resource", "foo2", "cluster-attr1").asString());
    assertEquals("baz2", subsystem.get("resource", "foo2", "cluster-attr2").asString());
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 13 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class PersistentResourceXMLParserTestCase method testChildlessResource.

@Test
public void testChildlessResource() throws Exception {
    MyParser parser = new ChildlessParser();
    String xml = "<subsystem xmlns=\"" + MyParser.NAMESPACE + "\">" + "   <cluster attr1=\"alice\"/>" + "</subsystem>";
    StringReader strReader = new StringReader(xml);
    XMLMapper mapper = XMLMapper.Factory.create();
    mapper.registerRootElement(new QName(MyParser.NAMESPACE, "subsystem"), parser);
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StreamSource(strReader));
    List<ModelNode> operations = new ArrayList<>();
    mapper.parseDocument(operations, reader);
    ModelNode subsystem = opsToModel(operations);
    StringWriter stringWriter = new StringWriter();
    XMLExtendedStreamWriter xmlStreamWriter = createXMLStreamWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    SubsystemMarshallingContext context = new SubsystemMarshallingContext(subsystem, xmlStreamWriter);
    mapper.deparseDocument(parser, context, xmlStreamWriter);
    String out = stringWriter.toString();
    Assert.assertEquals(normalizeXML(xml), normalizeXML(out));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) ArrayList(java.util.ArrayList) XMLMapper(org.jboss.staxmapper.XMLMapper) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ModelNode(org.jboss.dmr.ModelNode) Test(org.junit.Test)

Example 14 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class ASModuleHandler method addModule.

protected void addModule(CommandContext ctx, final ParsedCommandLine parsedCmd) throws CommandLineException {
    final String moduleName = name.getValue(parsedCmd, true);
    // resources required only if we are generating module.xml
    if (!moduleArg.isPresent(parsedCmd) && !(resources.isPresent(parsedCmd) || absoluteResources.isPresent(parsedCmd))) {
        throw new CommandFormatException("You must specify at least one resource: use --resources or --absolute-resources parameter");
    }
    final String resourcePaths = resources.getValue(parsedCmd);
    final String absoluteResourcePaths = absoluteResources.getValue(parsedCmd);
    String pathDelimiter = PATH_SEPARATOR;
    if (resourceDelimiter.isPresent(parsedCmd)) {
        pathDelimiter = resourceDelimiter.getValue(parsedCmd);
    }
    final FilenameTabCompleter pathCompleter = Util.isWindows() ? new WindowsFilenameTabCompleter(ctx) : new DefaultFilenameTabCompleter(ctx);
    final String[] resourceArr = (resourcePaths == null) ? new String[0] : resourcePaths.split(pathDelimiter);
    File[] resourceFiles = new File[resourceArr.length];
    boolean allowNonExistent = allowNonExistentResources.isPresent(parsedCmd);
    for (int i = 0; i < resourceArr.length; ++i) {
        final File f = new File(pathCompleter.translatePath(resourceArr[i]));
        if (!f.exists() && !allowNonExistent) {
            throw new CommandLineException("Failed to locate " + f.getAbsolutePath() + ", if you defined a nonexistent resource on purpose you should " + "use the " + allowNonExistentResources.getFullName() + " option");
        }
        resourceFiles[i] = f;
    }
    final String[] absoluteResourceArr = (absoluteResourcePaths == null) ? new String[0] : absoluteResourcePaths.split(pathDelimiter);
    File[] absoluteResourceFiles = new File[absoluteResourceArr.length];
    for (int i = 0; i < absoluteResourceArr.length; ++i) {
        final File f = new File(pathCompleter.translatePath(absoluteResourceArr[i]));
        if (!f.exists()) {
            throw new CommandLineException("Failed to locate " + f.getAbsolutePath());
        }
        absoluteResourceFiles[i] = f;
    }
    final File moduleDir = getModulePath(getModulesDir(ctx), moduleName, slot.getValue(parsedCmd));
    if (moduleDir.exists()) {
        throw new CommandLineException("Module " + moduleName + " already exists at " + moduleDir.getAbsolutePath());
    }
    if (!moduleDir.mkdirs()) {
        throw new CommandLineException("Failed to create directory " + moduleDir.getAbsolutePath());
    }
    final ModuleConfigImpl config;
    final String moduleXml = moduleArg.getValue(parsedCmd);
    if (moduleXml != null) {
        config = null;
        final File source = new File(moduleXml);
        if (!source.exists()) {
            throw new CommandLineException("Failed to locate the file on the filesystem: " + source.getAbsolutePath());
        }
        copy(source, new File(moduleDir, "module.xml"));
    } else {
        config = new ModuleConfigImpl(moduleName);
    }
    for (File f : resourceFiles) {
        copyResource(f, new File(moduleDir, f.getName()), ctx, this);
        if (config != null) {
            config.addResource(new ResourceRoot(f.getName()));
        }
    }
    for (File f : absoluteResourceFiles) {
        if (config != null) {
            try {
                config.addResource(new ResourceRoot(f.getCanonicalPath()));
            } catch (IOException ioe) {
                throw new CommandLineException("Failed to read path: " + f.getAbsolutePath(), ioe);
            }
        }
    }
    if (config != null) {
        Set<String> modules = new HashSet<>();
        final String dependenciesStr = dependencies.getValue(parsedCmd);
        if (dependenciesStr != null) {
            final String[] depsArr = dependenciesStr.split(",+");
            for (String dep : depsArr) {
                // TODO validate dependencies
                String depName = dep.trim();
                config.addDependency(new ModuleDependency(depName));
                modules.add(depName);
            }
        }
        final String exportDependenciesStr = exportDependencies.getValue(parsedCmd);
        if (exportDependenciesStr != null) {
            final String[] depsArr = exportDependenciesStr.split(",+");
            for (String dep : depsArr) {
                // TODO validate dependencies
                String depName = dep.trim();
                if (modules.contains(depName)) {
                    deleteRecursively(moduleDir);
                    throw new CommandLineException("Error, duplicated dependency " + depName);
                }
                modules.add(depName);
                config.addDependency(new ModuleDependency(depName, true));
            }
        }
        final String propsStr = props.getValue(parsedCmd);
        if (propsStr != null) {
            final String[] pairs = propsStr.split(",");
            for (String pair : pairs) {
                int equals = pair.indexOf('=');
                if (equals == -1) {
                    throw new CommandFormatException("Property '" + pair + "' in '" + propsStr + "' is missing the equals sign.");
                }
                final String propName = pair.substring(0, equals);
                if (propName.isEmpty()) {
                    throw new CommandFormatException("Property name is missing for '" + pair + "' in '" + propsStr + "'");
                }
                config.setProperty(propName, pair.substring(equals + 1));
            }
        }
        final String slotVal = slot.getValue(parsedCmd);
        if (slotVal != null) {
            config.setSlot(slotVal);
        }
        final String mainCls = mainClass.getValue(parsedCmd);
        if (mainCls != null) {
            config.setMainClass(mainCls);
        }
        FileOutputStream fos = null;
        final File moduleFile = new File(moduleDir, "module.xml");
        try {
            fos = new FileOutputStream(moduleFile);
            XMLExtendedStreamWriter xmlWriter = create(XMLOutputFactory.newInstance().createXMLStreamWriter(fos, StandardCharsets.UTF_8.name()));
            config.writeContent(xmlWriter, null);
            xmlWriter.flush();
        } catch (IOException e) {
            throw new CommandLineException("Failed to create file " + moduleFile.getAbsolutePath(), e);
        } catch (XMLStreamException e) {
            throw new CommandLineException("Failed to write to " + moduleFile.getAbsolutePath(), e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
Also used : DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) IOException(java.io.IOException) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) DefaultFilenameTabCompleter(org.jboss.as.cli.handlers.DefaultFilenameTabCompleter) FilenameTabCompleter(org.jboss.as.cli.handlers.FilenameTabCompleter) CommandLineException(org.jboss.as.cli.CommandLineException) XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) CommandFormatException(org.jboss.as.cli.CommandFormatException) FileOutputStream(java.io.FileOutputStream) WindowsFilenameTabCompleter(org.jboss.as.cli.handlers.WindowsFilenameTabCompleter) File(java.io.File) HashSet(java.util.HashSet)

Example 15 with XMLExtendedStreamWriter

use of org.jboss.staxmapper.XMLExtendedStreamWriter in project wildfly-core by wildfly.

the class AbstractMgmtTestBase method modelToXml.

public String modelToXml(String subsystemName, String childType, XMLElementWriter<SubsystemMarshallingContext> parser) throws Exception {
    final ModelNode address = new ModelNode();
    address.add("subsystem", subsystemName);
    address.protect();
    final ModelNode operation = new ModelNode();
    operation.get(OP).set("read-children-resources");
    operation.get("child-type").set(childType);
    operation.get(RECURSIVE).set(true);
    operation.get(OP_ADDR).set(address);
    final ModelNode result = executeOperation(operation);
    Assert.assertNotNull(result);
    ModelNode dsNode = new ModelNode();
    dsNode.get(childType).set(result);
    StringWriter strWriter = new StringWriter();
    XMLExtendedStreamWriter writer = XMLExtendedStreamWriterFactory.create(XMLOutputFactory.newInstance().createXMLStreamWriter(strWriter));
    parser.writeContent(writer, new SubsystemMarshallingContext(dsNode, writer));
    writer.flush();
    return strWriter.toString();
}
Also used : XMLExtendedStreamWriter(org.jboss.staxmapper.XMLExtendedStreamWriter) StringWriter(java.io.StringWriter) ModelNode(org.jboss.dmr.ModelNode) SubsystemMarshallingContext(org.jboss.as.controller.persistence.SubsystemMarshallingContext)

Aggregations

XMLExtendedStreamWriter (org.jboss.staxmapper.XMLExtendedStreamWriter)15 ModelNode (org.jboss.dmr.ModelNode)13 StringWriter (java.io.StringWriter)12 StringReader (java.io.StringReader)10 ArrayList (java.util.ArrayList)10 QName (javax.xml.namespace.QName)10 XMLStreamReader (javax.xml.stream.XMLStreamReader)10 StreamSource (javax.xml.transform.stream.StreamSource)10 XMLMapper (org.jboss.staxmapper.XMLMapper)10 Test (org.junit.Test)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 CommandFormatException (org.jboss.as.cli.CommandFormatException)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 DefaultFilenameTabCompleter (org.jboss.as.cli.handlers.DefaultFilenameTabCompleter)1 FilenameTabCompleter (org.jboss.as.cli.handlers.FilenameTabCompleter)1