Example usage for org.xml.sax Attributes getQName

List of usage examples for org.xml.sax Attributes getQName

Introduction

In this page you can find the example usage for org.xml.sax Attributes getQName.

Prototype

public abstract String getQName(int index);

Source Link

Document

Look up an attribute's XML qualified (prefixed) name by index.

Usage

From source file:ca.sqlpower.wabit.dao.WorkspaceSAXHandler.java

/**
 * Throws a {@link CancellationException} if either the loading of the file
 * was cancelled by a method call or cancelled internally due to a problem
 * in the file. If there was a problem with the file this method will notify
 * the user and another notification does not need to be sent.
 * <p>/* w  ww  .jav a2 s .  com*/
 * 
 * @see WorkspaceXMLDAO#FILE_VERSION
 */
private void startElementImpl(final String uri, final String localName, final String name,
        Attributes attributes) throws SAXException {
    if (isCancelled()) {
        return;
    }

    xmlContext.push(name);

    final WabitObject createdObject;

    if (name.equals("wabit")) {
        createdObject = null;

        String versionString = attributes.getValue("export-format");

        //NOTE: For correct versioning behaviour see WorkspaceXMLDAO.FILE_VERSION.
        if (versionString == null) {
            UserPrompter up = promptFactory.createUserPrompter(
                    "This Wabit workspace file is very old. It may not read correctly, but I will try my best.",
                    UserPromptType.MESSAGE, UserPromptOptions.OK, UserPromptResponse.OK, null, "OK");
            up.promptUser();
        } else {
            Version fileVersion = new Version(versionString);
            Version fileMajorMinorVersion = new Version(fileVersion, 2);
            Version currentMajorMinorVersion = new Version(WorkspaceXMLDAO.FILE_VERSION, 2);
            Version fileMajorVersion = new Version(fileVersion, 1);
            Version currentMajorVersion = new Version(WorkspaceXMLDAO.FILE_VERSION, 1);

            String message = null;
            boolean displayMessage = true;
            if (fileMajorVersion.compareTo(currentMajorVersion) < 0) {
                message = "The Wabit workspace you are opening is too old to be successfully loaded.\n"
                        + "An older version of Wabit is required to view the saved workspace.";
                setCancelled(true);
            } else if (fileVersion.equals(new Version("1.0.0"))) {
                message = "The Wabit workspace you are opening is an old version that does not record\n"
                        + "information about page orientation. All pages will default to portrait orientation.";
            } else if (fileVersion.compareTo(new Version("1.1.0")) < 0) {
                message = "The Wabit workspace you are opening contains OLAP and/or reports from an.\n"
                        + "old version of the wabit. These items cannot be loaded and need to be updated\n"
                        + "to the latest version.";
            } else if (fileVersion.compareTo(new Version("1.2.0")) < 0) {
                message = "The Wabit workspace you are opening was created in an older version of Wabit\n"
                        + "which stored charts within reports rather than sharing them within the Workspace.\n"
                        + "Your charts will appear as empty boxes; you will have to re-create them.";
            } else if (fileMajorMinorVersion.compareTo(currentMajorMinorVersion) > 0) {
                message = "The Wabit workspace you are opening was created in a newer version of Wabit.\n"
                        + "Due to large changes in the file format this file cannot be loaded without updating "
                        + "Wabit.";
                setCancelled(true);
            } else if (fileVersion.compareTo(WorkspaceXMLDAO.FILE_VERSION) > 0) {
                message = "The Wabit workspace you are opening was created in a newer version of Wabit.\n"
                        + "I will attempt to load this workspace but it is recommended to update Wabit\n"
                        + "to the latest version.";
            } else {
                displayMessage = false;
            }

            if (fileVersion.compareTo(new Version("1.2.5")) >= 0) {
                nameMandatory = true;
                uuidMandatory = true;
            } else {
                nameMandatory = false;
                uuidMandatory = false;
            }

            if (displayMessage) {
                UserPrompter up = promptFactory.createUserPrompter(message, UserPromptType.MESSAGE,
                        UserPromptOptions.OK, UserPromptResponse.OK, null, "OK");
                up.promptUser();
            }
            if (isCancelled())
                throw new CancellationException();
        }

    } else if (name.equals("project")) {
        createdObject = session.getWorkspace();
    } else if (name.equals("data-source")) {
        String dsName = attributes.getValue("name");
        checkMandatory("name", dsName);

        progressMessage = session.getWorkspace().getName() + ": loading data source " + dsName;

        SPDataSource ds = dsCollection.getDataSource(dsName);
        if (ds == null) {
            List<Class<? extends SPDataSource>> dsTypes = new ArrayList<Class<? extends SPDataSource>>();
            dsTypes.add(JDBCDataSource.class);
            dsTypes.add(Olap4jDataSource.class);
            //Note: the new prompt here is so that on the server side the user still has the
            //option of creating a new datasource
            UserPrompter prompter = promptFactory.createDatabaseUserPrompter(
                    "The data source \"" + dsName + "\" does not exist. Please select a replacement.", dsTypes,
                    UserPromptOptions.OK_NEW_NOTOK_CANCEL, UserPromptResponse.NOT_OK, null, dsCollection,
                    "Select Data Source", "New...", "Skip Data Source", "Cancel Load");

            UserPromptResponse response = prompter.promptUser();
            if (response == UserPromptResponse.OK || response == UserPromptResponse.NEW) {
                ds = (SPDataSource) prompter.getUserSelectedResponse();
                createdObject = new WabitDataSource(ds);
                if (!session.getWorkspace().dsAlreadyAdded(ds)) {
                    session.getWorkspace().addDataSource(ds);
                }
                oldToNewDSNames.put(dsName, ds.getName());
            } else if (response == UserPromptResponse.NOT_OK) {
                ds = null;
                createdObject = null;
            } else {
                setCancelled(true);
                createdObject = null;
            }
        } else if (!session.getWorkspace().dsAlreadyAdded(ds)) {
            session.getWorkspace().addDataSource(ds);
            createdObject = new WabitDataSource(ds);
        } else {
            createdObject = null;
        }
    } else if (name.equals("query")) {
        cache = new QueryCache(session.getContext(), false);
        createdObject = cache;

        String queryName = attributes.getValue("name");
        cache.setName(queryName);
        progressMessage = session.getWorkspace().getName() + " : loading query " + queryName;

        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("uuid")) {
                // already loaded
            } else if (aname.equals("name")) {
                // already loaded
            } else if (aname.equals("data-source")) {
                JDBCDataSource ds = session.getWorkspace().getDataSource(aval, JDBCDataSource.class);
                if (ds == null) {
                    String newDSName = oldToNewDSNames.get(aval);
                    if (newDSName != null) {
                        ds = session.getWorkspace().getDataSource(newDSName, JDBCDataSource.class);
                        if (ds == null) {
                            logger.debug("Data source " + aval
                                    + " is not in the workspace. Attempted to replace with new data source "
                                    + newDSName + ". Query " + aname + " was connected to it previously.");
                            throw new NullPointerException(
                                    "Data source " + newDSName + " was not found in the workspace.");
                        }
                    }
                    logger.debug("Workspace has data sources " + session.getWorkspace().getDataSources());
                }
                cache.setDataSourceWithoutSideEffects(ds);
            } else if (aname.equals("zoom")) {
                cache.setZoomLevel(Integer.parseInt(aval));
            } else if (aname.equals("streaming-row-limit")) {
                cache.setStreamingRowLimit(Integer.parseInt(aval));
            } else if (aname.equals("row-limit")) {
                cache.setRowLimit(Integer.parseInt(aval));
            } else if (aname.equals("grouping-enabled")) {
                cache.setGroupingEnabled(Boolean.parseBoolean(aval));
            } else if (aname.equals("prompt-for-cross-joins")) {
                cache.setPromptForCrossJoins(Boolean.parseBoolean(aval));
            } else if (aname.equals("execute-queries-with-cross-joins")) {
                cache.setExecuteQueriesWithCrossJoins(Boolean.parseBoolean(aval));
            } else if (aname.equals("automatically-executing")) {
                cache.setAutomaticallyExecuting(Boolean.parseBoolean(aval));
            } else if (aname.equals("streaming")) {
                cache.setStreaming(Boolean.parseBoolean(aval));
            } else {
                logger.warn("Unexpected attribute of <query>: " + aname + "=" + aval);
            }
        }
        session.getWorkspace().addQuery(cache, session);
    } else if (name.equals("constants")) {
        createdObject = null;
        String uuid = attributes.getValue("uuid");
        checkMandatory("uuid", uuid);
        cache.getConstantsContainer().setUUID(uuid);
        Container constants = cache.getConstantsContainer();
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("uuid")) {
                // already loaded
            } else if (aname.equals("xpos")) {
                constants.setPosition(
                        new Point2D.Double(Double.parseDouble(aval), constants.getPosition().getY()));
                logger.debug("Constants container is at position " + constants.getPosition());
            } else if (aname.equals("ypos")) {
                constants.setPosition(
                        new Point2D.Double(constants.getPosition().getX(), Double.parseDouble(aval)));
            } else {
                logger.warn("Unexpected attribute of <constants>: " + aname + "=" + aval);
            }
        }
    } else if (name.equals("table")) {
        createdObject = null;
        String tableName = attributes.getValue("name");
        String schema = attributes.getValue("schema");
        String catalog = attributes.getValue("catalog");
        String uuid = attributes.getValue("uuid");
        checkMandatory("uuid", uuid);
        checkMandatory("name", tableName);
        TableContainer table = new TableContainer(uuid, cache.getDatabase(), tableName, schema, catalog,
                new ArrayList<SQLObjectItem>());
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name") || aname.equals("schema") || aname.equals("catalog")
                    || aname.equals("uuid")) {
                // already loaded.
            } else if (aname.equals("xpos")) {
                table.setPosition(new Point2D.Double(Double.parseDouble(aval), table.getPosition().getY()));
            } else if (aname.equals("ypos")) {
                table.setPosition(new Point2D.Double(table.getPosition().getX(), Double.parseDouble(aval)));
            } else if (aname.equals("alias")) {
                table.setAlias(aval);
            } else {
                logger.warn("Unexpected attribute of <table>: " + aname + "=" + aval);
            }
        }
        container = table;
        containerItems = new ArrayList<SQLObjectItem>();
    } else if (name.equals("column")) {
        createdObject = null;
        if (parentIs("constants")) {
            String itemName = attributes.getValue("name");
            String uuid = attributes.getValue("id");
            checkMandatory("name", itemName);
            checkMandatory("id", uuid);
            Item item = new StringItem(itemName, uuid);
            for (int i = 0; i < attributes.getLength(); i++) {
                String aname = attributes.getQName(i);
                String aval = attributes.getValue(i);
                if (aname.equals("name") || aname.equals("id")) {
                    //already loaded.
                } else if (aname.equals("alias")) {
                    item.setAlias(aval);
                } else if (aname.equals("where-text")) {
                    item.setWhere(aval);
                } else if (aname.equals("group-by")) {
                    item.setGroupBy(SQLGroupFunction.valueOf(aval));
                } else if (aname.equals("having")) {
                    item.setHaving(aval);
                } else if (aname.equals("order-by")) {
                    item.setOrderBy(OrderByArgument.valueOf(aval));
                } else {
                    logger.warn("Unexpected attribute of <constant-column>: " + aname + "=" + aval);
                }
            }
            cache.getConstantsContainer().addItem(item);
            uuidToItemMap.put(uuid, item);
        } else if (parentIs("table")) {
            String itemName = attributes.getValue("name");
            String uuid = attributes.getValue("id");
            checkMandatory("name", itemName);
            checkMandatory("id", uuid);
            SQLObjectItem item = new SQLObjectItem(itemName, uuid);
            for (int i = 0; i < attributes.getLength(); i++) {
                String aname = attributes.getQName(i);
                String aval = attributes.getValue(i);
                if (aname.equals("name") || aname.equals("id")) {
                    //already loaded.
                } else if (aname.equals("alias")) {
                    item.setAlias(aval);
                } else if (aname.equals("where-text")) {
                    item.setWhere(aval);
                } else if (aname.equals("group-by")) {
                    item.setGroupBy(SQLGroupFunction.valueOf(aval));
                } else if (aname.equals("having")) {
                    item.setHaving(aval);
                } else if (aname.equals("order-by")) {
                    item.setOrderBy(OrderByArgument.valueOf(aval));
                } else {
                    logger.warn("Unexpected attribute of <constant-column>: " + aname + "=" + aval);
                }
            }
            containerItems.add(item);
            uuidToItemMap.put(uuid, item);
        } else if (parentIs("select")) {
            String uuid = attributes.getValue("id");
            checkMandatory("id", uuid);
            if (uuidToItemMap.get(uuid) == null) {
                throw new IllegalStateException(
                        "Cannot find a column with id " + uuid + " to add to the select statement.");
            }
            cache.selectItem(uuidToItemMap.get(uuid));
        } else {
            throw new IllegalStateException(
                    "A column is being loaded that is not contained by any tables. Parent is "
                            + xmlContext.get(xmlContext.size() - 2));
        }
    } else if (name.equals("join")) {
        createdObject = null;
        String leftUUID = attributes.getValue("left-item-id");
        String rightUUID = attributes.getValue("right-item-id");
        checkMandatory("left-item-id", leftUUID);
        checkMandatory("right-item-id", rightUUID);
        Item leftItem = uuidToItemMap.get(leftUUID);
        Item rightItem = uuidToItemMap.get(rightUUID);
        if (leftItem == null) {
            throw new IllegalStateException(
                    "The left side of a join was not found. Trying to match UUID " + leftUUID);
        }
        if (rightItem == null) {
            throw new IllegalStateException(
                    "The right side of a join was not found. Trying to match UUID " + rightUUID);
        }
        SQLJoin join = new SQLJoin(leftItem, rightItem);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("left-item-id") || aname.equals("right-item-id")) {
                // already loaded
            } else if (aname.equals("left-is-outer")) {
                join.setLeftColumnOuterJoin(Boolean.parseBoolean(aval));
            } else if (aname.equals("right-is-outer")) {
                join.setRightColumnOuterJoin(Boolean.parseBoolean(aval));
            } else if (aname.equals("comparator")) {
                join.setComparator(aval);
            } else {
                logger.warn("Unexpected attribute of <join>: " + aname + "=" + aval);
            }
        }
        cache.addJoin(join);
    } else if (name.equals("select")) {
        createdObject = null;
        // Select portion loaded in the "column" part above.
    } else if (name.equals("global-where")) {
        createdObject = null;
        cache.setGlobalWhereClause(attributes.getValue("text"));
    } else if (name.equals("group-by-aggregate")) { // For backwards compatibility to Wabit 0.9.6 and older
        createdObject = null;
        String uuid = attributes.getValue("column-id");
        String aggregate = attributes.getValue("aggregate");
        checkMandatory("column-id", uuid);
        checkMandatory("aggregate", aggregate);
        Item item = uuidToItemMap.get(uuid);
        if (item == null) {
            throw new IllegalStateException(
                    "Could not get a column for grouping. Trying to match UUID " + uuid);
        }
        cache.setGroupingEnabled(true);
        item.setGroupBy(SQLGroupFunction.getGroupType(aggregate));
    } else if (name.equals("having")) { // For backwards compatibility to Wabit 0.9.6 and older
        createdObject = null;
        String uuid = attributes.getValue("column-id");
        String text = attributes.getValue("text");
        checkMandatory("column-id", uuid);
        checkMandatory("text", text);
        Item item = uuidToItemMap.get(uuid);
        if (item == null) {
            throw new IllegalStateException(
                    "Could not get a column to add a having filter. Trying to match UUID " + uuid);
        }
        cache.setGroupingEnabled(true);
        item.setHaving(text);
    } else if (name.equals("order-by")) {
        createdObject = null;
        String uuid = attributes.getValue("column-id");
        checkMandatory("column-id", uuid);
        Item item = uuidToItemMap.get(uuid);
        if (item == null) {
            throw new IllegalStateException(
                    "Could not get a column to add order by to the select statement. Trying to match UUID "
                            + uuid);
        }
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("column-id")) {
                //already loaded.
            }
            if (aname.equals("direction")) {// For backwards compatibility to Wabit 0.9.6 and older
                item.setOrderBy(OrderByArgument.valueOf(aval));
            } else {
                logger.warn("Unexpected attribute of <order-by>: " + aname + " = " + aval);
            }
        }
        //Reinserting the items for cases where when the items were first created they defined a sort
        //order and were placed in the query in an incorrect order to sort the columns in.
        cache.moveOrderByItemToEnd(item);
    } else if (name.equals("query-string")) {
        createdObject = null;
        String queryString = attributes.getValue("string");
        checkMandatory("string", queryString);
        cache.setUserModifiedQuery(queryString);
    } else if (name.equals("text") && parentIs("query")) {
        createdObject = null;
    } else if (name.equals("olap-query")) {
        olapName = attributes.getValue("name");
        olapID = attributes.getValue("uuid");
        String dsName = attributes.getValue("data-source");
        olapDataSource = session.getWorkspace().getDataSource(dsName, Olap4jDataSource.class);
        if (olapDataSource == null) {
            String newDSName = oldToNewDSNames.get(dsName);
            if (newDSName != null) {
                olapDataSource = session.getWorkspace().getDataSource(newDSName, Olap4jDataSource.class);
                if (olapDataSource == null) {
                    logger.debug("Data source " + dsName
                            + " is not in the workspace or was not of the correct type. Attempted to replace with new data source "
                            + newDSName + ". Query " + "data-source" + " was connected to it previously.");
                    throw new NullPointerException("Data source " + newDSName
                            + " was not found in the workspace or was not an Olap4j Datasource.");
                }
            }
            logger.debug("Workspace has data sources " + session.getWorkspace().getDataSources());
        }
        createdObject = null;
    } else if (name.equals("olap-cube")) {
        catalogName = attributes.getValue("catalog");
        schemaName = attributes.getValue("schema");
        cubeName = attributes.getValue("cube-name");
        createdObject = null;
    } else if (name.equals("olap4j-query")) {
        olapQuery = new OlapQuery(olapID, session.getContext(), attributes.getValue("name"),
                attributes.getValue("name"), catalogName, schemaName, cubeName,
                attributes.getValue("modifiedOlapQuery"), !isInLayout);
        olapQuery.setName(olapName);
        olapQuery.setOlapDataSource(olapDataSource);
        if (cellSetRenderer == null) {
            session.getWorkspace().addOlapQuery(olapQuery);
        } else {
            cellSetRenderer.setModifiedOlapQuery(olapQuery);
        }
        createdObject = null;
    } else if (name.equals("olap4j-axis")) {
        olapAxis = new WabitOlapAxis(
                org.olap4j.Axis.Factory.forOrdinal(Integer.parseInt(attributes.getValue("ordinal"))));
        olapQuery.addAxis(olapAxis);
        createdObject = olapAxis;
    } else if (name.equals("olap4j-dimension")) {
        olapDimension = new WabitOlapDimension(attributes.getValue("dimension-name"));
        olapAxis.addDimension(olapDimension);
        createdObject = olapDimension;
    } else if (name.equals("olap4j-selection")) {
        WabitOlapInclusion olapInclusion = new WabitOlapInclusion(
                Operator.valueOf(attributes.getValue("operator")), attributes.getValue("unique-member-name"));
        olapDimension.addInclusion(olapInclusion);
        createdObject = olapInclusion;
    } else if (name.equals("olap4j-exclusion")) {
        WabitOlapExclusion olapExclusion = new WabitOlapExclusion(
                Operator.valueOf(attributes.getValue("operator")), attributes.getValue("unique-member-name"));
        olapDimension.addExclusion(olapExclusion);
        createdObject = olapExclusion;
    } else if (name.equals("wabit-image")) {
        currentWabitImage = new WabitImage();
        createdObject = currentWabitImage;
        session.getWorkspace().addImage(currentWabitImage);

        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                //already loaded
            } else {
                logger.warn("Unexpected attribute of <wabit-image>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("chart")) {
        chart = new Chart();
        createdObject = chart;
        session.getWorkspace().addChart(chart);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("uuid")) {
                //already loaded
            } else if (aname.equals("y-axis-name")) {
                chart.setYaxisName(aval);
            } else if (aname.equals("x-axis-name")) {
                chart.setXaxisName(aval);
            } else if (aname.equals("x-axis-label-rotation")) {
                chart.setXAxisLabelRotation(Double.parseDouble(aval));
            } else if (aname.equals("type")) {
                chart.setType(ChartType.valueOf(aval));
            } else if (aname.equals("legend-position")) {
                chart.setLegendPosition(LegendPosition.valueOf(aval));
            } else if (aname.equals("gratuitous-animation")) {
                chart.setGratuitouslyAnimated(Boolean.parseBoolean(aval));

            } else if (aname.equals("auto-x-axis")) {
                chart.setAutoXAxisRange(Boolean.parseBoolean(aval));
            } else if (aname.equals("auto-y-axis")) {
                chart.setAutoXAxisRange(Boolean.parseBoolean(aval));

            } else if (aname.equals("x-axis-max")) {
                chart.setXAxisMaxRange(Double.parseDouble(aval));
            } else if (aname.equals("y-axis-max")) {
                chart.setYAxisMaxRange(Double.parseDouble(aval));
            } else if (aname.equals("x-axis-min")) {
                chart.setXAxisMinRange(Double.parseDouble(aval));
            } else if (aname.equals("y-axis-min")) {
                chart.setYAxisMinRange(Double.parseDouble(aval));

            } else if (aname.equals("query-id")) {
                QueryCache query = null;
                for (QueryCache q : session.getWorkspace().getQueries()) {
                    if (q.getUUID().equals(aval)) {
                        query = q;
                        break;
                    }
                }
                if (query != null) {
                    chart.setQuery(query);
                }
                OlapQuery olapQuery = null;
                for (OlapQuery q : session.getWorkspace().getOlapQueries()) {
                    if (q.getUUID().equals(aval)) {
                        olapQuery = q;
                        break;
                    }
                }
                if (olapQuery != null) {
                    try {
                        chart.setMagicEnabled(false);
                        chart.setQuery(olapQuery);
                    } finally {
                        chart.setMagicEnabled(true);
                    }
                }
                if (query == null && olapQuery == null) {
                    throw new IllegalArgumentException(
                            "The query with UUID " + aval + " is missing from this project.");
                }
            } else {
                logger.warn("Unexpected attribute of <chart>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("chart-column")) {
        ChartColumn colIdentifier = loadColumnIdentifier(attributes, "");
        createdObject = colIdentifier;
        if (colIdentifier == null) {
            throw new IllegalStateException("The chart " + chart.getName() + " with uuid " + chart.getUUID()
                    + " has a missing column identifier when ordering columns and cannot be loaded.");
        }

        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                //already handled
            } else if (aname.equals("role")) {
                colIdentifier.setRoleInChart(ColumnRole.valueOf(aval));
            } else if (aname.matches("x-axis-.*")) {
                ChartColumn xAxisIdentifier = loadColumnIdentifier(attributes, "x-axis-");
                colIdentifier.setXAxisIdentifier(xAxisIdentifier);
            }
        }

        if (readingMissingChartCols) {
            chart.addMissingIdentifier(colIdentifier);
        } else {
            chart.addChartColumn(colIdentifier);
        }

    } else if (name.equals("missing-columns")) {
        readingMissingChartCols = true;
        createdObject = null;

    } else if (name.equals("layout")) {
        this.isInLayout = true;
        String layoutName = attributes.getValue("name");
        checkMandatory("name", layoutName);
        if (attributes.getValue("template") == null || !Boolean.parseBoolean(attributes.getValue("template"))) {
            layout = new Report(layoutName);
            session.getWorkspace().addReport((Report) layout);
        } else {
            layout = new Template(layoutName);
            session.getWorkspace().addTemplate((Template) layout);
        }
        createdObject = layout;
        selectorContainer = layout;

        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                //already loaded
            } else if (aname.equals("zoom")) {
                layout.setZoomLevel(Integer.parseInt(aval));
            } else {
                logger.warn("Unexpected attribute of <layout>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("layout-page")) {
        Page page = layout.getPage();
        createdObject = page;
        //Remove all guides from the page as they will be loaded in a later
        //part of this handler.
        for (WabitObject object : page.getChildren()) {
            if (object instanceof Guide) {
                page.removeGuide((Guide) object);
            }
        }

        //This sets the orientation before setting the width and height to prevent
        //a change in the orientation from switching the width and height. If the
        //orientation changes between portrait and landscape the width and height
        //values are swapped.
        String orientation = attributes.getValue("orientation");
        if (orientation != null) {
            // XXX the null check is for compatibility with export-format 1.0.0
            page.setOrientation(PageOrientation.valueOf(orientation));
        }

        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                // already loaded
            } else if (aname.equals("height")) {
                page.setHeight(Integer.parseInt(aval));
            } else if (aname.equals("width")) {
                page.setWidth(Integer.parseInt(aval));
            } else if (aname.equals("orientation")) {
                //already loaded
            } else {
                logger.warn("Unexpected attribute of <layout-page>: " + aname + "=" + aval);
            }
        }
    } else if (name.equals("content-box")) {
        contentBox = new ContentBox();
        createdObject = contentBox;
        selectorContainer = contentBox;
        layout.getPage().addContentBox(contentBox);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                // already loaded
            } else if (aname.equals("width")) {
                contentBox.setWidth(Double.parseDouble(aval));
            } else if (aname.equals("height")) {
                contentBox.setHeight(Double.parseDouble(aval));
            } else if (aname.equals("xpos")) {
                contentBox.setX(Double.parseDouble(aval));
            } else if (aname.equals("ypos")) {
                contentBox.setY(Double.parseDouble(aval));
            } else {
                logger.warn("Unexpected attribute of <content-box>: " + aname + "=" + aval);
            }
        }
    } else if (name.equals("content-label")) {
        WabitLabel label = new WabitLabel();
        createdObject = label;
        contentBox.setContentRenderer(label);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                //handled elsewhere
            } else if (aname.equals("text")) {
                label.setText(aval);
            } else if (aname.equals("horizontal-align")) {
                label.setHorizontalAlignment(HorizontalAlignment.valueOf(aval));
            } else if (aname.equals("vertical-align")) {
                label.setVerticalAlignment(VerticalAlignment.valueOf(aval));
            } else if (aname.equals("bg-colour")) {
                label.setBackgroundColour(new Color(Integer.parseInt(aval)));
            } else {
                logger.warn("Unexpected attribute of <content-label>: " + aname + "=" + aval);
            }
        }
    } else if (name.equals("text") && parentIs("content-label")) {
        createdObject = null;
    } else if (name.equals("image-renderer")) {
        imageRenderer = new ImageRenderer();
        createdObject = imageRenderer;
        //Old image renderers always had the image in the top left. If
        //the file is new it will have the horizontal and vertical alignments
        //set.
        imageRenderer.setHAlign(HorizontalAlignment.LEFT);
        imageRenderer.setVAlign(VerticalAlignment.TOP);

        contentBox.setContentRenderer(imageRenderer);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("name")) {
                //Handled elsewhere
            } else if (aname.equals("wabit-image-uuid")) {
                for (WabitImage image : session.getWorkspace().getImages()) {
                    if (image.getUUID().equals(aval)) {
                        imageRenderer.setImage(image);
                        break;
                    }
                }
                if (imageRenderer.getImage() == null) {
                    throw new IllegalStateException("Could not load the workspace as the report "
                            + layout.getName() + " is missing the image " + aval);
                }
            } else if (aname.equals("preserving-aspect-ratio")) {
                imageRenderer.setPreservingAspectRatio(Boolean.valueOf(aval));
            } else if (aname.equals("h-align")) {
                imageRenderer.setHAlign(HorizontalAlignment.valueOf(aval));
            } else if (aname.equals("v-align")) {
                imageRenderer.setVAlign(VerticalAlignment.valueOf(aval));
            } else {
                logger.warn("Unexpected attribute of <image-renderer>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("chart-renderer")) {
        String chartUuid = attributes.getValue("chart-uuid");
        Chart chart = session.getWorkspace().findByUuid(chartUuid, Chart.class);
        if (chart == null) {
            throw new IllegalStateException("Missing chart with UUID " + chartUuid + ", which is supposed"
                    + " to be attached to a chart renderer");
        }

        final ChartRenderer chartRenderer = new ChartRenderer(chart);
        createdObject = chartRenderer;
        contentBox.setContentRenderer(chartRenderer);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("uuid")) {
                // handled elsewhere
            } else if (aname.equals("chart-uuid")) {
                // already handled
            } else if (aname.equals("name")) {
                // handled elsewhere
            } else {
                logger.warn("Unexpected attribute of <chart-renderer>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("content-result-set")) {
        String queryID = attributes.getValue("query-id");
        checkMandatory("query-id", queryID);
        WabitResultSetProducer query = session.getWorkspace().findByUuid(queryID, WabitResultSetProducer.class);
        rsRenderer = new ResultSetRenderer(query);
        createdObject = rsRenderer;
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("query-id")) {
                // handled elsewhere
            } else if (aname.equals("name")) {
                // handled elsewhere
            } else if (aname.equals("null-string")) {
                rsRenderer.setNullString(aval);
            } else if (aname.equals("bg-colour")) {
                Color color = new Color(Integer.parseInt(aval));
                logger.debug("Renderer has background " + color.getRed() + ", " + color.getBlue() + ", "
                        + color.getGreen());
                rsRenderer.setBackgroundColour(color);
            } else if (aname.equals("data-colour")) {
                Color color = new Color(Integer.parseInt(aval));
                rsRenderer.setDataColour(color);
            } else if (aname.equals("header-colour")) {
                Color color = new Color(Integer.parseInt(aval));
                rsRenderer.setHeaderColour(color);
            } else if (aname.equals("border")) {
                rsRenderer.setBorderType(BorderStyles.valueOf(aval));
            } else if (aname.equals("grand-totals")) {
                rsRenderer.setPrintingGrandTotals(Boolean.parseBoolean(aval));
            } else {
                logger.warn("Unexpected attribute of <content-result-set>: " + aname + "=" + aval);
            }
        }
        //columnInfoList.clear();
        contentBox.setContentRenderer(rsRenderer);
    } else if (name.equals("header-font")) {
        if (parentIs("content-result-set")) {
            rsRenderer.setHeaderFont(loadFont(attributes));
            createdObject = null;
        } else {
            throw new IllegalStateException("There are no header fonts defined for the parent "
                    + xmlContext.get(xmlContext.size() - 2));
        }
    } else if (name.equals("body-font")) {
        if (parentIs("content-result-set")) {
            createdObject = null;
            rsRenderer.setBodyFont(loadFont(attributes));
        } else {
            throw new IllegalStateException(
                    "There are no body fonts defined for the parent " + xmlContext.get(xmlContext.size() - 2));
        }
    } else if (name.equals("column-info")) {
        colInfo = null;
        createdObject = null; //Not going to set name later, as this may break alias association
        String colInfoName = attributes.getValue("name");
        String colInfoItem = attributes.getValue("column-info-item-id");

        /*
         * XXX This retro compatibility fix forces us to violate
         * proper encapsulation and cannot be maintained if we want to
         * display OLAP queries in a RSRenderer. Imma comment it
         * out for now and evaluate later if we want to keep it.
         * It is for retrocompatibility with 0.9.1, which is a terribly
         * old version anyways. I'm not even sure we can load those files
         * now anyways.
         */
        //           //For backwards compatability with 0.9.1
        //           String colInfoKey = attributes.getValue("column-info-key");
        //           if (colInfoKey != null && colInfoItem == null) {
        //              Query q = rsRenderer.getContent();
        //              for (Map.Entry<String, Item> entry : uuidToItemMap.entrySet()) {
        //                 Item item = entry.getValue();
        //                 if (q.getSelectedColumns().contains(item) && (item.getAlias().equals(colInfoKey) || item.getName().equals(colInfoKey))) {
        //                    colInfoItem = entry.getKey();
        //                    break;
        //                 }
        //              }
        //              if (colInfoItem == null) {
        //                 colInfo = new ColumnInfo(colInfoKey, colInfoName);
        //              }
        //           }

        String colAlias = attributes.getValue("column-alias");
        if (colInfo == null && colAlias != null && colInfoItem == null) {
            colInfo = new ColumnInfo(colAlias, colInfoName);
        }

        checkMandatory("name", colInfoName);
        if (colInfo == null) {
            colInfo = new ColumnInfo(uuidToItemMap.get(colInfoItem), colInfoName);
        }
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("column-info-key") || aname.equals("name")) {
                //already loaded
            } else if (aname.equals("uuid")) {
                colInfo.setUUID(aval);
            } else if (aname.equals("width")) {
                colInfo.setWidth(Integer.parseInt(aval));
            } else if (aname.equals("horizontal-align")) {
                colInfo.setHorizontalAlignment(HorizontalAlignment.valueOf(aval));
            } else if (aname.equals("data-type")) {
                colInfo.setDataType(DataType.valueOf(aval));
            } else if (aname.equals("break-on-column")) {
                if (Boolean.parseBoolean(aval)) {
                    colInfo.setWillGroupOrBreak(GroupAndBreak.GROUP);
                } else {
                    colInfo.setWillGroupOrBreak(GroupAndBreak.NONE);
                }
            } else if (aname.equals("group-or-break")) {
                colInfo.setWillGroupOrBreak(GroupAndBreak.valueOf(aval));
            } else if (aname.equals("will-subtotal")) {
                colInfo.setWillSubtotal(Boolean.parseBoolean(aval));
            } else {
                logger.warn("Unexpected attribute of <column-info>: " + aname + "=" + aval);
            }
        }
        rsRenderer.addChild(colInfo, rsRenderer.getChildren().size());
    } else if (name.equals("date-format")) {
        createdObject = null;
        if (parentIs("column-info")) {
            String format = attributes.getValue("format");
            checkMandatory("format", format);
            colInfo.setFormat(new SimpleDateFormat(format));
        } else {
            throw new IllegalStateException(
                    "There is no date format defined for the parent " + xmlContext.get(xmlContext.size() - 2));
        }
    } else if (name.equals("decimal-format")) {
        createdObject = null;
        if (parentIs("column-info")) {
            String format = attributes.getValue("format");
            checkMandatory("format", format);
            colInfo.setFormat(new DecimalFormat(format));
        } else {
            throw new IllegalStateException(
                    "There is no date format defined for the parent " + xmlContext.get(xmlContext.size() - 2));
        }

    } else if (name.equals("selector")) {

        String type = attributes.getValue("type");

        if (type.equals(ComboBoxSelector.class.getSimpleName())) {
            this.selector = new ComboBoxSelector();
        } else if (type.equals(TextBoxSelector.class.getSimpleName())) {
            this.selector = new TextBoxSelector();
        } else if (type.equals(DateSelector.class.getSimpleName())) {
            this.selector = new DateSelector();
        } else {
            throw new IllegalStateException("Cannot create a selector of type " + type);
        }

        if (selectorContainer == null) {
            throw new AssertionError("Program error. 'selectorContainer' not set.");
        } else if (selectorContainer instanceof Report || selectorContainer instanceof ContentBox) {
            selectorContainer.addChild(selector, selectorContainer.getChildren(Selector.class).size());
        } else {
            throw new IllegalStateException("Selectors can only be added to reports and content boxes..");
        }

        createdObject = this.selector;

    } else if (name.equals("selector-config")) {

        if (selector instanceof ComboBoxSelector) {
            ((ComboBoxSelector) selector).setSourceKey(attributes.getValue("sourceKey"));
            ((ComboBoxSelector) selector).setStaticValues(attributes.getValue("staticValues"));
            ((ComboBoxSelector) selector).setDefaultValue(attributes.getValue("defaultValue"));
            ((ComboBoxSelector) selector).setAlwaysIncludeDefaultValue(
                    Boolean.valueOf(attributes.getValue("alwaysIncludeDefaultValue")));
        } else if (selector instanceof TextBoxSelector) {
            ((TextBoxSelector) selector).setDefaultValue(attributes.getValue("defaultValue"));
        } else if (selector instanceof DateSelector) {
            String defValueS = attributes.getValue("defaultValue");
            final Date defValue;
            if (defValueS == null) {
                defValue = null;
            } else {
                defValue = new DateConverter().convertToComplexType(defValueS);
            }
            ((DateSelector) selector).setDefaultValue(defValue);
        } else {
            throw new IllegalStateException("Cannot configure selector.");
        }

        createdObject = null;

    } else if (name.equals("cell-set-renderer")) {
        String queryUUID = attributes.getValue("olap-query-uuid");
        OlapQuery newQuery = null;
        for (OlapQuery query : session.getWorkspace().getOlapQueries()) {
            if (query.getUUID().equals(queryUUID)) {
                newQuery = query;
                break;
            }
        }
        if (newQuery == null) {
            throw new NullPointerException("Cannot load workspace due to missing olap query in report.");
        }
        cellSetRenderer = new CellSetRenderer(newQuery);
        createdObject = cellSetRenderer;
        contentBox.setContentRenderer(cellSetRenderer);
        for (int i = 0; i < attributes.getLength(); i++) {
            String aname = attributes.getQName(i);
            String aval = attributes.getValue(i);
            if (aname.equals("uuid") || aname.equals("olap-query-uuid")) {
                // handled elsewhere
            } else if (aname.equals("name")) {
                // handled elsewhere
            } else if (aname.equals("body-alignment")) {
                cellSetRenderer.setBodyAlignment(HorizontalAlignment.valueOf(aval));
            } else if (aname.equals("body-format-pattern")) {
                cellSetRenderer.setBodyFormat(new DecimalFormat(aval));
            } else {
                logger.warn("Unexpected attribute of <cell-set-renderer>: " + aname + "=" + aval);
            }
        }

    } else if (name.equals("olap-header-font")) {
        createdObject = null;
        cellSetRenderer.setHeaderFont(loadFont(attributes));
    } else if (name.equals("olap-body-font")) {
        createdObject = null;
        cellSetRenderer.setBodyFont(loadFont(attributes));
    } else if (name.equals("guide")) {
        String axisName = attributes.getValue("axis");
        String offsetAmount = attributes.getValue("offset");
        checkMandatory("axis", axisName);
        checkMandatory("offset", offsetAmount);
        Guide guide = new Guide(Axis.valueOf(axisName), Double.parseDouble(offsetAmount));
        createdObject = guide;
        layout.getPage().addGuide(guide);
    } else if (name.equals("font")) {
        createdObject = null;
        Font font = loadFont(attributes);
        if (parentIs("layout-page")) {
            layout.getPage().setDefaultFont(font);
        } else if (parentIs("content-box")) {
            contentBox.setFont(font);
        } else if (parentIs("content-label")) {
            ((WabitLabel) contentBox.getContentRenderer()).setFont(font);
        }
    } else {
        createdObject = null;
        logger.warn("Unknown object type: " + name);
    }

    if (createdObject != null) {
        String valName = attributes.getValue("name");
        String valUUID = attributes.getValue("uuid");
        if (nameMandatory) {
            checkMandatory("name", valName);
        }
        if (uuidMandatory) {
            checkMandatory("uuid", valUUID);
        }
        if (valName != null) {
            createdObject.setName(valName);
        }
        if (valUUID != null) {
            createdObject.setUUID(valUUID);
        }

        progressMessage = session.getWorkspace().getName() + ": reading " + valName;
    }

}

