< prev index next >

src/java.xml/share/classes/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java

Print this page
rev 52881 : 8214971: Replace use of string.equals("") with isEmpty()
Reviewed-by: jlaskey, prappo, lancea, dfuchs, redestad


 611     }
 612 
 613     @Override
 614     public void writeAttribute(String prefix, String namespaceURI,
 615         String localName, String value) throws XMLStreamException {
 616         try {
 617             if (!fStartTagOpened) {
 618                 throw new XMLStreamException(
 619                     "Attribute not associated with any element");
 620             }
 621 
 622             if (namespaceURI == null) {
 623                 throw new XMLStreamException("NamespaceURI cannot be null");
 624             }
 625 
 626             if (localName == null) {
 627                 throw new XMLStreamException("Local name cannot be null");
 628             }
 629 
 630             if (!fIsRepairingNamespace) {
 631                 if (prefix == null || prefix.equals("")){
 632                     if (!namespaceURI.equals("")) {
 633                         throw new XMLStreamException("prefix cannot be null or empty");
 634                     } else {
 635                         writeAttributeWithPrefix(null, localName, value);
 636                         return;
 637                     }
 638                 }
 639 
 640                 if (!prefix.equals(XMLConstants.XML_NS_PREFIX) ||
 641                         !namespaceURI.equals(XMLConstants.XML_NS_URI)) {
 642 
 643                     prefix = fSymbolTable.addSymbol(prefix);
 644                     namespaceURI = fSymbolTable.addSymbol(namespaceURI);
 645 
 646                     if (fInternalNamespaceContext.containsPrefixInCurrentContext(prefix)){
 647 
 648                         String tmpURI = fInternalNamespaceContext.getURI(prefix);
 649 
 650                         if (tmpURI != null && tmpURI != namespaceURI){
 651                             throw new XMLStreamException("Prefix "+prefix+" is " +
 652                                     "already bound to "+tmpURI+


 891             throw new XMLStreamException(e);
 892         }
 893     }
 894 
 895     @Override
 896     public void writeEndDocument() throws XMLStreamException {
 897         try {
 898             if (fStartTagOpened) {
 899                 closeStartTag();
 900             }
 901 
 902             while (!fElementStack.empty()) {
 903                 ElementState elem = fElementStack.pop();
 904                 fInternalNamespaceContext.popContext();
 905 
 906                 if (elem.isEmpty) {
 907                     //fWriter.write(CLOSE_EMPTY_ELEMENT);
 908                 } else {
 909                     fWriter.write(OPEN_END_TAG);
 910 
 911                     if ((elem.prefix != null) && !(elem.prefix).equals("")) {
 912                         fWriter.write(elem.prefix);
 913                         fWriter.write(":");
 914                     }
 915 
 916                     fWriter.write(elem.localpart);
 917                     fWriter.write(CLOSE_END_TAG);
 918                 }
 919             }
 920         } catch (IOException e) {
 921             throw new XMLStreamException(e);
 922         } catch (ArrayIndexOutOfBoundsException e) {
 923             throw new XMLStreamException("No more elements to write");
 924         }
 925     }
 926 
 927     @Override
 928     public void writeEndElement() throws XMLStreamException {
 929         try {
 930             if (fStartTagOpened) {
 931                 closeStartTag();
 932             }
 933 
 934             ElementState currentElement = fElementStack.pop();
 935 
 936             if (currentElement == null) {
 937                 throw new XMLStreamException("No element was found to write");
 938             }
 939 
 940             if (currentElement.isEmpty) {
 941                 //fWriter.write(CLOSE_EMPTY_ELEMENT);
 942                 return;
 943             }
 944 
 945             fWriter.write(OPEN_END_TAG);
 946 
 947             if ((currentElement.prefix != null) &&
 948                     !(currentElement.prefix).equals("")) {
 949                 fWriter.write(currentElement.prefix);
 950                 fWriter.write(":");
 951             }
 952 
 953             fWriter.write(currentElement.localpart);
 954             fWriter.write(CLOSE_END_TAG);
 955             fInternalNamespaceContext.popContext();
 956         } catch (IOException e) {
 957             throw new XMLStreamException(e);
 958         } catch (ArrayIndexOutOfBoundsException e) {
 959             throw new XMLStreamException(
 960                     "No element was found to write: "
 961                     + e.toString(), e);
 962         }
 963     }
 964 
 965     @Override
 966     public void writeEntityRef(String refName) throws XMLStreamException {
 967         try {
 968             if (fStartTagOpened) {


1163      */
1164     @Override
1165     public void writeStartDocument(String encoding, String version)
1166         throws XMLStreamException {
1167         writeStartDocument(encoding, version, false, false);
1168     }
1169 
1170     public void writeStartDocument(String encoding, String version,
1171             boolean standalone, boolean standaloneSet)
1172         throws XMLStreamException {
1173 
1174         try {
1175             if ((encoding == null || encoding.length() == 0)
1176                     && (version == null || version.length() == 0)
1177                     && (!standaloneSet)) {
1178                 fWriter.write(DEFAULT_XMLDECL);
1179                 return;
1180             }
1181 
1182             // Verify the encoding before writing anything
1183             if (encoding != null && !encoding.equals("")) {
1184                 verifyEncoding(encoding);
1185             }
1186 
1187             fWriter.write("<?xml version=\"");
1188 
1189             if ((version == null) || version.equals("")) {
1190                 fWriter.write(DEFAULT_XML_VERSION);
1191             } else {
1192                 fWriter.write(version);
1193             }
1194 
1195             if (encoding != null && !encoding.equals("")) {
1196                 fWriter.write("\" encoding=\"");
1197                 fWriter.write(encoding);
1198             }
1199 
1200             if (standaloneSet) {
1201                 fWriter.write("\" standalone=\"");
1202                 if (standalone) {
1203                     fWriter.write("yes");
1204                 } else {
1205                     fWriter.write("no");
1206                 }
1207             }
1208 
1209             fWriter.write("\"?>");
1210         } catch (IOException ex) {
1211             throw new XMLStreamException(ex);
1212         }
1213     }
1214 
1215     /**


1567 
1568                 for (int i = 0; i < len; i++) {
1569                     qname = fNamespaceDecls.get(i);
1570 
1571                     if (qname != null) {
1572                         if (fInternalNamespaceContext.declarePrefix(qname.prefix,
1573                             qname.uri)) {
1574                             writenamespace(qname.prefix, qname.uri);
1575                         }
1576                     }
1577                 }
1578 
1579                 fNamespaceDecls.clear();
1580 
1581                 Attribute attr;
1582 
1583                 for (int j = 0; j < fAttributeCache.size(); j++) {
1584                     attr = fAttributeCache.get(j);
1585 
1586                     if ((attr.prefix != null) && (attr.uri != null)) {
1587                         if (!attr.prefix.equals("") && !attr.uri.equals("") ) {
1588                             String tmp = fInternalNamespaceContext.getPrefix(attr.uri);
1589 
1590                             if ((tmp == null) || (!tmp.equals(attr.prefix))) {
1591                                 tmp = getAttrPrefix(attr.uri);
1592                                 if (tmp == null) {
1593                                     if (fInternalNamespaceContext.declarePrefix(attr.prefix,
1594                                         attr.uri)) {
1595                                         writenamespace(attr.prefix, attr.uri);
1596                                     }
1597                                 } else {
1598                                     writenamespace(attr.prefix, attr.uri);
1599                                 }
1600                             }
1601                         }
1602                     }
1603 
1604                     writeAttributeWithPrefix(attr.prefix, attr.localpart,
1605                         attr.value);
1606                 }
1607                 fAttrNamespace = null;


1748 
1749             if ((tmpURI != null) && tmpURI.equals(uri)) {
1750                 return true;
1751             }
1752         }
1753 
1754         return false;
1755     }
1756 
1757     /**
1758      * Correct's namespaces  as per requirements of isReparisingNamespace property.
1759      */
1760     protected void repair() {
1761         Attribute attr;
1762         Attribute attr2;
1763         ElementState currentElement = fElementStack.peek();
1764         removeDuplicateDecls();
1765 
1766         for(int i=0 ; i< fAttributeCache.size();i++){
1767             attr = fAttributeCache.get(i);
1768             if((attr.prefix != null && !attr.prefix.equals("")) || (attr.uri != null && !attr.uri.equals(""))) {
1769                 correctPrefix(currentElement,attr);
1770             }
1771         }
1772 
1773         if (!isDeclared(currentElement)) {
1774             if ((currentElement.prefix != null) &&
1775                     (currentElement.uri != null)) {
1776                 if ((!currentElement.prefix.equals("")) && (!currentElement.uri.equals(""))) {
1777                     fNamespaceDecls.add(currentElement);
1778                 }
1779             }
1780         }
1781 
1782         for(int i=0 ; i< fAttributeCache.size();i++){
1783             attr = fAttributeCache.get(i);
1784             for(int j=i+1;j<fAttributeCache.size();j++){
1785                 attr2 = fAttributeCache.get(j);
1786                 if(!"".equals(attr.prefix)&& !"".equals(attr2.prefix)){
1787                     correctPrefix(attr,attr2);
1788                 }
1789             }
1790         }
1791 
1792         repairNamespaceDecl(currentElement);
1793 
1794         int i;
1795 
1796         for (i = 0; i < fAttributeCache.size(); i++) {
1797             attr = fAttributeCache.get(i);
1798             /* If 'attr' is an attribute and it is in no namespace(which means that prefix="", uri=""), attr's
1799                namespace should not be redinded. See [http://www.w3.org/TR/REC-xml-names/#defaulting].
1800              */
1801             if (attr.prefix != null && attr.prefix.equals("") && attr.uri != null && attr.uri.equals("")){
1802                 repairNamespaceDecl(attr);
1803             }
1804         }
1805 
1806         QName qname = null;
1807 
1808         for (i = 0; i < fNamespaceDecls.size(); i++) {
1809             qname = fNamespaceDecls.get(i);
1810 
1811             if (qname != null) {
1812                 fInternalNamespaceContext.declarePrefix(qname.prefix, qname.uri);
1813             }
1814         }
1815 
1816         for (i = 0; i < fAttributeCache.size(); i++) {
1817             attr = fAttributeCache.get(i);
1818             correctPrefix(attr, XMLStreamConstants.ATTRIBUTE);
1819         }
1820     }
1821 




 611     }
 612 
 613     @Override
 614     public void writeAttribute(String prefix, String namespaceURI,
 615         String localName, String value) throws XMLStreamException {
 616         try {
 617             if (!fStartTagOpened) {
 618                 throw new XMLStreamException(
 619                     "Attribute not associated with any element");
 620             }
 621 
 622             if (namespaceURI == null) {
 623                 throw new XMLStreamException("NamespaceURI cannot be null");
 624             }
 625 
 626             if (localName == null) {
 627                 throw new XMLStreamException("Local name cannot be null");
 628             }
 629 
 630             if (!fIsRepairingNamespace) {
 631                 if (prefix == null || prefix.isEmpty()){
 632                     if (!namespaceURI.isEmpty()) {
 633                         throw new XMLStreamException("prefix cannot be null or empty");
 634                     } else {
 635                         writeAttributeWithPrefix(null, localName, value);
 636                         return;
 637                     }
 638                 }
 639 
 640                 if (!prefix.equals(XMLConstants.XML_NS_PREFIX) ||
 641                         !namespaceURI.equals(XMLConstants.XML_NS_URI)) {
 642 
 643                     prefix = fSymbolTable.addSymbol(prefix);
 644                     namespaceURI = fSymbolTable.addSymbol(namespaceURI);
 645 
 646                     if (fInternalNamespaceContext.containsPrefixInCurrentContext(prefix)){
 647 
 648                         String tmpURI = fInternalNamespaceContext.getURI(prefix);
 649 
 650                         if (tmpURI != null && tmpURI != namespaceURI){
 651                             throw new XMLStreamException("Prefix "+prefix+" is " +
 652                                     "already bound to "+tmpURI+


 891             throw new XMLStreamException(e);
 892         }
 893     }
 894 
 895     @Override
 896     public void writeEndDocument() throws XMLStreamException {
 897         try {
 898             if (fStartTagOpened) {
 899                 closeStartTag();
 900             }
 901 
 902             while (!fElementStack.empty()) {
 903                 ElementState elem = fElementStack.pop();
 904                 fInternalNamespaceContext.popContext();
 905 
 906                 if (elem.isEmpty) {
 907                     //fWriter.write(CLOSE_EMPTY_ELEMENT);
 908                 } else {
 909                     fWriter.write(OPEN_END_TAG);
 910 
 911                     if ((elem.prefix != null) && !(elem.prefix).isEmpty()) {
 912                         fWriter.write(elem.prefix);
 913                         fWriter.write(":");
 914                     }
 915 
 916                     fWriter.write(elem.localpart);
 917                     fWriter.write(CLOSE_END_TAG);
 918                 }
 919             }
 920         } catch (IOException e) {
 921             throw new XMLStreamException(e);
 922         } catch (ArrayIndexOutOfBoundsException e) {
 923             throw new XMLStreamException("No more elements to write");
 924         }
 925     }
 926 
 927     @Override
 928     public void writeEndElement() throws XMLStreamException {
 929         try {
 930             if (fStartTagOpened) {
 931                 closeStartTag();
 932             }
 933 
 934             ElementState currentElement = fElementStack.pop();
 935 
 936             if (currentElement == null) {
 937                 throw new XMLStreamException("No element was found to write");
 938             }
 939 
 940             if (currentElement.isEmpty) {
 941                 //fWriter.write(CLOSE_EMPTY_ELEMENT);
 942                 return;
 943             }
 944 
 945             fWriter.write(OPEN_END_TAG);
 946 
 947             if ((currentElement.prefix != null) &&
 948                     !(currentElement.prefix).isEmpty()) {
 949                 fWriter.write(currentElement.prefix);
 950                 fWriter.write(":");
 951             }
 952 
 953             fWriter.write(currentElement.localpart);
 954             fWriter.write(CLOSE_END_TAG);
 955             fInternalNamespaceContext.popContext();
 956         } catch (IOException e) {
 957             throw new XMLStreamException(e);
 958         } catch (ArrayIndexOutOfBoundsException e) {
 959             throw new XMLStreamException(
 960                     "No element was found to write: "
 961                     + e.toString(), e);
 962         }
 963     }
 964 
 965     @Override
 966     public void writeEntityRef(String refName) throws XMLStreamException {
 967         try {
 968             if (fStartTagOpened) {


1163      */
1164     @Override
1165     public void writeStartDocument(String encoding, String version)
1166         throws XMLStreamException {
1167         writeStartDocument(encoding, version, false, false);
1168     }
1169 
1170     public void writeStartDocument(String encoding, String version,
1171             boolean standalone, boolean standaloneSet)
1172         throws XMLStreamException {
1173 
1174         try {
1175             if ((encoding == null || encoding.length() == 0)
1176                     && (version == null || version.length() == 0)
1177                     && (!standaloneSet)) {
1178                 fWriter.write(DEFAULT_XMLDECL);
1179                 return;
1180             }
1181 
1182             // Verify the encoding before writing anything
1183             if (encoding != null && !encoding.isEmpty()) {
1184                 verifyEncoding(encoding);
1185             }
1186 
1187             fWriter.write("<?xml version=\"");
1188 
1189             if ((version == null) || version.isEmpty()) {
1190                 fWriter.write(DEFAULT_XML_VERSION);
1191             } else {
1192                 fWriter.write(version);
1193             }
1194 
1195             if (encoding != null && !encoding.isEmpty()) {
1196                 fWriter.write("\" encoding=\"");
1197                 fWriter.write(encoding);
1198             }
1199 
1200             if (standaloneSet) {
1201                 fWriter.write("\" standalone=\"");
1202                 if (standalone) {
1203                     fWriter.write("yes");
1204                 } else {
1205                     fWriter.write("no");
1206                 }
1207             }
1208 
1209             fWriter.write("\"?>");
1210         } catch (IOException ex) {
1211             throw new XMLStreamException(ex);
1212         }
1213     }
1214 
1215     /**


1567 
1568                 for (int i = 0; i < len; i++) {
1569                     qname = fNamespaceDecls.get(i);
1570 
1571                     if (qname != null) {
1572                         if (fInternalNamespaceContext.declarePrefix(qname.prefix,
1573                             qname.uri)) {
1574                             writenamespace(qname.prefix, qname.uri);
1575                         }
1576                     }
1577                 }
1578 
1579                 fNamespaceDecls.clear();
1580 
1581                 Attribute attr;
1582 
1583                 for (int j = 0; j < fAttributeCache.size(); j++) {
1584                     attr = fAttributeCache.get(j);
1585 
1586                     if ((attr.prefix != null) && (attr.uri != null)) {
1587                         if (!attr.prefix.isEmpty() && !attr.uri.isEmpty() ) {
1588                             String tmp = fInternalNamespaceContext.getPrefix(attr.uri);
1589 
1590                             if ((tmp == null) || (!tmp.equals(attr.prefix))) {
1591                                 tmp = getAttrPrefix(attr.uri);
1592                                 if (tmp == null) {
1593                                     if (fInternalNamespaceContext.declarePrefix(attr.prefix,
1594                                         attr.uri)) {
1595                                         writenamespace(attr.prefix, attr.uri);
1596                                     }
1597                                 } else {
1598                                     writenamespace(attr.prefix, attr.uri);
1599                                 }
1600                             }
1601                         }
1602                     }
1603 
1604                     writeAttributeWithPrefix(attr.prefix, attr.localpart,
1605                         attr.value);
1606                 }
1607                 fAttrNamespace = null;


1748 
1749             if ((tmpURI != null) && tmpURI.equals(uri)) {
1750                 return true;
1751             }
1752         }
1753 
1754         return false;
1755     }
1756 
1757     /**
1758      * Correct's namespaces  as per requirements of isReparisingNamespace property.
1759      */
1760     protected void repair() {
1761         Attribute attr;
1762         Attribute attr2;
1763         ElementState currentElement = fElementStack.peek();
1764         removeDuplicateDecls();
1765 
1766         for(int i=0 ; i< fAttributeCache.size();i++){
1767             attr = fAttributeCache.get(i);
1768             if((attr.prefix != null && !attr.prefix.isEmpty()) || (attr.uri != null && !attr.uri.isEmpty())) {
1769                 correctPrefix(currentElement,attr);
1770             }
1771         }
1772 
1773         if (!isDeclared(currentElement)) {
1774             if ((currentElement.prefix != null) &&
1775                     (currentElement.uri != null)) {
1776                 if ((!currentElement.prefix.isEmpty()) && (!currentElement.uri.isEmpty())) {
1777                     fNamespaceDecls.add(currentElement);
1778                 }
1779             }
1780         }
1781 
1782         for(int i=0 ; i< fAttributeCache.size();i++){
1783             attr = fAttributeCache.get(i);
1784             for(int j=i+1;j<fAttributeCache.size();j++){
1785                 attr2 = fAttributeCache.get(j);
1786                 if(!"".equals(attr.prefix)&& !"".equals(attr2.prefix)){
1787                     correctPrefix(attr,attr2);
1788                 }
1789             }
1790         }
1791 
1792         repairNamespaceDecl(currentElement);
1793 
1794         int i;
1795 
1796         for (i = 0; i < fAttributeCache.size(); i++) {
1797             attr = fAttributeCache.get(i);
1798             /* If 'attr' is an attribute and it is in no namespace(which means that prefix="", uri=""), attr's
1799                namespace should not be redinded. See [http://www.w3.org/TR/REC-xml-names/#defaulting].
1800              */
1801             if (attr.prefix != null && attr.prefix.isEmpty() && attr.uri != null && attr.uri.isEmpty()){
1802                 repairNamespaceDecl(attr);
1803             }
1804         }
1805 
1806         QName qname = null;
1807 
1808         for (i = 0; i < fNamespaceDecls.size(); i++) {
1809             qname = fNamespaceDecls.get(i);
1810 
1811             if (qname != null) {
1812                 fInternalNamespaceContext.declarePrefix(qname.prefix, qname.uri);
1813             }
1814         }
1815 
1816         for (i = 0; i < fAttributeCache.size(); i++) {
1817             attr = fAttributeCache.get(i);
1818             correctPrefix(attr, XMLStreamConstants.ATTRIBUTE);
1819         }
1820     }
1821 


< prev index next >