Example usage for org.apache.wicket.markup.parser XmlTag isOpen

List of usage examples for org.apache.wicket.markup.parser XmlTag isOpen

Introduction

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

Prototype

public boolean isOpen() 

Source Link

Document

Gets whether this is an open tag.

Usage

From source file:com.servoy.j2db.server.headlessclient.dataui.StripHTMLTagsConverter.java

License:Open Source License

/**
 * @param bodyText/*from   w  w w .ja va 2 s  .  co  m*/
 * @param solution
 * @return
 */
@SuppressWarnings("nls")
public static StrippedText convertBodyText(Component component, CharSequence bodyText,
        FlattenedSolution solutionRoot) {
    StrippedText st = new StrippedText();
    if (RequestCycle.get() == null) {
        st.setBodyTxt(bodyText);
        return st;
    }

    ResourceReference rr = new ResourceReference("media"); //$NON-NLS-1$
    String solutionName = solutionRoot.getSolution().getName();

    StringBuffer bodyTxt = new StringBuffer(bodyText.length());
    XmlPullParser parser = new XmlPullParser();

    ICrypt urlCrypt = null;
    if (Application.exists())
        urlCrypt = Application.get().getSecuritySettings().getCryptFactory().newCrypt();

    try {
        parser.parse(new ByteArrayInputStream(bodyText.toString().getBytes("UTF8")), "UTF8"); //$NON-NLS-1$ //$NON-NLS-2$
        XmlTag me = (XmlTag) parser.nextTag();

        while (me != null) {
            CharSequence tmp = parser.getInputFromPositionMarker(me.getPos());
            if (tmp.toString().trim().length() > 0)
                bodyTxt.append(tmp);
            parser.setPositionMarker();

            String currentTagName = me.getName().toLowerCase();

            if (currentTagName.equals("script")) //$NON-NLS-1$
            {
                if (!me.isClose()) {
                    String srcUrl = (String) me.getAttributes().get("src"); //$NON-NLS-1$
                    if (srcUrl == null)
                        srcUrl = (String) me.getAttributes().get("SRC"); //$NON-NLS-1$
                    me = (XmlTag) parser.nextTag();
                    if (srcUrl != null) {
                        st.getJavascriptUrls()
                                .add(convertMediaReferences(srcUrl, solutionName, rr, "", true).toString());
                    } else {
                        if (me != null) {
                            st.getJavascriptScripts().add(parser.getInputFromPositionMarker(me.getPos()));
                            parser.setPositionMarker();
                        }
                    }
                } else {
                    me = (XmlTag) parser.nextTag();
                }
                continue;
            } else if (currentTagName.equals("style")) {
                if (me.isOpen()) {
                    me = (XmlTag) parser.nextTag();
                    List<CharSequence> styles = st.getStyles();
                    String style = parser.getInputFromPositionMarker(me.getPos()).toString().trim();
                    if (!"".equals(style) && !styles.contains(style)) {
                        styles.add(convertMediaReferences(style, solutionName, rr, "", false));
                    }
                    parser.setPositionMarker();
                } else {
                    me = (XmlTag) parser.nextTag();
                }
                continue;
            } else if (currentTagName.equals("link")) {
                if (me.isOpen() || me.isOpenClose()) {
                    String end = "\n";
                    if (me.isOpen())
                        end = "</link>\n";
                    st.getLinkTags().add(
                            convertMediaReferences(me.toXmlString(null) + end, solutionName, rr, "", false));
                }
                me = (XmlTag) parser.nextTag();
                continue;
            }
            if (ignoreTags.contains(currentTagName)) {
                if (currentTagName.equals("body") && (me.isOpen() || me.isOpenClose())) {
                    if (me.getAttributes().size() > 0) {
                        st.addBodyAttributes(me.getAttributes());
                    }
                    me = (XmlTag) parser.nextTag();
                } else {
                    me = (XmlTag) parser.nextTag();
                }
                continue;
            }

            if (currentTagName.equals("img") && component instanceof ILabel) {
                ILabel label = (ILabel) component;
                String onload = "Servoy.Utils.setLabelChildHeight('" + component.getMarkupId() + "', "
                        + label.getVerticalAlignment() + ");";
                onload = me.getAttributes().containsKey("onload")
                        ? me.getAttributes().getString("onload") + ";" + onload
                        : onload;
                me.getAttributes().put("onload", onload);
            }

            boolean ignoreOnclick = false;
            IValueMap attributeMap = me.getAttributes();
            // first transfer over the tabindex to anchor tags
            if (currentTagName.equals("a")) {
                int tabIndex = TabIndexHelper.getTabIndex(component);
                if (tabIndex != -1)
                    attributeMap.put("tabindex", Integer.valueOf(tabIndex));
            }
            // TODO attributes with casing?
            // now they have to be lowercase. (that is a xhtml requirement)
            for (String attribute : scanTags) {
                if (ignoreOnclick && attribute.equals("onclick")) //$NON-NLS-1$
                    continue;
                String src = attributeMap.getString(attribute);
                if (src == null) {
                    continue;
                }
                String lowercase = src.toLowerCase();
                if (lowercase.startsWith(MediaURLStreamHandler.MEDIA_URL_DEF)) {
                    String name = src.substring(MediaURLStreamHandler.MEDIA_URL_DEF.length());
                    if (name.startsWith(MediaURLStreamHandler.MEDIA_URL_BLOBLOADER)) {
                        String url = generateBlobloaderUrl(component, urlCrypt, name);
                        me.getAttributes().put(attribute, url);
                    } else {
                        String translatedUrl = MediaURLStreamHandler.getTranslatedMediaURL(solutionRoot,
                                lowercase);
                        if (translatedUrl != null) {
                            me.getAttributes().put(attribute, translatedUrl);
                        }
                    }
                } else if (component instanceof ISupportScriptCallback && lowercase.startsWith("javascript:")) {
                    String script = src;
                    if (script.length() > 13) {
                        String scriptName = script.substring(11);
                        if ("href".equals(attribute)) {
                            if (attributeMap.containsKey("externalcall")) {
                                attributeMap.remove("externalcall");
                            } else {
                                me.getAttributes().put("href", "#");
                                me.getAttributes().put("onclick",
                                        ((ISupportScriptCallback) component).getCallBackUrl(scriptName, true));
                                ignoreOnclick = true;
                            }
                        } else {
                            me.getAttributes().put(attribute, ((ISupportScriptCallback) component)
                                    .getCallBackUrl(scriptName, "onclick".equals(attribute)));
                        }
                    }
                } else if (component instanceof FormComponent<?> && lowercase.startsWith("javascript:")) {
                    String script = src;
                    if (script.length() > 13) {
                        String scriptName = script.substring(11);
                        if ("href".equals(attribute)) {
                            me.getAttributes().put("href", "#");
                            me.getAttributes().put("onclick",
                                    getTriggerJavaScript((FormComponent<?>) component, scriptName));
                            ignoreOnclick = true;
                        } else {
                            me.getAttributes().put(attribute,
                                    getTriggerJavaScript((FormComponent<?>) component, scriptName));
                        }
                    }
                }
            }
            bodyTxt.append(me.toString());
            me = (XmlTag) parser.nextTag();
        }
        bodyTxt.append(parser.getInputFromPositionMarker(-1));

        st.setBodyTxt(convertMediaReferences(convertBlobLoaderReferences(bodyTxt, component), solutionName, rr,
                "", false)); //$NON-NLS-1$
    } catch (ParseException ex) {
        Debug.error(ex);
        bodyTxt.append("<span style=\"color : #ff0000;\">"); //$NON-NLS-1$
        bodyTxt.append(ex.getMessage());
        bodyTxt.append(bodyText.subSequence(ex.getErrorOffset(),
                Math.min(ex.getErrorOffset() + 100, bodyText.length())));
        bodyTxt.append("</span></body></html>"); //$NON-NLS-1$
        st.setBodyTxt(bodyTxt);
    } catch (Exception ex) {
        Debug.error(ex);
        bodyTxt.append("<span style=\"color : #ff0000;\">"); //$NON-NLS-1$
        bodyTxt.append(ex.getMessage());
        bodyTxt.append("</span></body></html>"); //$NON-NLS-1$
        st.setBodyTxt(bodyTxt);
    }
    return st;
}

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