From source file:nl.coinsweb.sdk.FileManager.java

public static String getXmlBaseOrxmlns(File xmlFile) {
    final ArrayList<String> baseUriDropHere = new ArrayList<>();
    final ArrayList<String> xmlnsDropHere = new ArrayList<>();

    DefaultHandler handler = new DefaultHandler() {

        @Override/*from  w ww  .j  a  v a 2s.  c  o m*/
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {

            if ("rdf:RDF".equals(qName)) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    if ("xml:base".equals(attributes.getQName(i))) {
                        baseUriDropHere.add(attributes.getValue(i));
                    }
                    if ("xmlns".equals(attributes.getQName(i))) {
                        xmlnsDropHere.add(attributes.getValue(i));
                    }
                }
                return;
            }
        }
    };

    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        SAXParser parser = factory.newSAXParser();
        parser.parse(xmlFile, handler);

    } catch (ParserConfigurationException e) {
        // do not print this, this is supposed to crash for non-xml files
    } catch (SAXException e) {
        // do not print this, this is supposed to crash for non-xml files
    } catch (IOException e) {
        log.error("problem reading xml file", e);
    }

    if (baseUriDropHere.isEmpty()) {
        if (xmlnsDropHere.isEmpty()) {
            return null;
        } else {
            return xmlnsDropHere.get(0);
        }
    } else {
        return baseUriDropHere.get(0);
    }
}

