Example usage for org.apache.wicket.markup.parser XmlPullParser parse

List of usage examples for org.apache.wicket.markup.parser XmlPullParser parse

Introduction

In this page you can find the example usage for org.apache.wicket.markup.parser XmlPullParser parse.

Prototype

@Override
public void parse(final InputStream in) throws IOException 

Source Link

Document

Reads and parses markup from an input stream, using UTF-8 encoding by default when not specified in XML declaration.

Usage

From source file:org.wicketstuff.poi.excel.TableParser.java

License:Apache License

private void doParse(CharSequence gridComponentMarkup, Component tableComponent)
        throws IOException, ResourceStreamNotFoundException, ParseException {
    XmlPullParser parser = new XmlPullParser();
    parser.parse(gridComponentMarkup);
    XmlTag tag;//  w w  w .  j  av a  2 s.  c om
    int tableDeep = 0;
    while ((tag = parser.nextTag()) != null) {
        if ("table".equals(tag.getName().toLowerCase())) {
            if (tag.isOpen()) {
                tableDeep++;
            } else {
                tableDeep--;
            }
        }
        if (tableDeep > 1) {
            // we don't want to read inner tables
            continue;
        }
        if (tag.isOpen()) {
            String tagName = tag.getName().toLowerCase();

            if ("tr".equals(tagName)) {
                if (tableDeep == 0) {
                    // means that root table is outside the component markup
                    tableDeep = 1;
                }
                int index = row == null ? 0 : row.getRowNum() + 1;
                row = targetSheet.createRow(index);
                cell = null;
            } else if ("td".equals(tagName) || "th".equals(tagName)) {
                int index = cell == null ? 0 : cell.getColumnIndex() + 1 + colsToSpan;
                if (skipColumn(index)) {
                    index += columnSpan.get(index);
                }
                colsToSpan = 0;
                CharSequence rowspan = tag.getAttribute("rowspan");
                CharSequence colspan = tag.getAttribute("colspan");
                cell = row.createCell(index);
                if (rowspan != null || colspan != null) {
                    int rowsToSpan = rowspan == null ? 0 : Integer.valueOf(rowspan.toString()) - 1;
                    colsToSpan = colspan == null ? 0 : Integer.valueOf(colspan.toString()) - 1;

                    if (rowsToSpan > 0) {
                        rowsToSpanByColumn.put(index, rowsToSpan);
                        columnSpan.put(index, colsToSpan + 1);
                    }

                    int lastRowNum = row.getRowNum() + rowsToSpan;
                    int lastColIndex = index + colsToSpan;
                    targetSheet.addMergedRegion(new CellRangeAddress(//
                            row.getRowNum(), // first row (0-based)
                            lastRowNum, // last row (0-based)
                            index, // first column (0-based)
                            lastColIndex // last column (0-based)
                    ));
                }
                cellExporter.exportCell(tag, parser, cell, tableComponent);
            }
        }
    }
}

From source file:org.wicketstuff.push.cometd.CometdPushBehavior.java

License:Apache License

/**
 * Parse the web.xml to find cometd context Path. This context path will be cache for all the
 * application//ww  w  .j a  v  a  2 s . co m
 * 
 * @return cometd context path
 */
private static String guessCometdServletPath() {
    final ServletContext servletContext = ((WebApplication) Application.get()).getServletContext();
    final InputStream is = servletContext.getResourceAsStream("/WEB-INF/web.xml");

    /*
     * get the servlet name from class assignable to org.mortbay.cometd.CometdServlet
     */
    try {
        final XmlPullParser parser = new XmlPullParser();
        parser.parse(is);
        String urlPattern = null;

        while (true) {
            XmlTag elem;
            // go down until servlet is found
            do
                elem = parser.nextTag();
            while (elem != null && !(elem.getName().equals("servlet") && elem.isOpen()));

            // stop if elem is null
            if (elem == null)
                break;

            // get the servlet name for org.mortbay.cometd.CometdServlet
            String servletName = null, servletClassName = null;
            do {
                elem = parser.nextTag();
                if (elem.isOpen())
                    parser.setPositionMarker();
                else if (elem.isClose() && elem.getName().equals("servlet-name"))
                    servletName = parser.getInputFromPositionMarker(elem.getPos()).toString();
                else if (elem.isClose() && elem.getName().equals("servlet-class"))
                    servletClassName = parser.getInputFromPositionMarker(elem.getPos()).toString();
            } while (servletClassName == null
                    || !CometdServlet.class.isAssignableFrom(Class.forName(servletClassName)));

            if (servletName == null)
                break;

            // go down until servlet-mapping is found
            do
                elem = parser.nextTag();
            while (elem != null && !(elem.getName().equals("servlet-mapping") && elem.isOpen()));

            // stop if elem is null
            if (elem == null)
                break;

            // get the servlet name for org.mortbay.cometd.CometdServlet
            String servletNameMapping = null;
            do {
                elem = parser.nextTag();
                if (elem.isOpen())
                    parser.setPositionMarker();
                else if (elem.isClose() && elem.getName().equals("servlet-name"))
                    servletNameMapping = parser.getInputFromPositionMarker(elem.getPos()).toString();
            } while (!servletName.equals(servletNameMapping));

            // and the urlPattern
            do {
                elem = parser.nextTag();
                if (elem.isOpen())
                    parser.setPositionMarker();
                else if (elem.isClose() && elem.getName().equals("url-pattern"))
                    urlPattern = parser.getInputFromPositionMarker(elem.getPos()).toString();
            } while (urlPattern == null);

            // all it is found
            break;
        }

        if (urlPattern == null)
            throw new ServletException("Error searching for cometd Servlet");

        // Check for leading '/' and trailing '/*'.
        if (!urlPattern.startsWith("/") || !urlPattern.endsWith("/*"))
            throw new ServletException("Url pattern for cometd should start with / and finish with /*");

        // Strip trailing '/*'.
        return servletContext.getContextPath() + urlPattern.substring(0, urlPattern.length() - 2);

    } catch (final Exception ex) {
        final String path = servletContext.getContextPath() + "/cometd";
        LOG.warn("Error finding filter cometd servlet in web.xml using default path " + path, ex);
        return path;
    }
}