License:Apache License

public void exportCell(XmlTag tag, XmlPullParser parser, Cell cell, Component gridComponent)
        throws ParseException {
    XmlTag firstMostNestedTag = tag;/*from   w ww  .ja va  2s  .c  om*/
    // find the most inner tag value
    while ((tag = parser.nextTag()).getName().equals("td") == false) {
        if (tag.isOpen() || tag.isOpenClose()) {
            firstMostNestedTag = tag;
        } else {
            break;
        }
    }
    CharSequence possibleComponentReference = firstMostNestedTag
            .getAttribute(TableParser.OutputPathBehavior.PATH_ATTRIBUTE);
    if (possibleComponentReference != null) {
        Component firstMostNestedComponent = gridComponent.getPage().get(possibleComponentReference.toString());
        // exclude auto links
        if (firstMostNestedComponent.getClass().getName().contains("ResourceReferenceAutolink")) {
            cell.setCellValue("");
            return;
        }
        // handle links
        if (firstMostNestedComponent instanceof Link) {
            Link<?> link = (Link<?>) firstMostNestedComponent;
            firstMostNestedComponent = getLinkInnerComponent(link);
            if (firstMostNestedComponent == null) {
                cell.setCellValue("");
                return;
            }
        }
        Object modelValue = firstMostNestedComponent.getDefaultModelObject();
        if (modelValue != null) {
            if (modelValue instanceof Number) {
                handleNumber(cell, (Number) modelValue);
            } else if (modelValue instanceof CharSequence) {
                cell.setCellValue(modelValue.toString());
            } else if (modelValue instanceof CharSequence) {
                cell.setCellValue(modelValue.toString());
            } else if (modelValue instanceof Boolean) {
                cell.setCellValue((Boolean) modelValue);
            } else if (modelValue instanceof Calendar) {
                handleCalendar(cell, (Calendar) modelValue);
            } else if (modelValue instanceof Date) {
                handleDate(cell, (Date) modelValue);
            } else {
                cell.setCellValue(modelValue.toString());
            }
        }
    } else {
        // simply set the first most nested tag value
        String value = parser
                .getInput(firstMostNestedTag.getPos() + firstMostNestedTag.getLength(), tag.getPos())
                .toString();
        cell.setCellValue(value);
    }
}

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);/*from  w  w  w. j a v a 2  s .co  m*/
    XmlTag tag;
    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/*  w ww.ja v  a2s  .c  o  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;
    }
}