From source file:nl.nn.adapterframework.configuration.AbstractSpringPoweredDigesterFactory.java

protected Map<String, String> copyAttrsToMap(Attributes attrs) {
    Map<String, String> map = new HashMap<String, String>(attrs.getLength());
    for (int i = 0; i < attrs.getLength(); ++i) {
        String value = attrs.getValue(i);
        map.put(attrs.getQName(i), value);
    }//w ww .ja va 2s  .  c  o m
    return map;
}

From source file:nl.nn.adapterframework.configuration.AttributeCheckingRule.java

public void begin(String uri, String elementName, Attributes attributes) throws Exception {

    Object top = digester.peek();

    for (int i = 0; i < attributes.getLength(); i++) {
        String name = attributes.getLocalName(i);
        if ("".equals(name)) {
            name = attributes.getQName(i);
        }//  ww w  .j  a v a 2  s  .  c o m
        if (name != null && !name.equals("className")) {
            //            if (log.isDebugEnabled()) {
            //               log.debug(getObjectName(top)+" checking for setter for attribute ["+name+"]");
            //            }
            PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(top, name);
            Method m = null;
            if (pd != null) {
                m = PropertyUtils.getWriteMethod(pd);
            }
            if (m == null) {
                Locator loc = digester.getDocumentLocator();
                String msg = "line " + loc.getLineNumber() + ", col " + loc.getColumnNumber() + ": "
                        + getObjectName(top) + " does not have an attribute [" + name + "] to set to value ["
                        + attributes.getValue(name) + "]";
                configWarnings.add(log, msg);
            }
        }
    }

}

