Return-Path: X-Original-To: apmail-chemistry-commits-archive@www.apache.org Delivered-To: apmail-chemistry-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7308CDE00 for ; Tue, 28 May 2013 07:25:01 +0000 (UTC) Received: (qmail 86684 invoked by uid 500); 28 May 2013 07:24:58 -0000 Delivered-To: apmail-chemistry-commits-archive@chemistry.apache.org Received: (qmail 85783 invoked by uid 500); 28 May 2013 07:24:46 -0000 Mailing-List: contact commits-help@chemistry.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@chemistry.apache.org Delivered-To: mailing list commits@chemistry.apache.org Received: (qmail 85212 invoked by uid 99); 28 May 2013 07:24:41 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 May 2013 07:24:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 May 2013 07:24:11 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id C3C722388C93 for ; Tue, 28 May 2013 07:22:51 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r863410 [11/39] - in /websites/staging/chemistry/trunk/content: ./ java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/ java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/css/ java/0.9.0/... Date: Tue, 28 May 2013 07:22:44 -0000 To: commits@chemistry.apache.org From: buildbot@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130528072251.C3C722388C93@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.html ============================================================================== --- websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.html (added) +++ websites/staging/chemistry/trunk/content/java/0.9.0/maven/chemistry-opencmis-server/chemistry-opencmis-server-bindings/xref/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.html Tue May 28 07:22:41 2013 @@ -0,0 +1,619 @@ + + + + +AtomEntryParser xref + + + +
+
+1   /*
+2    * Licensed to the Apache Software Foundation (ASF) under one
+3    * or more contributor license agreements.  See the NOTICE file
+4    * distributed with this work for additional information
+5    * regarding copyright ownership.  The ASF licenses this file
+6    * to you under the Apache License, Version 2.0 (the
+7    * "License"); you may not use this file except in compliance
+8    * with the License.  You may obtain a copy of the License at
+9    *
+10   * http://www.apache.org/licenses/LICENSE-2.0
+11   *
+12   * Unless required by applicable law or agreed to in writing,
+13   * software distributed under the License is distributed on an
+14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+15   * KIND, either express or implied.  See the License for the
+16   * specific language governing permissions and limitations
+17   * under the License.
+18   */
+19  package org.apache.chemistry.opencmis.server.impl.atompub;
+20  
+21  import java.io.ByteArrayInputStream;
+22  import java.io.ByteArrayOutputStream;
+23  import java.io.InputStream;
+24  import java.math.BigInteger;
+25  import java.util.Iterator;
+26  import java.util.List;
+27  import java.util.Locale;
+28  import java.util.Map;
+29  
+30  import javax.xml.namespace.QName;
+31  import javax.xml.stream.XMLOutputFactory;
+32  import javax.xml.stream.XMLStreamReader;
+33  import javax.xml.stream.XMLStreamWriter;
+34  
+35  import org.apache.chemistry.opencmis.commons.PropertyIds;
+36  import org.apache.chemistry.opencmis.commons.data.Acl;
+37  import org.apache.chemistry.opencmis.commons.data.ContentStream;
+38  import org.apache.chemistry.opencmis.commons.data.ObjectData;
+39  import org.apache.chemistry.opencmis.commons.data.Properties;
+40  import org.apache.chemistry.opencmis.commons.data.PropertyData;
+41  import org.apache.chemistry.opencmis.commons.data.PropertyId;
+42  import org.apache.chemistry.opencmis.commons.data.PropertyString;
+43  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
+44  import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+45  import org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException;
+46  import org.apache.chemistry.opencmis.commons.impl.Base64;
+47  import org.apache.chemistry.opencmis.commons.impl.XMLConstants;
+48  import org.apache.chemistry.opencmis.commons.impl.XMLConstraints;
+49  import org.apache.chemistry.opencmis.commons.impl.XMLConverter;
+50  import org.apache.chemistry.opencmis.commons.impl.XMLUtils;
+51  import org.apache.chemistry.opencmis.commons.impl.dataobjects.BulkUpdateImpl;
+52  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
+53  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertiesImpl;
+54  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringImpl;
+55  import org.apache.chemistry.opencmis.server.shared.CappedInputStream;
+56  import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream;
+57  import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
+58  
+59  /**
+60   * Parser for Atom Entries.
+61   */
+62  public class AtomEntryParser {
+63  
+64      private static final long MAX_STREAM_LENGTH = 10 * 1024 * 1024;
+65  
+66      private static final String TAG_ENTRY = "entry";
+67      private static final String TAG_TITLE = "title";
+68      private static final String TAG_OBJECT = "object";
+69      private static final String TAG_CONTENT = "content";
+70      private static final String TAG_BASE64 = "base64";
+71      private static final String TAG_MEDIATYPE = "mediatype";
+72      private static final String TAG_FILENAME = "filename";
+73      private static final String TAG_TYPE = "type";
+74      private static final String TAG_BULK_UPDATE = "bulkUpdate";
+75  
+76      private static final String ATTR_SRC = "src";
+77      private static final String ATTR_TYPE = "type";
+78  
+79      protected boolean ignoreAtomContentSrc;
+80  
+81      private CappedInputStream cappedStream;
+82  
+83      private final ThresholdOutputStreamFactory streamFactory;
+84  
+85      private ObjectData object;
+86      private ContentStreamImpl atomContentStream;
+87      private ContentStreamImpl cmisContentStream;
+88      private TypeDefinition typeDef;
+89      private BulkUpdateImpl bulkUpdate;
+90  
+91      /**
+92       * Constructor.
+93       */
+94      public AtomEntryParser(ThresholdOutputStreamFactory streamFactory) {
+95          this.streamFactory = streamFactory;
+96      }
+97  
+98      /**
+99       * Constructor that immediately parses the given stream.
+100      */
+101     public AtomEntryParser(InputStream stream, ThresholdOutputStreamFactory streamFactory) throws Exception {
+102         this(streamFactory);
+103         parse(stream);
+104     }
+105 
+106     /**
+107      * Sets the flag controlling whether atom content src (external content) is
+108      * ignored. This flag is false by default (not ignored).
+109      */
+110     public void setIgnoreAtomContentSrc(boolean ignoreAtomContentSrc) {
+111         this.ignoreAtomContentSrc = ignoreAtomContentSrc;
+112     }
+113 
+114     /**
+115      * Returns the object.
+116      */
+117     public ObjectData getObject() {
+118         return object;
+119     }
+120 
+121     /**
+122      * Returns the properties of the object.
+123      */
+124     public Properties getProperties() {
+125         return (object == null ? null : object.getProperties());
+126     }
+127 
+128     /**
+129      * Returns the Id of the object.
+130      */
+131     public String getId() {
+132         Properties properties = getProperties();
+133         if (properties == null) {
+134             return null;
+135         }
+136 
+137         Map<String, PropertyData<?>> propertiesMap = properties.getProperties();
+138         if (propertiesMap == null) {
+139             return null;
+140         }
+141 
+142         PropertyData<?> property = propertiesMap.get(PropertyIds.OBJECT_ID);
+143         if (property instanceof PropertyId) {
+144             return ((PropertyId) property).getFirstValue();
+145         }
+146 
+147         return null;
+148     }
+149 
+150     /**
+151      * Returns the ACL of the object.
+152      */
+153     public Acl getAcl() {
+154         return (object == null ? null : object.getAcl());
+155     }
+156 
+157     /**
+158      * Returns the policy id list of the object.
+159      */
+160     public List<String> getPolicyIds() {
+161         if ((object == null) || (object.getPolicyIds() == null)) {
+162             return null;
+163         }
+164 
+165         return object.getPolicyIds().getPolicyIds();
+166     }
+167 
+168     /**
+169      * Returns the content stream.
+170      */
+171     public ContentStream getContentStream() {
+172         return (cmisContentStream == null ? atomContentStream : cmisContentStream);
+173     }
+174 
+175     /**
+176      * Returns the type definition.
+177      */
+178     public TypeDefinition getTypeDefinition() {
+179         return typeDef;
+180     }
+181 
+182     /**
+183      * Returns the bulk update data.
+184      */
+185     public BulkUpdateImpl getBulkUpdate() {
+186         return bulkUpdate;
+187     }
+188 
+189     /**
+190      * Parses the stream.
+191      */
+192     public void parse(InputStream stream) throws Exception {
+193         object = null;
+194         atomContentStream = null;
+195         cmisContentStream = null;
+196 
+197         if (stream == null) {
+198             return;
+199         }
+200 
+201         cappedStream = new CappedInputStream(stream, MAX_STREAM_LENGTH);
+202         XMLStreamReader parser = XMLUtils.createParser(cappedStream);
+203 
+204         while (true) {
+205             int event = parser.getEventType();
+206             if (event == XMLStreamReader.START_ELEMENT) {
+207                 QName name = parser.getName();
+208 
+209                 if (XMLConstants.NAMESPACE_ATOM.equals(name.getNamespaceURI())
+210                         && (TAG_ENTRY.equals(name.getLocalPart()))) {
+211                     parseEntry(parser);
+212                     break;
+213                 } else {
+214                     throw new CmisInvalidArgumentException("XML is not an Atom entry!");
+215                 }
+216             }
+217 
+218             if (!XMLUtils.next(parser)) {
+219                 break;
+220             }
+221         }
+222 
+223         parser.close();
+224     }
+225 
+226     /**
+227      * Parses an Atom entry.
+228      */
+229     private void parseEntry(XMLStreamReader parser) throws Exception {
+230         String atomTitle = null;
+231 
+232         XMLUtils.next(parser);
+233 
+234         // walk through all tags in entry
+235         while (true) {
+236             int event = parser.getEventType();
+237             if (event == XMLStreamReader.START_ELEMENT) {
+238                 QName name = parser.getName();
+239 
+240                 if (XMLConstants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) {
+241                     if (TAG_OBJECT.equals(name.getLocalPart())) {
+242                         parseObject(parser);
+243                     } else if (TAG_TYPE.equals(name.getLocalPart())) {
+244                         parseTypeDefinition(parser);
+245                     } else if (TAG_BULK_UPDATE.equals(name.getLocalPart())) {
+246                         parseBulkUpdate(parser);
+247                     } else if (TAG_CONTENT.equals(name.getLocalPart())) {
+248                         parseCmisContent(parser);
+249                     } else {
+250                         XMLUtils.skip(parser);
+251                     }
+252                 } else if (XMLConstants.NAMESPACE_ATOM.equals(name.getNamespaceURI())) {
+253                     if (TAG_CONTENT.equals(name.getLocalPart())) {
+254                         parseAtomContent(parser);
+255                     } else if (TAG_TITLE.equals(name.getLocalPart())) {
+256                         atomTitle = XMLUtils.readText(parser, XMLConstraints.MAX_STRING_LENGTH);
+257                     } else {
+258                         XMLUtils.skip(parser);
+259                     }
+260                 } else {
+261                     XMLUtils.skip(parser);
+262                 }
+263             } else if (event == XMLStreamReader.END_ELEMENT) {
+264                 break;
+265             } else {
+266                 if (!XMLUtils.next(parser)) {
+267                     break;
+268                 }
+269             }
+270         }
+271 
+272         // overwrite cmis:name with Atom title
+273         if ((object != null) && (object.getProperties() != null) && (atomTitle != null) && (atomTitle.length() > 0)) {
+274             PropertyString nameProperty = new PropertyStringImpl(PropertyIds.NAME, atomTitle);
+275             ((PropertiesImpl) object.getProperties()).replaceProperty(nameProperty);
+276         }
+277     }
+278 
+279     /**
+280      * Parses a CMIS object.
+281      */
+282     private void parseObject(XMLStreamReader parser) throws Exception {
+283         object = XMLConverter.convertObject(parser);
+284     }
+285 
+286     /**
+287      * Parses a CMIS type.
+288      */
+289     private void parseTypeDefinition(XMLStreamReader parser) throws Exception {
+290         typeDef = XMLConverter.convertTypeDefinition(parser);
+291     }
+292 
+293     /**
+294      * Parses a bluk update.
+295      */
+296     private void parseBulkUpdate(XMLStreamReader parser) throws Exception {
+297         bulkUpdate = XMLConverter.convertBulkUpdate(parser);
+298     }
+299 
+300     /**
+301      * Extract the content stream.
+302      */
+303     private void parseAtomContent(XMLStreamReader parser) throws Exception {
+304         atomContentStream = new ContentStreamImpl();
+305 
+306         // read attributes
+307         String type = "text";
+308         for (int i = 0; i < parser.getAttributeCount(); i++) {
+309             QName attrName = parser.getAttributeName(i);
+310             if (ATTR_TYPE.equals(attrName.getLocalPart())) {
+311                 atomContentStream.setMimeType(parser.getAttributeValue(i));
+312                 if (parser.getAttributeValue(i) != null) {
+313                     type = parser.getAttributeValue(i).trim().toLowerCase(Locale.ENGLISH);
+314                 }
+315             } else if (ATTR_SRC.equals(attrName.getLocalPart())) {
+316                 if (ignoreAtomContentSrc) {
+317                     atomContentStream = null;
+318                     XMLUtils.skip(parser);
+319                     return;
+320                 }
+321                 throw new CmisNotSupportedException("External content not supported!");
+322             }
+323         }
+324 
+325         ThresholdOutputStream ths = null;
+326         byte[] bytes = null;
+327         if (type.equals("text") || type.equals("html")) {
+328             ths = readContentBytes(parser);
+329         } else if (type.equals("xhtml")) {
+330             bytes = copy(parser);
+331         } else if (type.endsWith("/xml") || type.endsWith("+xml")) {
+332             bytes = copy(parser);
+333         } else if (type.startsWith("text/")) {
+334             ths = readContentBytes(parser);
+335         } else {
+336             ths = readBase64(parser);
+337         }
+338 
+339         if (ths != null) {
+340             atomContentStream.setStream(ths.getInputStream());
+341             atomContentStream.setLength(BigInteger.valueOf(ths.getSize()));
+342         }
+343 
+344         if (bytes != null) {
+345             atomContentStream.setStream(new ByteArrayInputStream(bytes));
+346             atomContentStream.setLength(BigInteger.valueOf(bytes.length));
+347             cappedStream.deductBytes(bytes.length);
+348         }
+349     }
+350 
+351     /**
+352      * Extract the content stream.
+353      */
+354     private void parseCmisContent(XMLStreamReader parser) throws Exception {
+355         cmisContentStream = new ContentStreamImpl();
+356 
+357         XMLUtils.next(parser);
+358 
+359         // walk through all tags in content
+360         while (true) {
+361             int event = parser.getEventType();
+362             if (event == XMLStreamReader.START_ELEMENT) {
+363                 QName name = parser.getName();
+364 
+365                 if (XMLConstants.NAMESPACE_RESTATOM.equals(name.getNamespaceURI())) {
+366                     if (TAG_MEDIATYPE.equals(name.getLocalPart())) {
+367                         cmisContentStream.setMimeType(XMLUtils.readText(parser, XMLConstraints.MAX_STRING_LENGTH));
+368                     } else if (TAG_BASE64.equals(name.getLocalPart())) {
+369                         ThresholdOutputStream ths = readBase64(parser);
+370                         cmisContentStream.setStream(ths.getInputStream());
+371                         cmisContentStream.setLength(BigInteger.valueOf(ths.getSize()));
+372                     } else {
+373                         XMLUtils.skip(parser);
+374                     }
+375                 } else if (XMLConstants.NAMESPACE_APACHE_CHEMISTRY.equals(name.getNamespaceURI())) {
+376                     if (TAG_FILENAME.equals(name.getLocalPart())) {
+377                         cmisContentStream.setFileName(XMLUtils.readText(parser, XMLConstraints.MAX_STRING_LENGTH));
+378                     } else {
+379                         XMLUtils.skip(parser);
+380                     }
+381                 } else {
+382                     XMLUtils.skip(parser);
+383                 }
+384             } else if (event == XMLStreamReader.END_ELEMENT) {
+385                 break;
+386             } else {
+387                 if (!XMLUtils.next(parser)) {
+388                     break;
+389                 }
+390             }
+391         }
+392 
+393         XMLUtils.next(parser);
+394     }
+395 
+396     /**
+397      * Parses a tag that contains content bytes.
+398      */
+399     private ThresholdOutputStream readContentBytes(XMLStreamReader parser) throws Exception {
+400         ThresholdOutputStream bufferStream = streamFactory.newOutputStream();
+401 
+402         XMLUtils.next(parser);
+403 
+404         try {
+405             while (true) {
+406                 int event = parser.getEventType();
+407                 if (event == XMLStreamReader.END_ELEMENT) {
+408                     break;
+409                 } else if (event == XMLStreamReader.CHARACTERS) {
+410                     String s = parser.getText();
+411                     if (s != null) {
+412                         byte[] bytes = s.getBytes("UTF-8");
+413                         bufferStream.write(bytes);
+414                         cappedStream.deductBytes(bytes.length);
+415                     }
+416                 } else if (event == XMLStreamReader.START_ELEMENT) {
+417                     throw new RuntimeException("Unexpected tag: " + parser.getName());
+418                 }
+419 
+420                 if (!XMLUtils.next(parser)) {
+421                     break;
+422                 }
+423             }
+424         } catch (Exception e) {
+425             bufferStream.destroy(); // remove temp file
+426             throw e;
+427         }
+428 
+429         XMLUtils.next(parser);
+430 
+431         return bufferStream;
+432     }
+433 
+434     /**
+435      * Parses a tag that contains base64 encoded content.
+436      */
+437     private ThresholdOutputStream readBase64(XMLStreamReader parser) throws Exception {
+438         ThresholdOutputStream bufferStream = streamFactory.newOutputStream();
+439         @SuppressWarnings("resource")
+440         Base64.OutputStream b64stream = new Base64.OutputStream(bufferStream, Base64.DECODE);
+441 
+442         XMLUtils.next(parser);
+443 
+444         try {
+445             while (true) {
+446                 int event = parser.getEventType();
+447                 if (event == XMLStreamReader.END_ELEMENT) {
+448                     break;
+449                 } else if (event == XMLStreamReader.CHARACTERS) {
+450                     int len = parser.getTextLength();
+451                     if (len > 0) {
+452                         char[] chars = parser.getTextCharacters();
+453                         int offset = parser.getTextStart();
+454                         for (int i = 0; i < len; i++) {
+455                             // it's base64/ASCII
+456                             b64stream.write(chars[offset + i]);
+457                         }
+458                         cappedStream.deductBytes(len);
+459                     }
+460                 } else if (event == XMLStreamReader.START_ELEMENT) {
+461                     throw new RuntimeException("Unexpected tag: " + parser.getName());
+462                 }
+463 
+464                 if (!XMLUtils.next(parser)) {
+465                     break;
+466                 }
+467             }
+468 
+469             b64stream.close();
+470         } catch (Exception e) {
+471             bufferStream.destroy(); // remove temp file
+472             throw e;
+473         }
+474 
+475         XMLUtils.next(parser);
+476 
+477         return bufferStream;
+478     }
+479 
+480     /**
+481      * Copies a subtree into a stream.
+482      */
+483     private static byte[] copy(XMLStreamReader parser) throws Exception {
+484         // create a writer
+485         ByteArrayOutputStream out = new ByteArrayOutputStream();
+486         XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
+487 
+488         writer.writeStartDocument();
+489 
+490         // copy subtree
+491         int level = 1;
+492         while (XMLUtils.next(parser)) {
+493             int event = parser.getEventType();
+494             if (event == XMLStreamReader.START_ELEMENT) {
+495                 copyStartElement(parser, writer);
+496                 level++;
+497             } else if (event == XMLStreamReader.CHARACTERS) {
+498                 writer.writeCharacters(parser.getText());
+499             } else if (event == XMLStreamReader.COMMENT) {
+500                 writer.writeComment(parser.getText());
+501             } else if (event == XMLStreamReader.CDATA) {
+502                 writer.writeCData(parser.getText());
+503             } else if (event == XMLStreamReader.END_ELEMENT) {
+504                 level--;
+505                 if (level == 0) {
+506                     break;
+507                 }
+508                 writer.writeEndElement();
+509             } else {
+510                 break;
+511             }
+512         }
+513 
+514         writer.writeEndDocument();
+515 
+516         XMLUtils.next(parser);
+517 
+518         return out.toByteArray();
+519     }
+520 
+521     /**
+522      * Copies a XML start element.
+523      */
+524     private static void copyStartElement(XMLStreamReader parser, XMLStreamWriter writer) throws Exception {
+525         String namespaceUri = parser.getNamespaceURI();
+526         String prefix = parser.getPrefix();
+527         String localName = parser.getLocalName();
+528 
+529         // write start element
+530         if (namespaceUri != null) {
+531             if ((prefix == null) || (prefix.length() == 0)) {
+532                 writer.writeStartElement(localName);
+533             } else {
+534                 writer.writeStartElement(prefix, localName, namespaceUri);
+535             }
+536         } else {
+537             writer.writeStartElement(localName);
+538         }
+539 
+540         // set namespaces
+541         for (int i = 0; i < parser.getNamespaceCount(); i++) {
+542             addNamespace(writer, parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
+543         }
+544         addNamespaceIfMissing(writer, prefix, namespaceUri);
+545 
+546         // write attributes
+547         for (int i = 0; i < parser.getAttributeCount(); i++) {
+548             String attrNamespaceUri = parser.getAttributeNamespace(i);
+549             String attrPrefix = parser.getAttributePrefix(i);
+550             String attrName = parser.getAttributeLocalName(i);
+551             String attrValue = parser.getAttributeValue(i);
+552 
+553             if ((attrNamespaceUri == null) || (attrNamespaceUri.trim().length() == 0)) {
+554                 writer.writeAttribute(attrName, attrValue);
+555             } else if ((attrPrefix == null) || (attrPrefix.trim().length() == 0)) {
+556                 writer.writeAttribute(attrNamespaceUri, attrName, attrValue);
+557             } else {
+558                 addNamespaceIfMissing(writer, attrPrefix, attrNamespaceUri);
+559                 writer.writeAttribute(attrPrefix, attrNamespaceUri, attrName, attrValue);
+560             }
+561         }
+562     }
+563 
+564     /**
+565      * Checks if the given prefix is assigned to the given namespace.
+566      */
+567     @SuppressWarnings("unchecked")
+568     private static void addNamespaceIfMissing(XMLStreamWriter writer, String prefix, String namespaceUri)
+569             throws Exception {
+570         if ((namespaceUri == null) || (namespaceUri.trim().length() == 0)) {
+571             return;
+572         }
+573 
+574         if (prefix == null) {
+575             prefix = "";
+576         }
+577 
+578         Iterator<String> iter = (Iterator<String>) writer.getNamespaceContext().getPrefixes(namespaceUri);
+579         if (iter == null) {
+580             return;
+581         }
+582 
+583         while (iter.hasNext()) {
+584             String p = iter.next();
+585             if ((p != null) && (p.equals(prefix))) {
+586                 return;
+587             }
+588         }
+589 
+590         addNamespace(writer, prefix, namespaceUri);
+591     }
+592 
+593     /**
+594      * Adds a namespace to a XML element.
+595      */
+596     private static void addNamespace(XMLStreamWriter writer, String prefix, String namespaceUri) throws Exception {
+597         if ((prefix == null) || (prefix.trim().length() == 0)) {
+598             writer.setDefaultNamespace(namespaceUri);
+599             writer.writeDefaultNamespace(namespaceUri);
+600         } else {
+601             writer.setPrefix(prefix, namespaceUri);
+602             writer.writeNamespace(prefix, namespaceUri);
+603         }
+604     }
+605 }
+
+
+ +