List of usage examples for org.xml.sax.helpers AttributesImpl AttributesImpl
public AttributesImpl()
From source file:net.ontopia.persistence.rdbms.DatabaseProjectReader.java
public static void saveProject(Project project, ContentHandler dh) throws SAXException { AttributesImpl atts = new AttributesImpl(); dh.startDocument();// w w w.ja v a 2 s . c o m dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "dbschema", EMPTY_ATTR_LIST); Iterator<String> platforms = project.getDataTypePlatforms().iterator(); if (platforms.hasNext()) { while (platforms.hasNext()) { String platform = platforms.next(); atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PLATFORM, CDATA, platform); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatypes", atts); Iterator<DataType> datatypes = project.getDataTypes(platform).iterator(); while (datatypes.hasNext()) { // Platform datatypes DataType datatype = datatypes.next(); atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, (datatype.getName())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, TYPE, CDATA, (datatype.getType())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, SIZE, CDATA, (datatype.getSize() == null ? "" : datatype.getSize())); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "class", CDATA, (datatype.isVariable() ? "variable" : "constant")); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatype", atts); // Datatype properties Iterator<String> properties = datatype.getProperties().iterator(); while (properties.hasNext()) { String name = properties.next(); String value = datatype.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatype"); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "datatypes"); } Iterator<Table> tables = project.getTables().iterator(); while (tables.hasNext()) { Table table = tables.next(); // Table attributes atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, table.getName()); if (table.getShortName() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "short", CDATA, table.getShortName()); if (table.getPrimaryKeys() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "pks", CDATA, StringUtils.join(table.getPrimaryKeys(), " ")); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "table", atts); // Table properties Iterator<String> properties = table.getProperties().iterator(); while (properties.hasNext()) { String name = properties.next(); String value = table.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } Iterator<Column> columns = table.getColumns().iterator(); while (columns.hasNext()) { Column column = columns.next(); // Column attributes atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, column.getName()); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, TYPE, CDATA, column.getType()); if (column.isReference()) { atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "reftab", CDATA, column.getReferencedTable()); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "refcol", CDATA, column.getReferencedColumn()); } if (column.getSize() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, SIZE, CDATA, column.getName()); if (column.isNullable()) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "null", CDATA, "yes"); if (column.getDefault() != null) atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "default", CDATA, column.getDefault()); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "column", atts); // Column properties Iterator<String> properties2 = column.getProperties().iterator(); while (properties2.hasNext()) { String name = properties2.next(); String value = column.getProperty(name); if (value != null) { atts.clear(); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, NAME, CDATA, name); atts.addAttribute(EMPTY_NAMESPACE, EMPTY_LOCALNAME, VALUE, CDATA, value); dh.startElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY, atts); dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, PROPERTY); } } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "column"); } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "table"); } dh.endElement(EMPTY_NAMESPACE, EMPTY_LOCALNAME, "dbschema"); dh.endDocument(); }
From source file:it.polito.tellmefirst.web.rest.interfaces.EpubInterface.java
private String produceXML(ArrayList<String[]> topics) throws TMFOutputException { LOG.debug("[produceXML] - BEGIN"); String xml;/* ww w . j a v a 2 s . c o m*/ try { ByteArrayOutputStream out = new ByteArrayOutputStream(); TransformerHandler hd = initXMLDoc(out); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", "service", "", "ClassifyEpub"); hd.startElement("", "", "Classification", atts); int i = 0; for (String[] topic : topics) { if (i == 0) { atts.clear(); hd.startElement("", "", "Resources", atts); } atts.addAttribute("", "", "uri", "", topic[0]); atts.addAttribute("", "", "label", "", topic[1]); atts.addAttribute("", "", "title", "", topic[2]); atts.addAttribute("", "", "score", "", topic[3]); atts.addAttribute("", "", "mergedTypes", "", topic[4]); atts.addAttribute("", "", "image", "", topic[5]); hd.startElement("", "", "Resource", atts); hd.endElement("", "", "Resource"); i++; } if (i > 0) hd.endElement("", "", "Resources"); hd.endElement("", "", "Classification"); hd.endDocument(); xml = out.toString("utf-8"); System.out.println(xml); } catch (Exception e) { throw new TMFOutputException("Error creating XML output.", e); } LOG.debug("[produceXML] - END"); return xml; }
From source file:com.smartitengineering.exim.impl.xml.GenericDataTypeExporter.java
public void exportElement(AssociationType type, Object object, TransformerHandler handler) throws IOException { if (type == null || object == null || handler == null) { throw new IOException("All parameters are required!"); }// w w w .j av a 2 s . c o m final String elementName = type.getSimpleName(); final String value; switch (type) { case TYPE_DATE: value = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format((Date) object); break; case TYPE_STRING: case TYPE_INTEGER: case TYPE_LONG: case TYPE_FLOAT: case TYPE_DOUBLE: case TYPE_BOOLEAN: default: value = object == null ? null : object.toString(); break; } try { AttributesImpl atts = new AttributesImpl(); handler.startElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName, atts); if (AssociationType.TYPE_STRING.equals(type)) { handler.startCDATA(); } final char[] chars; if (StringUtils.isBlank(value)) { chars = "".toCharArray(); } else { chars = value.toCharArray(); } handler.characters(chars, 0, chars.length); if (AssociationType.TYPE_STRING.equals(type)) { handler.endCDATA(); } handler.endElement(EXIM_COLLECN_URI, EXIM_COLLECTION_NS, elementName); } catch (Exception exception) { throw new IOException(exception); } }
From source file:it.polito.tellmefirst.web.rest.interfaces.ClassifyInterface.java
private String produceXML(List<String[]> topics) throws TMFOutputException { LOG.debug("[produceXML] - BEGIN"); String xml;//from ww w. ja va2 s. c o m try { ByteArrayOutputStream out = new ByteArrayOutputStream(); TransformerHandler hd = initXMLDoc(out); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", "service", "", "Classify"); hd.startElement("", "", "Classification", atts); int i = 0; for (String[] topic : topics) { if (i == 0) { atts.clear(); hd.startElement("", "", "Resources", atts); } atts.addAttribute("", "", "uri", "", topic[0]); atts.addAttribute("", "", "label", "", topic[1]); atts.addAttribute("", "", "title", "", topic[2]); atts.addAttribute("", "", "score", "", topic[3]); atts.addAttribute("", "", "mergedTypes", "", topic[4]); atts.addAttribute("", "", "image", "", ""); // We should remove this parameter in favour of Wikimedia/Wikidata API hd.startElement("", "", "Resource", atts); hd.endElement("", "", "Resource"); i++; } if (i > 0) hd.endElement("", "", "Resources"); hd.endElement("", "", "Classification"); hd.endDocument(); xml = out.toString("utf-8"); System.out.println(xml); } catch (Exception e) { throw new TMFOutputException("Error creating XML output.", e); } LOG.debug("[produceXML] - END"); return xml; }
From source file:org.javelin.sws.ext.bind.SoapEncodingMarshaller.java
@Override public void marshal(Object graph, Result result) throws IOException, XmlMappingException { try {/* ww w . jav a2 s . c o m*/ ContentHandler contentHandler = ((SAXResult) result).getHandler(); contentHandler.startElement("", "hello", "hello", new AttributesImpl()); contentHandler.characters(((String) ((Object[]) graph)[0]).toCharArray(), 0, ((String) ((Object[]) graph)[0]).toCharArray().length); contentHandler.endElement("", "hello", "hello"); } catch (SAXException e) { throw new MarshallingFailureException(e.getMessage(), e); } }
From source file:it.polito.tellmefirst.web.rest.interfaces.EpubChaptersInterface.java
private String produceXML(HashMap<String, List<String[]>> chapters) throws TMFOutputException { LOG.debug("[produceXML] - BEGIN"); String xml;/*from w w w .jav a 2 s . c o m*/ try { ByteArrayOutputStream out = new ByteArrayOutputStream(); TransformerHandler hd = initXMLDoc(out); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", "service", "", "ClassifyEpubChapters"); hd.startElement("", "", "Classification", atts); int i = 0; Set set = chapters.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry me = (Map.Entry) iterator.next(); atts.clear(); if (i == 0) { hd.startElement("", "", "Chapters", atts); } atts.addAttribute("", "", "toc", "", me.getKey().toString()); hd.startElement("", "", "Chapter", atts); atts.clear(); ArrayList<String[]> topics = (ArrayList<String[]>) me.getValue(); int j = 0; for (String[] topic : topics) { atts.clear(); if (j == 0) { hd.startElement("", "", "Resources", atts); } atts.addAttribute("", "", "uri", "", topic[0]); atts.addAttribute("", "", "label", "", topic[1]); atts.addAttribute("", "", "title", "", topic[2]); atts.addAttribute("", "", "score", "", topic[3]); atts.addAttribute("", "", "mergedTypes", "", topic[4]); atts.addAttribute("", "", "image", "", topic[5]); hd.startElement("", "", "Resource", atts); hd.endElement("", "", "Resource"); j++; } if (j > 0) hd.endElement("", "", "Resources"); hd.endElement("", "", "Chapter"); i++; } if (i > 0) hd.endElement("", "", "Chapters"); hd.endElement("", "", "Classification"); hd.endDocument(); xml = out.toString("utf-8"); System.out.println(xml); } catch (Exception e) { throw new TMFOutputException("Error creating XML output.", e); } LOG.debug("[produceXML] - END"); return xml; }
From source file:hd3gtv.mydmam.db.BackupDbElasticsearch.java
BackupDbElasticsearch(File outfile, String index_name, ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mapping, ImmutableOpenMap<String, Settings> settings) throws Exception { quotedprintablecodec = new QuotedPrintableCodec("UTF-8"); GsonBuilder g_builder = new GsonBuilder(); g_builder.disableHtmlEscaping();//w w w . j a v a 2 s.com if (BackupDb.mode_debug) { g_builder.setPrettyPrinting(); } gson = g_builder.create(); /** * Preparation */ fileoutputstream = new FileOutputStream(outfile); OutputFormat of = new OutputFormat(); of.setMethod("xml"); of.setEncoding("UTF-8"); of.setVersion("1.0"); of.setIndenting(BackupDb.mode_debug); if (BackupDb.mode_debug) { of.setIndent(2); } serializer = new XMLSerializer(fileoutputstream, of); content = serializer.asContentHandler(); content.startDocument(); /** * Headers */ AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", "name", "CDATA", index_name); atts.addAttribute("", "", "created", "CDATA", String.valueOf(System.currentTimeMillis())); if (BackupDb.mode_debug) { atts.addAttribute("", "", "created_date", "CDATA", (new Date()).toString()); } content.startElement("", "", "index", atts); /** * Import configuration * ES Mapping */ String jo_mapping; ImmutableOpenMap<String, MappingMetaData> mapping_value; for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> mapping_cursor : mapping) { mapping_value = mapping_cursor.value; for (ObjectObjectCursor<String, MappingMetaData> mapping_value_cursor : mapping_value) { atts.clear(); atts.addAttribute("", "", "name", "CDATA", mapping_value_cursor.key); content.startElement("", "", "mapping", atts); jo_mapping = gson.toJson(mapping_value_cursor.value.getSourceAsMap()); if (BackupDb.mode_debug) { serializer.comment(jo_mapping); } jo_mapping = new String(quotedprintablecodec.encode(jo_mapping)); content.characters(jo_mapping.toCharArray(), 0, jo_mapping.length()); content.endElement("", "", "mapping"); } } /** * ES settings */ for (ObjectObjectCursor<String, Settings> settings_cursor : settings) { atts.clear(); content.startElement("", "", "settings", atts); jo_mapping = gson.toJson(settings_cursor.value.getAsMap()); if (BackupDb.mode_debug) { serializer.comment(jo_mapping); } jo_mapping = new String(quotedprintablecodec.encode(jo_mapping)); content.characters(jo_mapping.toCharArray(), 0, jo_mapping.length()); content.endElement("", "", "settings"); } }
From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOptionElement.java
/** * JavaScript constructor.// w w w .j a va 2 s .c o m * @param newText the text * @param newValue the value * @param defaultSelected Whether the option is initially selected * @param selected the current selection state of the option */ @JsxConstructor({ @WebBrowser(CHROME), @WebBrowser(FF), @WebBrowser(EDGE) }) public void jsConstructor(final String newText, final String newValue, final boolean defaultSelected, final boolean selected) { final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage(); AttributesImpl attributes = null; if (defaultSelected) { attributes = new AttributesImpl(); attributes.addAttribute(null, "selected", "selected", null, "selected"); } final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME).createElement(page, HtmlOption.TAG_NAME, attributes); htmlOption.setSelected(selected); setDomNode(htmlOption); if (!"undefined".equals(newText)) { htmlOption.appendChild(new DomText(page, newText)); htmlOption.setLabelAttribute(newText); } if (!"undefined".equals(newValue)) { htmlOption.setValueAttribute(newValue); } }
From source file:com.mirth.connect.model.converters.NCPDPReader.java
@Override public void parse(InputSource input) throws SAXException, IOException { // convert the InputSource to a String and trim the whitespace String message = IOUtils.toString(input.getCharacterStream()).trim(); ContentHandler contentHandler = getContentHandler(); contentHandler.startDocument();/*from w ww . j a va 2s .co m*/ if ((message == null) || (message.length() < 3)) { throw new SAXException("Unable to parse, message is null or too short: " + message); } // process header String header = parseHeader(message, contentHandler); // process body int groupDelimeterIndex = message.indexOf(groupDelimeter); int segmentDelimeterIndex = message.indexOf(segmentDelimeter); int bodyIndex = 0; if ((groupDelimeterIndex == -1) || (segmentDelimeterIndex < groupDelimeterIndex)) { bodyIndex = segmentDelimeterIndex; } else { bodyIndex = groupDelimeterIndex; } String body = message.substring(bodyIndex, message.length()); boolean hasMoreSegments = true; boolean inGroup = false; boolean firstTransaction = true; int groupCounter = 0; while (hasMoreSegments) { // get next segment or group groupDelimeterIndex = body.indexOf(groupDelimeter); segmentDelimeterIndex = body.indexOf(segmentDelimeter); if ((groupDelimeterIndex > -1) && (groupDelimeterIndex < segmentDelimeterIndex)) { // case: next part is a group // process last segment before group parseSegment(body.substring(0, groupDelimeterIndex), contentHandler); if (inGroup) { contentHandler.endElement("", "TRANSACTION", ""); } if (firstTransaction) { firstTransaction = false; contentHandler.startElement("", "TRANSACTIONS", "", null); } // process a group AttributesImpl attr = new AttributesImpl(); attr.addAttribute("", "counter", "counter", "", Integer.toString(++groupCounter)); contentHandler.startElement("", "TRANSACTION", "", attr); inGroup = true; } else if (groupDelimeterIndex == -1 && segmentDelimeterIndex == -1) { // case: last segment parseSegment(body, contentHandler); hasMoreSegments = false; } else { // case: next part is a segment String segment = body.substring(0, segmentDelimeterIndex); parseSegment(segment, contentHandler); } // remove processed segment from message body body = body.substring(segmentDelimeterIndex + segmentDelimeter.length()); } // end group if we have started one if (inGroup) { contentHandler.endElement("", "TRANSACTION", ""); contentHandler.endElement("", "TRANSACTIONS", ""); } contentHandler.endElement("", header, ""); contentHandler.endDocument(); }
From source file:IOUtils.java
/** * Checks if the used Trax implementation correctly handles namespaces set using * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes. * <p>// w ww. ja va 2s . c o m * The check consists in sending SAX events representing a minimal namespaced document * with namespaces defined only with calls to <code>startPrefixMapping</code> (no * xmlns:xxx attributes) and check if they are present in the resulting text. */ protected static boolean needsNamespacesAsAttributes(Properties format) throws TransformerException, SAXException { // Serialize a minimal document to check how namespaces are handled. final StringWriter writer = new StringWriter(); final String uri = "namespaceuri"; final String prefix = "nsp"; final String check = "xmlns:" + prefix + "='" + uri + "'"; final TransformerHandler handler = FACTORY.newTransformerHandler(); handler.getTransformer().setOutputProperties(format); handler.setResult(new StreamResult(writer)); // Output a single element handler.startDocument(); handler.startPrefixMapping(prefix, uri); handler.startElement(uri, "element", "element", new AttributesImpl()); handler.endElement(uri, "element", "element"); handler.endPrefixMapping(prefix); handler.endDocument(); final String text = writer.toString(); // Check if the namespace is there (replace " by ' to be sure of what we search in) boolean needsIt = (text.replace('"', '\'').indexOf(check) == -1); return needsIt; }