List of usage examples for org.jdom2 Namespace getNamespace
public static Namespace getNamespace(final String uri)
Namespace
for the supplied URI, and make it usable as a default namespace, as no prefix is supplied. From source file:ch.kostceco.tools.siardval.validation.module.impl.ValidationHcontentModuleImpl.java
License:Open Source License
@Override public boolean validate(File siardDatei) throws ValidationHcontentException { boolean valid = true; try {/*from w w w .j a va 2 s. c o m*/ /* * Extract the metadata.xml from the temporary work folder and build * a jdom document */ String pathToWorkDir = getConfigurationService().getPathToWorkDir(); File metadataXml = new File(new StringBuilder(pathToWorkDir).append(File.separator).append("header") .append(File.separator).append("metadata.xml").toString()); InputStream fin = new FileInputStream(metadataXml); SAXBuilder builder = new SAXBuilder(); Document document = builder.build(fin); fin.close(); /* * read the document and for each schema and table entry verify * existence in temporary extracted structure */ Namespace ns = Namespace.getNamespace("http://www.bar.admin.ch/xmlns/siard/1.0/metadata.xsd"); // select schema elements and loop List<Element> schemas = document.getRootElement().getChild("schemas", ns).getChildren("schema", ns); for (Element schema : schemas) { Element schemaFolder = schema.getChild("folder", ns); File schemaPath = new File(new StringBuilder(pathToWorkDir).append(File.separator).append("content") .append(File.separator).append(schemaFolder.getText()).toString()); if (schemaPath.isDirectory()) { Element[] tables = schema.getChild("tables", ns).getChildren("table", ns) .toArray(new Element[0]); for (Element table : tables) { Element tableFolder = table.getChild("folder", ns); File tablePath = new File(new StringBuilder(schemaPath.getAbsolutePath()) .append(File.separator).append(tableFolder.getText()).toString()); if (tablePath.isDirectory()) { File tableXml = new File(new StringBuilder(tablePath.getAbsolutePath()) .append(File.separator).append(tableFolder.getText() + ".xml").toString()); File tableXsd = new File(new StringBuilder(tablePath.getAbsolutePath()) .append(File.separator).append(tableFolder.getText() + ".xsd").toString()); if (verifyRowCount(tableXml, tableXsd)) { valid = validate(tableXml, tableXsd) && valid; } } } } } } catch (java.io.IOException ioe) { valid = false; getMessageService().logError(getTextResourceService().getText(MESSAGE_MODULE_H) + getTextResourceService().getText(MESSAGE_DASHES) + "IOException " + ioe.getMessage()); } catch (JDOMException e) { valid = false; getMessageService().logError(getTextResourceService().getText(MESSAGE_MODULE_H) + getTextResourceService().getText(MESSAGE_DASHES) + "JDOMException " + e.getMessage()); } catch (SAXException e) { valid = false; getMessageService().logError(getTextResourceService().getText(MESSAGE_MODULE_H) + getTextResourceService().getText(MESSAGE_DASHES) + "SAXException " + e.getMessage()); } return valid; }
From source file:com.hack23.cia.service.external.common.impl.XmlAgentImpl.java
License:Apache License
/** * Sets the name space on xml stream./*from w w w.j a v a2 s . c om*/ * * @param in * the in * @param nameSpace * the name space * @return the source * @throws Exception * the exception */ private static Source setNameSpaceOnXmlStream(final InputStream in, final String nameSpace) throws Exception { final SAXBuilder sb = new SAXBuilder(new XMLReaderSAX2Factory(false)); final Document doc = sb.build(in); doc.getRootElement().setNamespace(Namespace.getNamespace(nameSpace)); return new JDOMSource(doc); }
From source file:com.init.octo.schema.XSDCache.java
License:Open Source License
public void pushNamespaces(String schemaFileName) throws Exception { Namespace namespace = null;//from www .j a v a2 s .co m String schemaElement; namespaceIdx++; namespaceCache[namespaceIdx] = new HashMap<String, String>(); FileInputStream fis = null; try { byte[] buf = new byte[1024]; StringBuffer str = new StringBuffer(); fis = new FileInputStream(schemaFileName); int schemaStart = -1; int schemaEnd = -1; int buffersRead = 0; while (fis.read(buf, 0, buf.length) > 0 && schemaEnd == -1) { String tmp = new String(buf); str.append(tmp); for (int i = 0; i < tmp.length(); i++) { if (schemaStart == -1) { if (tmp.charAt(i) == '<') { String[] elementSplit = tmp.substring(i + 1).split("[\\s:]", 3); if ("schema".compareToIgnoreCase(elementSplit[0]) == 0 || "schema".compareToIgnoreCase(elementSplit[1]) == 0) { schemaStart = i + (buffersRead * buf.length); } } } else { if (tmp.charAt(i) == '>') { schemaEnd = i + (buffersRead * buf.length); break; } } } buffersRead++; } schemaElement = str.substring(schemaStart, schemaEnd); } catch (Exception ex) { log.error("Exception caching schema namespaces [" + schemaFileName + "]", ex); throw ex; } finally { if (null != fis) { fis.close(); } } String[] splitSchemaElement = schemaElement.split("[\\s]"); boolean cache = false; for (int i = 0; i < splitSchemaElement.length; i++) { cache = false; if (splitSchemaElement[i].startsWith("targetNamespace")) { String[] tmp = splitSchemaElement[i].split("\""); namespace = Namespace.getNamespace(tmp[1]); cache = true; } else if (splitSchemaElement[i].startsWith("xmlns")) { String[] tmp = splitSchemaElement[i].split("[\"=]"); String[] pre = tmp[0].split(":"); namespace = Namespace.getNamespace((pre.length < 2) ? "" : pre[1], tmp[2]); cache = true; } if (cache) { cacheNamespace(namespace); } } }
From source file:com.izforge.izpack.util.xmlmerge.factory.AttributeOperationFactory.java
License:Open Source License
/** * Creates a new AttributeOperationFactory. * * @param defaultOperation The factory's default operation * @param resolver The factory's operation resolver * @param keyword The name of the attribute representing the factory's operation * @param namespace The namespace describing the operations to apply *///from w w w .ja va 2 s . c o m public AttributeOperationFactory(Operation defaultOperation, OperationResolver resolver, String keyword, String namespace) { this.m_defaultOperation = defaultOperation; this.m_keyword = keyword; this.m_resolver = resolver; this.m_namespace = Namespace.getNamespace(namespace); }
From source file:com.izforge.izpack.util.xmlmerge.mapper.NamespaceFilterMapper.java
License:Open Source License
/** * Creates a new NamespaceFilterMapper./*from w ww . j a va2 s . com*/ * * @param filteredNamespace String representing the namespace defining the elements and * attributes to be filtered out */ public NamespaceFilterMapper(String filteredNamespace) { this.m_namespace = Namespace.getNamespace(filteredNamespace); }
From source file:com.kixeye.kixmpp.KixmppWebSocketCodec.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception { WebSocketFrame frame = null;/*w w w. ja va 2 s . com*/ if (msg instanceof Element) { Element element = (Element) msg; if (element.getNamespace() == null || element.getNamespace() == Namespace.NO_NAMESPACE) { if ("stream".equals(element.getNamespacePrefix())) { element.setNamespace(Namespace.getNamespace("http://etherx.jabber.org/streams")); } else { element.setNamespace(Namespace.getNamespace("jabber:client")); IteratorIterable<Content> descendants = element.getDescendants(); while (descendants.hasNext()) { Content content = descendants.next(); if (content instanceof Element) { Element descendantElement = (Element) content; if (descendantElement.getNamespace() == null || descendantElement.getNamespace() == Namespace.NO_NAMESPACE) { descendantElement.setNamespace(element.getNamespace()); } } } } } ByteBuf binaryData = ctx.alloc().buffer(); new XMLOutputter().output((Element) msg, new ByteBufOutputStream(binaryData)); frame = new TextWebSocketFrame(binaryData); } else if (msg instanceof KixmppStreamStart) { KixmppStreamStart streamStart = (KixmppStreamStart) msg; StringWriter writer = new StringWriter(); if (streamStart.doesIncludeXmlHeader()) { writer.append("<?xml version='1.0' encoding='UTF-8'?>"); } writer.append("<stream:stream "); if (streamStart.getId() != null) { writer.append(String.format("id=\"%s\" ", streamStart.getId())); } if (streamStart.getFrom() != null) { writer.append(String.format("from=\"%s\" ", streamStart.getFrom().getFullJid())); } if (streamStart.getTo() != null) { writer.append(String.format("to=\"%s\" ", streamStart.getTo())); } writer.append( "version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"); frame = new TextWebSocketFrame(writer.toString()); } else if (msg instanceof KixmppStreamEnd) { frame = new TextWebSocketFrame("</stream:stream>"); } else if (msg instanceof String) { frame = new TextWebSocketFrame((String) msg); } else if (msg instanceof ByteBuf) { frame = new TextWebSocketFrame((ByteBuf) msg); } if (frame != null) { if (logger.isDebugEnabled()) { logger.debug("Sending: [{}]", frame.content().toString(StandardCharsets.UTF_8)); } out.add(frame); } }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java
License:Apache License
/** * A user requests to join the room./* w ww. java 2s . c o m*/ * * @param channel * @param nickname * @param mucStanza */ public void join(final Channel channel, String nickname, Element mucStanza) { KixmppJid jid = channel.attr(BindKixmppServerModule.JID).get(); if (settings.isOpen() && !jidRoles.containsKey(jid.withoutResource())) { addUser(jid, nickname, MucRole.Participant, MucAffiliation.Member); } verifyMembership(jid.withoutResource()); checkForNicknameInUse(nickname, jid); User user = usersByNickname.get(nickname); boolean existingUser = true; if (user == null) { user = new User(nickname, jid.withoutResource()); usersByNickname.put(nickname, user); MucRoomEventHandler handler = service.getServer().getMucRoomEventHandler(); if (handler != null) { handler.userAdded(this, user); } existingUser = false; } Client client = user.addClient(new Client(jid, nickname, channel)); // xep-0045 7.2.3 begin // self presence KixmppJid fromRoomJid = roomJid.withResource(nickname); channel.writeAndFlush(createPresence(fromRoomJid, jid, MucRole.Participant, null)); if (settings.isPresenceEnabled() && !existingUser) { // Send presence from existing occupants to new occupant sendExistingOccupantsPresenceToNewOccupant(user, channel); // Send new occupant's presence to all occupants broadcastPresence(fromRoomJid, MucRole.Participant, null); } // xep-0045 7.2.3 end if (settings.getSubject() != null) { Element message = new Element("message"); message.setAttribute("id", UUID.randomUUID().toString()); message.setAttribute("from", roomJid.withResource(nickname).toString()); message.setAttribute("to", channel.attr(BindKixmppServerModule.JID).get().toString()); message.setAttribute("type", "groupchat"); message.addContent(new Element("subject").setText(settings.getSubject())); channel.writeAndFlush(message); } if (mucStanza != null) { Element history = mucStanza.getChild("history", mucStanza.getNamespace()); if (history != null) { MucHistoryProvider historyProvider = mucModule.getHistoryProvider(); if (historyProvider != null) { Integer maxChars = null; Integer maxStanzas = null; Integer seconds = null; String parsableString = history.getAttributeValue("maxchars"); if (parsableString != null) { try { maxChars = Integer.parseInt(parsableString); } catch (Exception e) { } } parsableString = history.getAttributeValue("maxstanzas"); if (parsableString != null) { try { maxStanzas = Integer.parseInt(parsableString); } catch (Exception e) { } } parsableString = history.getAttributeValue("seconds"); if (parsableString != null) { try { seconds = Integer.parseInt(parsableString); } catch (Exception e) { } } String since = history.getAttributeValue("since"); historyProvider.getHistory(roomJid, user.getBareJid(), maxChars, maxStanzas, seconds, since) .addListener(new GenericFutureListener<Future<List<MucHistory>>>() { @Override public void operationComplete(Future<List<MucHistory>> future) throws Exception { if (future.isSuccess()) { List<MucHistory> historyItems = future.get(); if (historyItems != null) { for (MucHistory historyItem : historyItems) { Element message = new Element("message") .setAttribute("id", UUID.randomUUID().toString()) .setAttribute("from", roomJid.withResource(historyItem.getNickname()) .toString()) .setAttribute("to", channel.attr(BindKixmppServerModule.JID).get() .toString()) .setAttribute("type", "groupchat"); message.addContent( new Element("body").setText(historyItem.getBody())); Element addresses = new Element("addresses", Namespace .getNamespace("http://jabber.org/protocol/address")); addresses .addContent(new Element("address", addresses.getNamespace()) .setAttribute("type", "ofrom").setAttribute("jid", historyItem.getFrom().toString())); message.addContent(addresses); message.addContent(new Element("delay", Namespace.getNamespace("urn:xmpp:delay")) .setAttribute("from", roomJid.toString()) .setAttribute("stamp", XmppDateUtils .format(historyItem.getTimestamp()))); channel.write(message); } channel.flush(); } } } }); } } } channel.closeFuture().addListener(new CloseChannelListener(client)); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java
License:Apache License
private Element createPresence(KixmppJid from, KixmppJid to, MucRole role, String type) { Element presence = new Element("presence"); presence.setAttribute("id", UUID.randomUUID().toString()); presence.setAttribute("from", from.toString()); presence.setAttribute("to", to.toString()); Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user")); if (type != null) { presence.setAttribute("type", type); }// w w w . j a va 2s .c om Element item = new Element("item", Namespace.getNamespace("http://jabber.org/protocol/muc#user")); item.setAttribute("affiliation", "member"); switch (role) { case Participant: item.setAttribute("role", "participant"); break; case Moderator: item.setAttribute("role", "moderator"); break; case Visitor: item.setAttribute("role", "visitor"); break; } x.addContent(item); presence.addContent(x); return presence; }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java
License:Apache License
/** * Sends an direct invitation to a user. Note: The smack client does not recognize this as a valid room invite. *//*w ww . ja v a 2 s . c o m*/ public void sendDirectInvite(KixmppJid from, Channel userChannelToInvite, String reason) { Element message = new Element("message"); message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid()); if (from != null) { message.setAttribute("from", from.getFullJid()); } Element x = new Element("x", Namespace.getNamespace("jabber:x:conference")); x.setAttribute("jid", roomJid.getFullJid()); if (reason != null) { x.setAttribute("reason", reason); } message.addContent(x); userChannelToInvite.writeAndFlush(message); }
From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java
License:Apache License
/** * Sends a mediated invitation to a user. *//* ww w.j a v a 2 s . c om*/ public void sendMediatedInvite(KixmppJid from, Channel userChannelToInvite, String reason) { Element invite = new Element("invite"); invite.setAttribute("from", from.getFullJid()); if (reason != null) { Element el = new Element("reason"); el.setText(reason); invite.addContent(el); } Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user")); x.addContent(invite); Element message = new Element("message"); message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid()); message.setAttribute("from", roomJid.getFullJid()); message.addContent(x); userChannelToInvite.writeAndFlush(message); }