From source file:nonjsp.application.XmlXulRuleSet.java

/**
 * This method is invoked when the beginning of the matched
 * Xml element is encountered ;/*from   w  w  w .j av  a2s. c  o  m*/
 *
 * @param attributes The element's attribute list
 */
public void begin(Attributes attributes) throws Exception {
    UIComponent uic = (UIComponent) digester.peek();
    if (log.isTraceEnabled()) {
        log.trace("component: " + uic.getId());
    }
    AttributesImpl attrs = new AttributesImpl(attributes);
    for (int i = 0; i < attrs.getLength(); i++) {
        String qName = attributes.getQName(i);
        attrs.setLocalName(i, qName);
        attrs.setValue(i, attributes.getValue(qName));
        if (log.isTraceEnabled()) {
            log.trace("ComponentRule: qName: " + qName + " value: " + attributes.getValue(qName));
        }
    }
    bc.applyAttributesToComponentInstance(uic, attrs);

    if (root == null) {
        root = (UIComponent) digester.peek(digester.getCount() - 1);
    }
    root.getChildren().add(uic);

    //If component is a form, make it the root so that children will be
    //added to it
    if (uic instanceof UIForm) {
        root = uic;
    }
}

From source file:nonjsp.application.XmlXulRuleSet.java

/**
 * This method is invoked when the beginning of the matched
 * Xml element is encountered (in this case "property");
 *
 * @param attributes The element's attribute list
 *///from w ww .j  av  a  2 s .  c  om
