List of usage examples for org.xml.sax.helpers AttributesImpl AttributesImpl
public AttributesImpl()
From source file:org.xwiki.rendering.xdomxmlcurrent.internal.renderer.XDOMXMLChainingStreamRenderer.java
private AttributesImpl createStartAttributes(String blockName, List<Object> parameters) { AttributesImpl attributes = new AttributesImpl(); if (!isValidBlockElementName(blockName)) { attributes.addAttribute(null, null, XDOMXMLConstants.ATT_BLOCK_NAME, null, blockName); }/*from ww w. ja va 2 s.co m*/ if (parameters != null) { ListenerElement element = this.descriptor.getElements().get(blockName.toLowerCase()); addInlineParameters(attributes, parameters, element); } return attributes; }
From source file:org.xwiki.rendering.xdomxmlcurrent.internal.renderer.XDOMXMLChainingStreamRenderer.java
private void onEvent(String eventName, Object[] parameters) { String blockName = getBlockName(eventName, "on"); ListenerElement element = this.descriptor.getElements().get(blockName.toLowerCase()); List<Object> elementParameters = parameters != null ? Arrays.asList(parameters) : null; // Remove useless parameters removeDefaultParameters(elementParameters, element); // Put as attributes parameters which are simple enough to not require full XML serialization AttributesImpl attributes = (elementParameters != null && elementParameters.size() > 1) ? createStartAttributes(blockName, Arrays.asList(parameters)) : new AttributesImpl(); // Get proper element name String elementName;// w w w . j a v a 2s .co m if (isValidBlockElementName(blockName)) { elementName = blockName; } else { elementName = XDOMXMLConstants.ELEM_BLOCK; } // Print start element startElement(elementName, attributes); // Print complex parameters if (parameters != null && parameters.length == 1 && XDOMXMLCurrentUtils.isSimpleType(element.getParameters().get(0))) { String value = parameters[0].toString(); try { this.contentHandler.characters(value.toCharArray(), 0, value.length()); } catch (SAXException e) { throw new RuntimeException("Failed to send sax event", e); } } else { printParameters(elementParameters, element); } // Print end element endElement(elementName); }
From source file:org.xwiki.rendering.xdomxmlcurrent.internal.renderer.XDOMXMLChainingStreamRenderer.java
/** * Convert provided table into {@link Attributes} to use in xml writer. *///from ww w . j av a 2s.c o m private Attributes createAttributes(String[][] parameters) { AttributesImpl attributes = new AttributesImpl(); if (parameters != null && parameters.length > 0) { for (String[] entry : parameters) { attributes.addAttribute(null, null, entry[0], null, entry[1]); } } return attributes; }
From source file:org.xwiki.rendering.xml.internal.serializer.DefaultXMLSerializer.java
private AttributesImpl createStartAttributes(String blockName, List<Object> parameters) { AttributesImpl attributes = new AttributesImpl(); if (!isValidBlockElementName(blockName)) { attributes.addAttribute(null, null, this.configuration.getAttributeBlockName(), null, blockName); }//w w w .j a va 2 s. com if (parameters != null) { ListenerElement element = this.descriptor.getElements().get(blockName.toLowerCase()); addInlineParameters(attributes, parameters, element); } return attributes; }
From source file:org.xwiki.rendering.xml.internal.serializer.DefaultXMLSerializer.java
private void onEvent(String eventName, Object[] parameters) { String blockName = getBlockName(eventName, "on"); ListenerElement element = this.descriptor.getElements().get(blockName.toLowerCase()); List<Object> elementParameters = parameters != null ? Arrays.asList(parameters) : null; // Remove useless parameters removeDefaultParameters(elementParameters, element); // Put as attributes parameters which are simple enough to not require full XML serialization AttributesImpl attributes = (elementParameters != null && elementParameters.size() > 1) ? createStartAttributes(blockName, Arrays.asList(parameters)) : new AttributesImpl(); // Get proper element name String elementName;/*from w w w .ja v a 2 s. c o m*/ if (isValidBlockElementName(blockName)) { elementName = blockName; } else { elementName = this.configuration.getElementBlock(); } // Print start element startElement(elementName, attributes); // Print complex parameters if (parameters != null && parameters.length == 1 && XMLUtils.isSimpleType(element.getParameters().get(0))) { String value = parameters[0].toString(); try { this.contentHandler.characters(value.toCharArray(), 0, value.length()); } catch (SAXException e) { throw new RuntimeException("Failed to send sax event", e); } } else { printParameters(elementParameters, element); } // Print end element endElement(elementName); }
From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java
/** * {@inheritDoc}/*w ww . j av a 2s . co m*/ */ public void outputContent(final Entity entity, final Entity sideBar, final HttpServletRequest request, final HttpServletResponse res) { if (!isAvailable()) return; if (!(entity instanceof RWikiEntity)) return; try { String skin = ServerConfigurationService.getString("skin.default"); //$NON-NLS-1$ String skinRepo = ServerConfigurationService.getString("skin.repo"); //$NON-NLS-1$ request.setAttribute("sakai.skin.repo", skinRepo); //$NON-NLS-1$ request.setAttribute("sakai.skin", skin); //$NON-NLS-1$ HttpSession s = request.getSession(); PageVisits pageVisits = (PageVisits) s .getAttribute(XSLTEntityHandler.class.getName() + this.getMinorType() + "_visits"); boolean withBreadcrumbs = !"0".equals(request.getParameter(breadCrumbParameter)); if (pageVisits == null) { pageVisits = new PageVisits(); s.setAttribute(XSLTEntityHandler.class.getName() + this.getMinorType() + "_visits", pageVisits); } RWikiEntity rwe = (RWikiEntity) entity; if (!rwe.isContainer()) { RWikiObject rwo = rwe.getRWikiObject(); pageVisits.addPage(rwo.getName()); } setCurrentRequest(request); if (responseHeaders != null) { for (Iterator i = responseHeaders.keySet().iterator(); i.hasNext();) { String name = (String) i.next(); String value = (String) responseHeaders.get(name); res.setHeader(name, value); } } OutputStream out = res.getOutputStream(); ContentHandler opch = getOutputHandler(out); ContentHandler ch = null; if (false) { ch = new DebugContentHandler(opch); } else { ch = opch; } Attributes dummyAttributes = new AttributesImpl(); ch.startDocument(); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITYSERVICE, SchemaNames.EL_NSENTITYSERVICE, dummyAttributes); AttributesImpl propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_REQUEST_PATH_INFO, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_PATH_INFO, "string", request //$NON-NLS-1$ .getPathInfo()); propA.addAttribute("", SchemaNames.ATTR_REQUEST_USER, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_USER, "string", request //$NON-NLS-1$ .getRemoteUser()); propA.addAttribute("", SchemaNames.ATTR_REQUEST_PROTOCOL, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_PROTOCOL, "string", request //$NON-NLS-1$ .getProtocol()); propA.addAttribute("", SchemaNames.ATTR_REQUEST_SERVER_NAME, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_SERVER_NAME, "string", request //$NON-NLS-1$ .getServerName()); propA.addAttribute("", SchemaNames.ATTR_REQUEST_SERVER_PORT, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_SERVER_PORT, "string", String //$NON-NLS-1$ .valueOf(request.getServerPort())); propA.addAttribute("", SchemaNames.ATTR_REQUEST_REQUEST_URL, //$NON-NLS-1$ SchemaNames.ATTR_REQUEST_REQUEST_URL, "string", String //$NON-NLS-1$ .valueOf(request.getRequestURL())); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PROPERTIES, SchemaNames.EL_NSREQUEST_PROPERTIES, propA); addRequestAttributes(ch, request); addRequestParameters(ch, request); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PROPERTIES, SchemaNames.EL_NSREQUEST_PROPERTIES); if (withBreadcrumbs) { ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISITS, SchemaNames.EL_NSPAGEVISITS, dummyAttributes); List<String[]> pv = pageVisits.getPageNames(this.getMinorType()); for (Iterator<String[]> i = pv.iterator(); i.hasNext();) { String[] visit = i.next(); propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_URL, SchemaNames.ATTR_URL, "string", visit[0]); addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISIT, SchemaNames.EL_NSPAGEVISIT, propA, visit[1]); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_PAGEVISITS, SchemaNames.EL_NSPAGEVISITS); } ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITY, SchemaNames.EL_NSENTITY, dummyAttributes); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES, dummyAttributes); ResourceProperties rp = entity.getProperties(); for (Iterator i = rp.getPropertyNames(); i.hasNext();) { Object key = i.next(); String name = String.valueOf(key); String value = String.valueOf(rp.getProperty(name)); propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", name); //$NON-NLS-1$ addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY, propA, value); } propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", "_handler"); //$NON-NLS-1$ //$NON-NLS-2$ addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY, propA, " XSLTEntity Handler"); //$NON-NLS-1$ if (!rwe.isContainer()) { RWikiObject rwo = rwe.getRWikiObject(); propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$ addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY, propA, NameHelper.localizeName(rwo.getName(), rwo.getRealm())); } else { propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$ addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY, propA, entity.getReference()); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES); /* http://jira.sakaiproject.org/browse/SAK-13281 * escapeXML is controlled via config settings */ if (!rwe.isContainer()) { RWikiObject rwo = rwe.getRWikiObject(); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT, SchemaNames.EL_NSRENDEREDCONTENT, dummyAttributes); renderToXML(rwo, ch, withBreadcrumbs, this.escaped); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT, SchemaNames.EL_NSRENDEREDCONTENT); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITY, SchemaNames.EL_NSENTITY); if (sideBar != null && sideBar instanceof RWikiEntity) { RWikiEntity sbrwe = (RWikiEntity) sideBar; RWikiObject sbrwo = sbrwe.getRWikiObject(); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_SIDEBAR, SchemaNames.EL_NSSIDEBAR, dummyAttributes); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES, dummyAttributes); propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", "_title"); //$NON-NLS-1$ //$NON-NLS-2$ addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTY, SchemaNames.EL_NSXMLPROPERTY, propA, NameHelper.localizeName(sbrwo.getName(), sbrwo.getRealm())); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_XMLPROPERTIES, SchemaNames.EL_NSXMLPROPERTIES); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT, SchemaNames.EL_NSRENDEREDCONTENT, dummyAttributes); renderToXML(sbrwo, ch, withBreadcrumbs, this.escaped); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RENDEREDCONTENT, SchemaNames.EL_NSRENDEREDCONTENT); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_SIDEBAR, SchemaNames.EL_NSSIDEBAR); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ENTITYSERVICE, SchemaNames.EL_NSENTITYSERVICE); ch.endDocument(); } catch (Throwable ex) { log.info("Failed to serialize " + ex.getMessage()); //$NON-NLS-1$ ex.printStackTrace(); throw new RuntimeException(Messages.getString("XSLTEntityHandler.68") //$NON-NLS-1$ + ex.getLocalizedMessage(), ex); } finally { setCurrentRequest(null); } }
From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java
/** * Serialises the rendered content of the RWiki Object to SAX * /*from w ww . j ava2 s.com*/ * @param rwo * @param ch * @param withBreadCrumb */ public void renderToXML(RWikiObject rwo, final ContentHandler ch, boolean withBreadCrumb, boolean escapeXML) throws SAXException, IOException { String renderedPage; try { renderedPage = render(rwo, withBreadCrumb); } catch (Exception e) { renderedPage = Messages.getString("XSLTEntityHandler.32") + rwo.getName() //$NON-NLS-1$ + Messages.getString("XSLTEntityHandler.33") + e.getClass() //$NON-NLS-1$ + Messages.getString("XSLTEntityHandler.34") + e.getMessage(); //$NON-NLS-1$ log.info(renderedPage, e); } String contentDigest = DigestHtml.digest(renderedPage); if (contentDigest.length() > 500) { contentDigest = contentDigest.substring(0, 500); } if (renderedPage == null || renderedPage.trim().length() == 0) { renderedPage = Messages.getString("XSLTEntityHandler.35"); //$NON-NLS-1$ } if (contentDigest == null || contentDigest.trim().length() == 0) { contentDigest = Messages.getString("XSLTEntityHandler.36"); //$NON-NLS-1$ } String cdataEscapedRendered = renderedPage.replaceAll("]]>", "]]>]]><![CDATA["); //$NON-NLS-1$ //$NON-NLS-2$ String cdataContentDigest = contentDigest.replaceAll("]]>", "]]>]]><![CDATA["); //$NON-NLS-1$ //$NON-NLS-2$ /* http://jira.sakaiproject.org/browse/SAK-13281 * ensure all page content is escaped or double escaped before it goes into the parser, * if this is not done then the parser will unescape html entities during processing */ renderedPage = "<content><rendered>" //$NON-NLS-1$ + (escapeXML ? StringEscapeUtils.escapeXml(renderedPage) : renderedPage) + "</rendered><rendered-cdata><![CDATA[" + cdataEscapedRendered //$NON-NLS-1$ + "]]></rendered-cdata><contentdigest><![CDATA[" + cdataContentDigest //$NON-NLS-1$ + "]]></contentdigest></content>"; //$NON-NLS-1$ try { parseToSAX(renderedPage, ch); } catch (SAXException ex) { SimpleCoverage.cover("Failed to parse renderedPage from " + rwo.getName()); //$NON-NLS-1$ Attributes dummyAttributes = new AttributesImpl(); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR, SchemaNames.EL_NSERROR, dummyAttributes); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC, SchemaNames.EL_NSERRORDESC, dummyAttributes); String s = Messages.getString("XSLTEntityHandler.46") //$NON-NLS-1$ + ex.getMessage(); ch.characters(s.toCharArray(), 0, s.length()); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC, SchemaNames.EL_NSERRORDESC); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT, SchemaNames.EL_NSRAWCONTENT, dummyAttributes); ch.characters(renderedPage.toCharArray(), 0, renderedPage.length()); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT, SchemaNames.EL_NSRAWCONTENT); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR, SchemaNames.EL_NSERROR); } // SimpleCoverage.cover("Failed to parse ::\n" + renderedPage // + "\n:: from ::\n" + rwo.getContent()); // Attributes dummyAttributes = new AttributesImpl(); // ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR, // SchemaNames.EL_NSERROR, dummyAttributes); // ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC, // SchemaNames.EL_NSERRORDESC, dummyAttributes); // String s = "The Rendered Content did not parse correctly " // + ex.getMessage(); // ch.characters(s.toCharArray(), 0, s.length()); // ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERRORDESC, // SchemaNames.EL_NSERRORDESC); // ch.startElement(SchemaNames.NS_CONTAINER, // SchemaNames.EL_RAWCONTENT, SchemaNames.EL_NSRAWCONTENT, // dummyAttributes); // ch.characters(renderedPage.toCharArray(), 0, renderedPage.length()); // ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_RAWCONTENT, // SchemaNames.EL_NSRAWCONTENT); // ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_ERROR, // SchemaNames.EL_NSERROR); }
From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java
public void addRequestAttributes(ContentHandler ch, HttpServletRequest request) throws Exception { if (!isAvailable()) return;/*w w w. j av a 2s.co m*/ // add the attributes AttributesImpl dummyAttributes = new AttributesImpl(); ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTES, SchemaNames.EL_NSREQUEST_ATTRIBUTES, dummyAttributes); for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object attr = request.getAttribute(name); AttributesImpl propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", name); //$NON-NLS-1$ if (attr instanceof Object[]) { Object[] oattr = (Object[]) attr; ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTE, SchemaNames.EL_NSREQUEST_ATTRIBUTE, propA); for (int i = 0; i < oattr.length; i++) { addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_VALUE, SchemaNames.EL_NSVALUE, dummyAttributes, oattr[i]); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTE, SchemaNames.EL_NSREQUEST_ATTRIBUTE); } else { ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTE, SchemaNames.EL_NSREQUEST_ATTRIBUTE, propA); addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_VALUE, SchemaNames.EL_NSVALUE, dummyAttributes, attr); ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTE, SchemaNames.EL_NSREQUEST_ATTRIBUTE); } } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_ATTRIBUTES, SchemaNames.EL_NSREQUEST_ATTRIBUTES); }
From source file:uk.ac.cam.caret.sakai.rwiki.component.service.impl.XSLTEntityHandler.java
public void addRequestParameters(ContentHandler ch, HttpServletRequest request) throws Exception { if (!isAvailable()) return;/*w w w. ja v a 2 s . com*/ AttributesImpl dummyAttributes = new AttributesImpl(); // add the request parameters ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAMS, SchemaNames.EL_NSREQUEST_PARAMS, dummyAttributes); for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); String[] attr = request.getParameterValues(name); AttributesImpl propA = new AttributesImpl(); propA.addAttribute("", SchemaNames.ATTR_NAME, //$NON-NLS-1$ SchemaNames.ATTR_NAME, "string", name); //$NON-NLS-1$ ch.startElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAM, SchemaNames.EL_NSREQUEST_PARAM, propA); for (int i = 0; i < attr.length; i++) { addElement(ch, SchemaNames.NS_CONTAINER, SchemaNames.EL_VALUE, SchemaNames.EL_NSVALUE, dummyAttributes, attr[i]); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAM, SchemaNames.EL_NSREQUEST_PARAM); } ch.endElement(SchemaNames.NS_CONTAINER, SchemaNames.EL_REQUEST_PARAMS, SchemaNames.EL_REQUEST_PARAMS); }
From source file:uk.ac.ebi.arrayexpress.utils.saxon.FlatFileXMLReader.java
public void parse(InputSource input) throws IOException, SAXException { int headerRows = getIntOptionValue(OPTION_HEADER_ROWS, 0); int page = getIntOptionValue(OPTION_PAGE, 0); int pageSize = getIntOptionValue(OPTION_PAGE_SIZE, -1); Integer sortBy = getIntOptionValue(OPTION_SORT_BY, null); String sortOrder = getStringOptionValue(OPTION_SORT_ORDER, "a"); ContentHandler ch = getContentHandler(); if (null == ch) { return;/* ww w . java 2 s .com*/ } Reader inStream; if (input.getCharacterStream() != null) { inStream = input.getCharacterStream(); } else if (input.getByteStream() != null) { inStream = new InputStreamReader(input.getByteStream()); } else if (input.getSystemId() != null) { URL url = new URL(input.getSystemId()); inStream = new InputStreamReader(url.openStream()); } else { throw new SAXException("Invalid InputSource object"); } CSVReader ffReader = new CSVReader(new BufferedReader(inStream), this.columnDelimiter, this.columnQuoteChar); List<String[]> ff = ffReader.readAll(); int cols = ff.size() > 0 ? ff.get(0).length : 0; // verify that sort by column is with in range of columns // if not then sort will not be performed // else - switch from 1-based to 0-based index if (null != sortBy) { if (sortBy < 1 || sortBy > cols) { sortBy = null; } else { sortBy = sortBy - 1; } } // 1. removes all dodgy rows (that have less columns than the first one) // 2. determines if column to be sorted is numeric ColDataType sortColDataType = ColDataType.INTEGER; int colTypeSkipRows = headerRows; for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext();) { String[] row = iterator.next(); if (row.length != cols || isRowBlank(row)) { iterator.remove(); } else { if (null != sortBy && 0 == colTypeSkipRows && ColDataType.STRING != sortColDataType) { ColDataType dataType = getColDataType(row[sortBy]); // downgrade from int to decimal or string if (ColDataType.INTEGER == sortColDataType && ColDataType.INTEGER != dataType) { sortColDataType = dataType; } // downgrade from decimal to string only if (ColDataType.DECIMAL == sortColDataType && ColDataType.STRING == dataType) { sortColDataType = dataType; } } if (colTypeSkipRows > 0) { colTypeSkipRows--; } } } int rows = ff.size() > 0 ? ff.size() - headerRows : 0; if (-1 == pageSize) { page = 1; pageSize = rows; } ch.startDocument(); AttributesImpl tableAttrs = new AttributesImpl(); tableAttrs.addAttribute(EMPTY_NAMESPACE, "rows", "rows", CDATA_TYPE, String.valueOf(rows)); ch.startElement(EMPTY_NAMESPACE, "table", "table", tableAttrs); for (Iterator<String[]> iterator = ff.iterator(); iterator.hasNext() && headerRows > 0; headerRows--) { String[] row = iterator.next(); outputRow(ch, true, null, row); iterator.remove(); } if (null != sortBy) { Collections.sort(ff, new SortColumnComparator(sortBy, sortOrder, sortColDataType)); } int rowSeq = 1; for (String[] row : ff) { if (rowSeq > (pageSize * (page - 1)) && rowSeq <= (pageSize * page)) { outputRow(ch, false, String.valueOf(rowSeq), row); } ++rowSeq; } ch.endElement(EMPTY_NAMESPACE, "table", "table"); ch.endDocument(); }