public void begin(Attributes attributes) throws Exception {
    UIComponent uic = (UIComponent) digester.peek();
    AttributesImpl attrs = new AttributesImpl(attributes);
    for (int i = 0; i < attrs.getLength(); i++) {
        String qName = attributes.getQName(i);
        attrs.setLocalName(i, qName);
        attrs.setValue(i, attributes.getValue(qName));
    }
    bc.handleNestedComponentTag(uic, "SelectOne_Option", attrs);
}

From source file:ome.dsl.SaxReader.java

/** simple outputting routine with indention */
private void outputStart(String element, Attributes attrs) {
    if (log.isDebugEnabled()) {
        log.debug(depth + element + "(");
    }/*  www.j  a  va  2s . co m*/
    for (int i = 0; i < attrs.getLength(); i++) {
        String attr = attrs.getQName(i);
        String value = attrs.getValue(i);
        if (log.isDebugEnabled()) {
            log.debug(" " + attr + "=\"" + value + "\" ");
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("): ");
    }
    depth += "  ";
}

From source file:ome.dsl.SaxReader.java

/** converts xml attributes to java.util.Properties */
private Properties attrs2props(Attributes attrs) {
    Properties p = new Properties();
    for (int i = 0; i < attrs.getLength(); i++) {
        String key = attrs.getQName(i);
        String value = attrs.getValue(i);
        p.put(key, value);//from w w  w . j  av a  2  s . com
    }
    return p;
}

From source file:org.ajax4jsf.renderkit.compiler.PutAttributesRule.java

public void begin(String namespace, String element, Attributes attrs) throws Exception {
    Object top = digester.peek();
    if (null != top) {
        Method put = top.getClass().getDeclaredMethod(methodName, new Class[] { Object.class, Object.class });
        for (int i = 0; i < attrs.getLength(); i++) {
            String qName = attrs.getQName(i);
            String value = attrs.getValue(i);
            if (!toSkip.contains(qName)) {
                put.invoke(top, new Object[] { qName, value });
            }/*  ww w . j a  va 2  s  .c om*/
        }
    }
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Writes (using the Writer) the start tag for element QName along with the
 * indicated attributes and namespace mappings.
 * @param qName is the name of the element
 * @param attributes are the attributes to write
 *//*from w ww  .  ja va 2  s  .  c o m*/
public void startElement(QName qName, Attributes attributes) throws IOException {
    java.util.ArrayList vecQNames = null;
    if (debugEnabled) {
        log.debug(Messages.getMessage("startElem00",
                "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
    }

    if (startOfDocument && sendXMLDecl) {
        writeXMLDeclaration();
    }

    if (writingStartTag) {
        writer.write('>');
        if (pretty)
            writer.write('\n');
        indent++;
    }

    if (pretty)
        for (int i = 0; i < indent; i++)
            writer.write(' ');
    String elementQName = qName2String(qName, true);
    writer.write('<');

    writer.write(elementQName);

    if (writeXMLType != null) {
        attributes = setTypeAttribute(attributes, writeXMLType);
        writeXMLType = null;
    }

    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            String qname = attributes.getQName(i);
            writer.write(' ');

            String prefix = "";
            String uri = attributes.getURI(i);
            if (uri != null && uri.length() > 0) {
                if (qname.length() == 0) {
                    // If qname isn't set, generate one
                    prefix = getPrefixForURI(uri);
                } else {
                    // If it is, make sure the prefix looks reasonable.
                    int idx = qname.indexOf(':');
                    if (idx > -1) {
                        prefix = qname.substring(0, idx);
                        prefix = getPrefixForURI(uri, prefix, true);
                    }
                }
                if (prefix.length() > 0) {
                    qname = prefix + ':' + attributes.getLocalName(i);
                } else {
                    qname = attributes.getLocalName(i);
                }
            } else {
                qname = attributes.getQName(i);
                if (qname.length() == 0)
                    qname = attributes.getLocalName(i);
            }

            if (qname.startsWith("xmlns")) {
                if (vecQNames == null)
                    vecQNames = new ArrayList();
                vecQNames.add(qname);
            }
            writer.write(qname);
            writer.write("=\"");

            getEncoder().writeEncoded(writer, attributes.getValue(i));

            writer.write('"');
        }
    }

    if (noNamespaceMappings) {
        nsStack.push();
    } else {
        for (Mapping map = nsStack.topOfFrame(); map != null; map = nsStack.next()) {
            if (!(map.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && map.getPrefix().equals("xmlns"))
                    && !(map.getNamespaceURI().equals(Constants.NS_URI_XML) && map.getPrefix().equals("xml"))) {
                StringBuffer sb = new StringBuffer("xmlns");
                if (map.getPrefix().length() > 0) {
                    sb.append(':');
                    sb.append(map.getPrefix());
                }
                if ((vecQNames == null) || (vecQNames.indexOf(sb.toString()) == -1)) {
                    writer.write(' ');
                    sb.append("=\"");
                    sb.append(map.getNamespaceURI());
                    sb.append('"');
                    writer.write(sb.toString());
                }
            }
        }

        noNamespaceMappings = true;
    }

    writingStartTag = true;

    elementStack.push(elementQName);

    onlyXML = true;
}