PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/xml.tax/lib/src/org/netbeans/tax/TreeUtilities.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1460 lines | 1001 code | 159 blank | 300 comment | 63 complexity | 6ea6d34d7c8846d0db5530667533688b MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.tax;
  45. import java.util.Map;
  46. import java.util.TreeMap;
  47. import java.util.Collection;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.IOException;
  50. import java.io.InputStreamReader;
  51. import org.netbeans.tax.spec.AttlistDecl;
  52. import org.netbeans.tax.spec.Attribute;
  53. import org.netbeans.tax.spec.CDATASection;
  54. import org.netbeans.tax.spec.CharacterReference;
  55. import org.netbeans.tax.spec.Comment;
  56. import org.netbeans.tax.spec.ConditionalSection;
  57. import org.netbeans.tax.spec.DocumentFragment;
  58. import org.netbeans.tax.spec.Document;
  59. import org.netbeans.tax.spec.DocumentType;
  60. import org.netbeans.tax.spec.DTD;
  61. import org.netbeans.tax.spec.ElementDecl;
  62. import org.netbeans.tax.spec.Element;
  63. import org.netbeans.tax.spec.EntityDecl;
  64. import org.netbeans.tax.spec.GeneralEntityReference;
  65. import org.netbeans.tax.spec.NotationDecl;
  66. import org.netbeans.tax.spec.ParameterEntityReference;
  67. import org.netbeans.tax.spec.ProcessingInstruction;
  68. import org.netbeans.tax.spec.Text;
  69. /**
  70. *
  71. * @author Libor Kramolis
  72. * @version 0.1
  73. */
  74. public final class TreeUtilities {
  75. /** */
  76. private static Constraints constraints = new Constraints ();
  77. //
  78. // Node.Constraints
  79. //
  80. /**
  81. */
  82. public static final void checkAttributeName (TreeName treeName) throws InvalidArgumentException {
  83. constraints.checkAttributeName (treeName);
  84. }
  85. /**
  86. */
  87. public static final boolean isValidAttributeName (TreeName treeName) {
  88. return constraints.isValidAttributeName (treeName);
  89. }
  90. /**
  91. */
  92. public static final void checkElementTagName (TreeName elementTreeName) throws InvalidArgumentException {
  93. constraints.checkElementTagName (elementTreeName);
  94. }
  95. /**
  96. */
  97. public static final boolean isValidElementTagName (TreeName elementTreeName) {
  98. return constraints.isValidElementTagName (elementTreeName);
  99. }
  100. /**
  101. */
  102. public static final void checkNotationDeclSystemId (String systemId) throws InvalidArgumentException {
  103. constraints.checkNotationDeclSystemId (systemId);
  104. }
  105. /**
  106. */
  107. public static final boolean isValidNotationDeclSystemId (String systemId) {
  108. return constraints.isValidNotationDeclSystemId (systemId);
  109. }
  110. /**
  111. */
  112. public static final void checkDocumentEncoding (String encoding) throws InvalidArgumentException {
  113. constraints.checkDocumentEncoding (encoding);
  114. }
  115. /**
  116. */
  117. public static final boolean isValidDocumentEncoding (String encoding) {
  118. return constraints.isValidDocumentEncoding (encoding);
  119. }
  120. /**
  121. */
  122. public static final void checkDTDEncoding (String encoding) throws InvalidArgumentException {
  123. constraints.checkDTDEncoding (encoding);
  124. }
  125. /**
  126. */
  127. public static final boolean isValidDTDEncoding (String encoding) {
  128. return constraints.isValidDTDEncoding (encoding);
  129. }
  130. /**
  131. */
  132. public static final void checkCharacterReferenceName (String name) throws InvalidArgumentException {
  133. constraints.checkCharacterReferenceName (name);
  134. }
  135. /**
  136. */
  137. public static final boolean isValidCharacterReferenceName (String name) {
  138. return constraints.isValidCharacterReferenceName (name);
  139. }
  140. /**
  141. */
  142. public static final void checkEntityDeclInternalText (String internalText) throws InvalidArgumentException {
  143. constraints.checkEntityDeclInternalText (internalText);
  144. }
  145. /**
  146. */
  147. public static final boolean isValidEntityDeclInternalText (String internalText) {
  148. return constraints.isValidEntityDeclInternalText (internalText);
  149. }
  150. /**
  151. */
  152. public static final void checkAttlistDeclElementName (String elementName) throws InvalidArgumentException {
  153. constraints.checkAttlistDeclElementName (elementName);
  154. }
  155. /**
  156. */
  157. public static final boolean isValidAttlistDeclElementName (String elementName) {
  158. return constraints.isValidAttlistDeclElementName (elementName);
  159. }
  160. /**
  161. */
  162. public static final void checkDTDVersion (String version) throws InvalidArgumentException {
  163. constraints.checkDTDVersion (version);
  164. }
  165. /**
  166. */
  167. public static final boolean isValidDTDVersion (String version) {
  168. return constraints.isValidDTDVersion (version);
  169. }
  170. /**
  171. */
  172. public static final void checkDocumentTypeSystemId (String systemId) throws InvalidArgumentException {
  173. constraints.checkDocumentTypeSystemId (systemId);
  174. }
  175. /**
  176. */
  177. public static final boolean isValidDocumentTypeSystemId (String systemId) {
  178. return constraints.isValidDocumentTypeSystemId (systemId);
  179. }
  180. /**
  181. */
  182. public static final void checkDocumentTypeElementName (String elementName) throws InvalidArgumentException {
  183. constraints.checkDocumentTypeElementName (elementName);
  184. }
  185. /**
  186. */
  187. public static final boolean isValidDocumentTypeElementName (String elementName) {
  188. return constraints.isValidDocumentTypeElementName (elementName);
  189. }
  190. /**
  191. */
  192. public static final void checkDocumentStandalone (String standalone) throws InvalidArgumentException {
  193. constraints.checkDocumentStandalone (standalone);
  194. }
  195. /**
  196. */
  197. public static final boolean isValidDocumentStandalone (String standalone) {
  198. return constraints.isValidDocumentStandalone (standalone);
  199. }
  200. /**
  201. */
  202. public static final void checkEntityDeclName (String name) throws InvalidArgumentException {
  203. constraints.checkEntityDeclName (name);
  204. }
  205. /**
  206. */
  207. public static final boolean isValidEntityDeclName (String name) {
  208. return constraints.isValidEntityDeclName (name);
  209. }
  210. /**
  211. */
  212. public static final void checkAttlistDeclAttributeEnumeratedType (String[] enumeratedType) throws InvalidArgumentException {
  213. constraints.checkAttlistDeclAttributeEnumeratedType (enumeratedType);
  214. }
  215. /**
  216. */
  217. public static final boolean isValidAttlistDeclAttributeEnumeratedType (String[] enumeratedType) {
  218. return constraints.isValidAttlistDeclAttributeEnumeratedType (enumeratedType);
  219. }
  220. /**
  221. */
  222. public static final void checkProcessingInstructionData (String data) throws InvalidArgumentException {
  223. constraints.checkProcessingInstructionData (data);
  224. }
  225. /**
  226. */
  227. public static final boolean isValidProcessingInstructionData (String data) {
  228. return constraints.isValidProcessingInstructionData (data);
  229. }
  230. /**
  231. */
  232. public static final void checkEntityDeclNotationName (String notationName) throws InvalidArgumentException {
  233. constraints.checkEntityDeclNotationName (notationName);
  234. }
  235. /**
  236. */
  237. public static final boolean isValidEntityDeclNotationName (String notationName) {
  238. return constraints.isValidEntityDeclNotationName (notationName);
  239. }
  240. /**
  241. */
  242. public static final void checkElementDeclName (String name) throws InvalidArgumentException {
  243. constraints.checkElementDeclName (name);
  244. }
  245. /**
  246. */
  247. public static final boolean isValidElementDeclName (String name) {
  248. return constraints.isValidElementDeclName (name);
  249. }
  250. /**
  251. */
  252. public static final void checkGeneralEntityReferenceName (String name) throws InvalidArgumentException {
  253. constraints.checkGeneralEntityReferenceName (name);
  254. }
  255. /**
  256. */
  257. public static final boolean isValidGeneralEntityReferenceName (String name) {
  258. return constraints.isValidGeneralEntityReferenceName (name);
  259. }
  260. /**
  261. */
  262. public static final void checkEntityDeclSystemId (String systemId) throws InvalidArgumentException {
  263. constraints.checkEntityDeclSystemId (systemId);
  264. }
  265. /**
  266. */
  267. public static final boolean isValidEntityDeclSystemId (String systemId) {
  268. return constraints.isValidEntityDeclSystemId (systemId);
  269. }
  270. /**
  271. */
  272. public static final void checkProcessingInstructionTarget (String target) throws InvalidArgumentException {
  273. constraints.checkProcessingInstructionTarget (target);
  274. }
  275. /**
  276. */
  277. public static final boolean isValidProcessingInstructionTarget (String target) {
  278. return constraints.isValidProcessingInstructionTarget (target);
  279. }
  280. /**
  281. */
  282. public static final void checkEntityDeclPublicId (String publicId) throws InvalidArgumentException {
  283. constraints.checkEntityDeclPublicId (publicId);
  284. }
  285. /**
  286. */
  287. public static final boolean isValidEntityDeclPublicId (String publicId) {
  288. return constraints.isValidEntityDeclPublicId (publicId);
  289. }
  290. /**
  291. */
  292. public static final void checkAttlistDeclAttributeDefaultValue (String defaultValue) throws InvalidArgumentException {
  293. constraints.checkAttlistDeclAttributeDefaultValue (defaultValue);
  294. }
  295. /**
  296. */
  297. public static final boolean isValidAttlistDeclAttributeDefaultValue (String defaultValue) {
  298. return constraints.isValidAttlistDeclAttributeDefaultValue (defaultValue);
  299. }
  300. /**
  301. */
  302. public static final void checkDocumentFragmentVersion (String version) throws InvalidArgumentException {
  303. constraints.checkDocumentFragmentVersion (version);
  304. }
  305. /**
  306. */
  307. public static final boolean isValidDocumentFragmentVersion (String version) {
  308. return constraints.isValidDocumentFragmentVersion (version);
  309. }
  310. /**
  311. */
  312. public static final void checkNotationDeclName (String name) throws InvalidArgumentException {
  313. constraints.checkNotationDeclName (name);
  314. }
  315. /**
  316. */
  317. public static final boolean isValidNotationDeclName (String name) {
  318. return constraints.isValidNotationDeclName (name);
  319. }
  320. /**
  321. */
  322. public static final void checkAttributeValue (String value) throws InvalidArgumentException {
  323. constraints.checkAttributeValue (value);
  324. }
  325. /**
  326. */
  327. public static final boolean isValidAttributeValue (String value) {
  328. return constraints.isValidAttributeValue (value);
  329. }
  330. /**
  331. */
  332. public static final void checkParameterEntityReferenceName (String name) throws InvalidArgumentException {
  333. constraints.checkParameterEntityReferenceName (name);
  334. }
  335. /**
  336. */
  337. public static final boolean isValidParameterEntityReferenceName (String name) {
  338. return constraints.isValidParameterEntityReferenceName (name);
  339. }
  340. /**
  341. */
  342. public static final void checkDocumentFragmentEncoding (String encoding) throws InvalidArgumentException {
  343. constraints.checkDocumentFragmentEncoding (encoding);
  344. }
  345. /**
  346. */
  347. public static final boolean isValidDocumentFragmentEncoding (String encoding) {
  348. return constraints.isValidDocumentFragmentEncoding (encoding);
  349. }
  350. /**
  351. */
  352. public static final void checkTextData (String data) throws InvalidArgumentException {
  353. constraints.checkTextData (data);
  354. }
  355. /**
  356. */
  357. public static final boolean isValidTextData (String data) {
  358. return constraints.isValidTextData (data);
  359. }
  360. /**
  361. */
  362. public static final void checkDocumentTypePublicId (String publicId) throws InvalidArgumentException {
  363. constraints.checkDocumentTypePublicId (publicId);
  364. }
  365. /**
  366. */
  367. public static final boolean isValidDocumentTypePublicId (String publicId) {
  368. return constraints.isValidDocumentTypePublicId (publicId);
  369. }
  370. /**
  371. */
  372. public static final void checkElementDeclContentType (TreeElementDecl.ContentType contentType) throws InvalidArgumentException {
  373. constraints.checkElementDeclContentType (contentType);
  374. }
  375. /**
  376. */
  377. public static final boolean isValidElementDeclContentType (TreeElementDecl.ContentType contentType) {
  378. return constraints.isValidElementDeclContentType (contentType);
  379. }
  380. /**
  381. */
  382. public static final void checkDocumentVersion (String version) throws InvalidArgumentException {
  383. constraints.checkDocumentVersion (version);
  384. }
  385. /**
  386. */
  387. public static final boolean isValidDocumentVersion (String version) {
  388. return constraints.isValidDocumentVersion (version);
  389. }
  390. /**
  391. */
  392. public static final void checkCDATASectionData (String data) throws InvalidArgumentException {
  393. constraints.checkCDATASectionData (data);
  394. }
  395. /**
  396. */
  397. public static final boolean isValidCDATASectionData (String data) {
  398. return constraints.isValidCDATASectionData (data);
  399. }
  400. /**
  401. */
  402. public static final void checkNotationDeclPublicId (String publicId) throws InvalidArgumentException {
  403. constraints.checkNotationDeclPublicId (publicId);
  404. }
  405. /**
  406. */
  407. public static final boolean isValidNotationDeclPublicId (String publicId) {
  408. return constraints.isValidNotationDeclPublicId (publicId);
  409. }
  410. /**
  411. */
  412. public static final void checkAttlistDeclAttributeName (String attributeName) throws InvalidArgumentException {
  413. constraints.checkAttlistDeclAttributeName (attributeName);
  414. }
  415. /**
  416. */
  417. public static final boolean isValidAttlistDeclAttributeName (String attributeName) {
  418. return constraints.isValidAttlistDeclAttributeName (attributeName);
  419. }
  420. /**
  421. */
  422. public static final void checkCommentData (String data) throws InvalidArgumentException {
  423. constraints.checkCommentData (data);
  424. }
  425. /**
  426. */
  427. public static final boolean isValidCommentData (String data) {
  428. return constraints.isValidCommentData (data);
  429. }
  430. /**
  431. */
  432. public static final void checkAttlistDeclAttributeType (short type) throws InvalidArgumentException {
  433. constraints.checkAttlistDeclAttributeType (type);
  434. }
  435. /**
  436. */
  437. public static final boolean isValidAttlistDeclAttributeType (short type) {
  438. return constraints.isValidAttlistDeclAttributeType (type);
  439. }
  440. /**
  441. */
  442. public static final void checkAttlistDeclAttributeDefaultType (short defaultType) throws InvalidArgumentException {
  443. constraints.checkAttlistDeclAttributeDefaultType (defaultType);
  444. }
  445. /**
  446. */
  447. public static final boolean isValidAttlistDeclAttributeDefaultType (short defaultType) {
  448. return constraints.isValidAttlistDeclAttributeDefaultType (defaultType);
  449. }
  450. //
  451. // Constraints
  452. //
  453. /**
  454. *
  455. */
  456. private static final class Constraints extends UnicodeClasses
  457. implements AttlistDecl.Constraints,
  458. Attribute.Constraints,
  459. CDATASection.Constraints,
  460. CharacterReference.Constraints,
  461. Comment.Constraints,
  462. ConditionalSection.Constraints,
  463. DocumentFragment.Constraints,
  464. Document.Constraints,
  465. DocumentType.Constraints,
  466. DTD.Constraints,
  467. ElementDecl.Constraints,
  468. Element.Constraints,
  469. EntityDecl.Constraints,
  470. GeneralEntityReference.Constraints,
  471. NotationDecl.Constraints,
  472. ParameterEntityReference.Constraints,
  473. ProcessingInstruction.Constraints,
  474. Text.Constraints {
  475. //
  476. // itself
  477. //
  478. /**
  479. */
  480. private static void checkNullArgument (String argName, Object argValue) throws InvalidArgumentException {
  481. if ( argValue == null ) {
  482. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_null_value"));
  483. }
  484. }
  485. /**
  486. */
  487. private static void checkEmptyString (String argName, String string, boolean trim) throws InvalidArgumentException {
  488. if ( (string.length () == 0) || (trim && (string.trim ().equals (""))) ) { // NOI18N
  489. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_empty_value"));
  490. }
  491. }
  492. //
  493. // global constraints
  494. //
  495. /**
  496. * @see http://www.w3.org/TR/REC-xml#NT-Name
  497. */
  498. private static void checkXMLName (String argName, String name) throws InvalidArgumentException {
  499. checkNullArgument (argName, name);
  500. checkEmptyString (argName, name, true);
  501. char first = name.charAt (0);
  502. if (!!! isXMLNameStartChar (first)) {
  503. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_first_char", String.valueOf (first)));
  504. }
  505. for (int i = 0, len = name.length (); i < len; i++) {
  506. char c = name.charAt (i);
  507. if (!!! isXMLNameChar (c)) {
  508. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", String.valueOf (c)));
  509. }
  510. }
  511. }
  512. /**
  513. * @see http://www.w3.org/TR/REC-xml#NT-Nmtoken
  514. */
  515. private static void checkNmToken (String argName, String token) throws InvalidArgumentException {
  516. checkNullArgument (argName, token);
  517. checkEmptyString (argName, token, true);
  518. for (int i = 0, len = token.length (); i < len; i++) {
  519. char c = token.charAt (i);
  520. if (!!! isXMLNameChar (c)) {
  521. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", String.valueOf (c)));
  522. }
  523. }
  524. }
  525. /**
  526. * @see http://www.w3.org/TR/REC-xml-names/#NT-NCName
  527. */
  528. private static void checkXMLNCName (String argName, String name) throws InvalidArgumentException {
  529. checkNullArgument (argName, name);
  530. checkEmptyString (argName, name, true);
  531. char first = name.charAt (0);
  532. if (!!! isXMLNCNameStartChar (first)) {
  533. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_first_char", String.valueOf (first)));
  534. }
  535. for (int i = 0, len = name.length (); i < len; i++) {
  536. char c = name.charAt (i);
  537. if (!!! isXMLNCNameChar (c)) {
  538. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", String.valueOf (c)));
  539. }
  540. }
  541. }
  542. /**
  543. */
  544. private static void checkNamespacePrefix (String prefix) throws InvalidArgumentException {
  545. String argName = Util.THIS.getString ("PROP_NamespacePrefix");
  546. checkXMLNCName (argName, prefix);
  547. }
  548. /**
  549. */
  550. private static void checkNamespaceURI (String uri) throws InvalidArgumentException {
  551. String argName = Util.THIS.getString ("PROP_NamespaceURI");
  552. checkAttributeValue (argName, uri);
  553. }
  554. /**
  555. */
  556. private static void checkElementName (String argName, String name) throws InvalidArgumentException {
  557. checkNullArgument (argName, name);
  558. checkXMLName (argName, name);
  559. }
  560. /**
  561. */
  562. private static void checkAttributeName (String argName, String name) throws InvalidArgumentException {
  563. checkNullArgument (argName, name);
  564. checkXMLName (argName, name);
  565. }
  566. /**
  567. */
  568. private static void checkAttributeValue (String argName, String value) throws InvalidArgumentException {
  569. checkNullArgument (argName, value);
  570. checkCharacterData (argName, value);
  571. int index = value.indexOf ('<');
  572. if ( index != -1 ) {
  573. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_invalid_attribute_value", value));
  574. }
  575. index = value.indexOf ('&');
  576. if ( index != -1 ) {
  577. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_invalid_attribute_value", value));
  578. }
  579. boolean apostrofFound = false;
  580. boolean quoteFound = false;
  581. for (int i = 0, len = value.length (); i < len; i++) {
  582. char c = value.charAt (i);
  583. if (c == '\'')
  584. if (quoteFound)
  585. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_invalid_attribute_value", value));
  586. else
  587. apostrofFound = true;
  588. if (c == '"')
  589. if (apostrofFound)
  590. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_invalid_attribute_value", value));
  591. else
  592. quoteFound = true;
  593. }
  594. }
  595. /**
  596. */
  597. private static void checkCharacterData (String argName, String text) throws InvalidArgumentException {
  598. checkNullArgument (argName, text);
  599. // do check
  600. for (int i = 0, len = text.length (); i < len; i++) {
  601. char c = text.charAt (i);
  602. if (!!! isXMLChar (c)) {
  603. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", "0x" + Integer.toHexString (c))); // NOI18N
  604. }
  605. }
  606. }
  607. /**
  608. */
  609. private static void checkSystemId (String argName, String systemId) throws InvalidArgumentException {
  610. boolean apostrofFound = false;
  611. boolean quoteFound = false;
  612. for (int i = 0, len = systemId.length (); i < len; i++) {
  613. char c = systemId.charAt (i);
  614. if (c == '\'')
  615. if (quoteFound)
  616. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_system_id", systemId));
  617. else
  618. apostrofFound = true;
  619. if (c == '"')
  620. if (apostrofFound)
  621. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_system_id", systemId));
  622. else
  623. quoteFound = true;
  624. }
  625. }
  626. /**
  627. */
  628. private static void checkPublicId (String argName, String publicId) throws InvalidArgumentException {
  629. boolean apostrofFound = false;
  630. boolean quoteFound = false;
  631. for (int i = 0, len = publicId.length (); i < len; i++) {
  632. char c = publicId.charAt (i);
  633. if (c == '\'') {
  634. if (quoteFound) {
  635. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_public_id", publicId));
  636. } else {
  637. apostrofFound = true;
  638. }
  639. } else if (c == '"') {
  640. if (apostrofFound) {
  641. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_public_id", publicId));
  642. } else {
  643. quoteFound = true;
  644. }
  645. } else if ( isXMLPubidLiteral (c) == false ) {
  646. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_public_id", publicId));
  647. }
  648. }
  649. }
  650. /**
  651. */
  652. private static void checkNotationName (String argName, String name) throws InvalidArgumentException {
  653. checkNullArgument (argName, name);
  654. checkXMLName (argName, name);
  655. }
  656. /**
  657. */
  658. private static void checkEncoding (String argName, String encoding) throws InvalidArgumentException {
  659. if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TreeUtilities::checkEncoding: encoding = " + encoding); // NOI18N
  660. ByteArrayInputStream stream = new ByteArrayInputStream (new byte[0]);
  661. if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::checkEncoding: stream = " + stream); // NOI18N
  662. try {
  663. InputStreamReader reader = new InputStreamReader (stream, iana2java (encoding));
  664. if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::checkEncoding: reader = " + reader); // NOI18N
  665. } catch (IOException exc) {
  666. if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" ::checkEncoding: IOException !!!", exc); // NOI18N
  667. throw new InvalidArgumentException (argName, Util.THIS.getString ("EXC_Invalid_encoding", encoding));
  668. }
  669. }
  670. /**
  671. */
  672. public void checkAttributeName (TreeName treeName) throws InvalidArgumentException {
  673. String argName = Util.THIS.getString ("PROP_AttributeName");
  674. checkAttributeName (argName, treeName.getQualifiedName ());
  675. }
  676. /**
  677. */
  678. public boolean isValidAttributeName (TreeName treeName) {
  679. try {
  680. checkAttributeName (treeName);
  681. } catch (InvalidArgumentException exc) {
  682. return false;
  683. }
  684. return true;
  685. }
  686. /**
  687. */
  688. public void checkElementTagName (TreeName elementTreeName) throws InvalidArgumentException {
  689. checkElementName (Util.THIS.getString ("PROP_ElementTagName"), elementTreeName.getQualifiedName ());
  690. }
  691. /**
  692. */
  693. public boolean isValidElementTagName (TreeName elementTreeName) {
  694. try {
  695. checkElementTagName (elementTreeName);
  696. } catch (InvalidArgumentException exc) {
  697. return false;
  698. }
  699. return true;
  700. }
  701. /**
  702. */
  703. public void checkNotationDeclSystemId (String systemId) throws InvalidArgumentException {
  704. if ( systemId == null ) {
  705. return;
  706. }
  707. checkSystemId (Util.THIS.getString ("PROP_NotationDeclSystemId"), systemId);
  708. }
  709. /**
  710. */
  711. public boolean isValidNotationDeclSystemId (String systemId) {
  712. try {
  713. checkNotationDeclSystemId (systemId);
  714. } catch (InvalidArgumentException exc) {
  715. return false;
  716. }
  717. return true;
  718. }
  719. /**
  720. */
  721. public void checkDocumentEncoding (String encoding) throws InvalidArgumentException {
  722. if ( encoding == null )
  723. return;
  724. checkEncoding (Util.THIS.getString ("PROP_DocumentEncoding"), encoding);
  725. }
  726. /**
  727. */
  728. public boolean isValidDocumentEncoding (String encoding) {
  729. try {
  730. checkDocumentEncoding (encoding);
  731. } catch (InvalidArgumentException exc) {
  732. return false;
  733. }
  734. return true;
  735. }
  736. /**
  737. */
  738. public void checkDTDEncoding (String encoding) throws InvalidArgumentException {
  739. if ( encoding == null )
  740. return;
  741. checkEncoding (Util.THIS.getString ("PROP_DTDEncoding"), encoding);
  742. }
  743. /**
  744. */
  745. public boolean isValidDTDEncoding (String encoding) {
  746. try {
  747. checkDTDEncoding (encoding);
  748. } catch (InvalidArgumentException exc) {
  749. return false;
  750. }
  751. return true;
  752. }
  753. /**
  754. */
  755. public void checkCharacterReferenceName (String name) throws InvalidArgumentException {
  756. String argName = Util.THIS.getString ("PROP_CharacterReferenceName");
  757. checkNullArgument (argName, name);
  758. checkEmptyString (argName, name, true);
  759. int i = 0;
  760. char first = name.charAt (i);
  761. if ( first != '#' ) {
  762. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_first_char", String.valueOf (first)));
  763. }
  764. i++;
  765. if ( name.length () <= i ) {
  766. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_empty_value"));
  767. }
  768. char second = name.charAt (i);
  769. int radix = 10;
  770. if ( second == 'x' ) {
  771. radix = 16;
  772. i++;
  773. if ( name.length () <= i ) {
  774. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_empty_value"));
  775. }
  776. }
  777. String number = name.substring (i);
  778. try {
  779. Short.parseShort (number, radix);
  780. } catch (NumberFormatException exc) {
  781. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", number));
  782. }
  783. /* for (int len = name.length(); i < len; i++) {
  784. char c = name.charAt (i);
  785. if ( Character.digit (c, radix) == -1 ) {
  786. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", String.valueOf (c)));
  787. }
  788. }*/
  789. }
  790. /**
  791. */
  792. public boolean isValidCharacterReferenceName (String name) {
  793. try {
  794. checkCharacterReferenceName (name);
  795. } catch (InvalidArgumentException exc) {
  796. return false;
  797. }
  798. return true;
  799. }
  800. /**
  801. */
  802. public void checkEntityDeclInternalText (String internalText) throws InvalidArgumentException {
  803. checkNullArgument (Util.THIS.getString ("PROP_EntityDeclInternalText"), internalText);
  804. boolean apostrofFound = false;
  805. boolean quoteFound = false;
  806. for (int i = 0, len = internalText.length (); i < len; i++) {
  807. char c = internalText.charAt (i);
  808. if (c == '\'')
  809. if (quoteFound)
  810. throw new InvalidArgumentException (Util.THIS.getString ("PROP_EntityDeclInternalText"), Util.THIS.getString ("EXC_Invalid_Entity_Decl_Internal_text", internalText));
  811. else
  812. apostrofFound = true;
  813. if (c == '"')
  814. if (apostrofFound)
  815. throw new InvalidArgumentException (Util.THIS.getString ("PROP_EntityDeclInternalText"), Util.THIS.getString ("EXC_Invalid_Entity_Decl_Internal_text", internalText));
  816. else
  817. quoteFound = true;
  818. // todo
  819. // if (c == '%' || c == '&')
  820. // throw new InvalidArgumentException ("EntityDeclInternalText", Util.THIS.getString ("EXC_Invalid_Entity_Decl_Internal_text", internalText));
  821. }
  822. }
  823. /**
  824. */
  825. public boolean isValidEntityDeclInternalText (String internalText) {
  826. try {
  827. checkEntityDeclInternalText (internalText);
  828. } catch (InvalidArgumentException exc) {
  829. return false;
  830. }
  831. return true;
  832. }
  833. /**
  834. */
  835. public void checkAttlistDeclElementName (String elementName) throws InvalidArgumentException {
  836. checkElementName (Util.THIS.getString ("PROP_AttlistDeclElementName"), elementName);
  837. }
  838. /**
  839. */
  840. public boolean isValidAttlistDeclElementName (String elementName) {
  841. try {
  842. checkAttlistDeclElementName (elementName);
  843. } catch (InvalidArgumentException exc) {
  844. return false;
  845. }
  846. return true;
  847. }
  848. /**
  849. */
  850. public void checkDTDVersion (String version) throws InvalidArgumentException {
  851. if (version == null)
  852. return;
  853. if (!!! version.equals ("1.0")) { // NOI18N
  854. String arg = Util.THIS.getString ("PROP_DTDVersion");
  855. String msg = Util.THIS.getString ("PROP_invalid_version_number", version);
  856. throw new InvalidArgumentException (arg, msg);
  857. }
  858. }
  859. /**
  860. */
  861. public boolean isValidDTDVersion (String version) {
  862. try {
  863. checkDTDVersion (version);
  864. } catch (InvalidArgumentException exc) {
  865. return false;
  866. }
  867. return true;
  868. }
  869. /**
  870. */
  871. public void checkDocumentTypeSystemId (String systemId) throws InvalidArgumentException {
  872. if ( systemId == null ) {
  873. return;
  874. }
  875. checkSystemId (Util.THIS.getString ("PROP_DocumentTypeSystemId"), systemId);
  876. }
  877. /**
  878. */
  879. public boolean isValidDocumentTypeSystemId (String systemId) {
  880. try {
  881. checkDocumentTypeSystemId (systemId);
  882. } catch (InvalidArgumentException exc) {
  883. return false;
  884. }
  885. return true;
  886. }
  887. /**
  888. */
  889. public void checkDocumentTypeElementName (String elementName) throws InvalidArgumentException {
  890. checkElementName (Util.THIS.getString ("PROP_DocumentTypeElementName"), elementName);
  891. }
  892. /**
  893. */
  894. public boolean isValidDocumentTypeElementName (String elementName) {
  895. try {
  896. checkDocumentTypeElementName (elementName);
  897. } catch (InvalidArgumentException exc) {
  898. return false;
  899. }
  900. return true;
  901. }
  902. /**
  903. */
  904. public void checkDocumentStandalone (String standalone) throws InvalidArgumentException {
  905. if (standalone == null)
  906. return;
  907. if (standalone.equals ("yes")) // NOI18N
  908. return;
  909. if (standalone.equals ("no")) // NOI18N
  910. return;
  911. throw new InvalidArgumentException (standalone, standalone + Util.THIS.getString ("PROP_is_not_valid_standalone_value"));
  912. }
  913. /**
  914. */
  915. public boolean isValidDocumentStandalone (String standalone) {
  916. try {
  917. checkDocumentStandalone (standalone);
  918. } catch (InvalidArgumentException exc) {
  919. return false;
  920. }
  921. return true;
  922. }
  923. /**
  924. */
  925. public void checkEntityDeclName (String name) throws InvalidArgumentException {
  926. checkXMLName (Util.THIS.getString ("PROP_EntityDeclName"), name);
  927. }
  928. /**
  929. */
  930. public boolean isValidEntityDeclName (String name) {
  931. try {
  932. checkEntityDeclName (name);
  933. } catch (InvalidArgumentException exc) {
  934. return false;
  935. }
  936. return true;
  937. }
  938. /**
  939. */
  940. public void checkAttlistDeclAttributeEnumeratedType (String[] enumeratedType) throws InvalidArgumentException {
  941. if ( enumeratedType == null ) {
  942. return;
  943. }
  944. for (int i = 0, len = enumeratedType.length; i < len; i++)
  945. checkNmToken (Util.THIS.getString ("PROP_AttlistDeclAttributeEnumeratedType"), enumeratedType[i]);
  946. }
  947. /**
  948. */
  949. public boolean isValidAttlistDeclAttributeEnumeratedType (String[] enumeratedType) {
  950. try {
  951. checkAttlistDeclAttributeEnumeratedType (enumeratedType);
  952. } catch (InvalidArgumentException exc) {
  953. return false;
  954. }
  955. return true;
  956. }
  957. /**
  958. */
  959. public void checkProcessingInstructionData (String data) throws InvalidArgumentException {
  960. checkCharacterData (Util.THIS.getString ("PROP_ProcessingInstructionData"), data);
  961. int index = data.indexOf ("?>"); // NOI18N
  962. if (index != -1) {
  963. throw new InvalidArgumentException (data, Util.THIS.getString ("PROP_invalid_processing_instruction_data"));
  964. }
  965. }
  966. /**
  967. */
  968. public boolean isValidProcessingInstructionData (String data) {
  969. try {
  970. checkProcessingInstructionData (data);
  971. } catch (InvalidArgumentException exc) {
  972. return false;
  973. }
  974. return true;
  975. }
  976. /**
  977. */
  978. public void checkEntityDeclNotationName (String notationName) throws InvalidArgumentException {
  979. if ( notationName == null ) {
  980. return;
  981. }
  982. checkNotationName (Util.THIS.getString ("PROP_EntityDeclNotationName"), notationName);
  983. }
  984. /**
  985. */
  986. public boolean isValidEntityDeclNotationName (String notationName) {
  987. try {
  988. checkEntityDeclNotationName (notationName);
  989. } catch (InvalidArgumentException exc) {
  990. return false;
  991. }
  992. return true;
  993. }
  994. /**
  995. */
  996. public void checkElementDeclName (String name) throws InvalidArgumentException {
  997. checkElementName (Util.THIS.getString ("PROP_ElementDeclName"), name);
  998. }
  999. /**
  1000. */
  1001. public boolean isValidElementDeclName (String name) {
  1002. try {
  1003. checkElementDeclName (name);
  1004. } catch (InvalidArgumentException exc) {
  1005. return false;
  1006. }
  1007. return true;
  1008. }
  1009. /**
  1010. */
  1011. public void checkGeneralEntityReferenceName (String name) throws InvalidArgumentException {
  1012. checkXMLName (Util.THIS.getString ("PROP_GeneralEntityReferenceName"), name);
  1013. }
  1014. /**
  1015. */
  1016. public boolean isValidGeneralEntityReferenceName (String name) {
  1017. try {
  1018. checkGeneralEntityReferenceName (name);
  1019. } catch (InvalidArgumentException exc) {
  1020. return false;
  1021. }
  1022. return true;
  1023. }
  1024. /**
  1025. */
  1026. public void checkEntityDeclSystemId (String systemId) throws InvalidArgumentException {
  1027. if ( systemId == null ) {
  1028. return;
  1029. }
  1030. checkSystemId (Util.THIS.getString ("PROP_EntityDeclSystemId"), systemId);
  1031. }
  1032. /**
  1033. */
  1034. public boolean isValidEntityDeclSystemId (String systemId) {
  1035. try {
  1036. checkEntityDeclSystemId (systemId);
  1037. } catch (InvalidArgumentException exc) {
  1038. return false;
  1039. }
  1040. return true;
  1041. }
  1042. /**
  1043. */
  1044. public void checkProcessingInstructionTarget (String target) throws InvalidArgumentException {
  1045. String argName = Util.THIS.getString ("PROP_ProcessingInstructionTarget");
  1046. checkXMLName (argName, target);
  1047. if (target.equalsIgnoreCase ("xml")) { // NOI18N
  1048. throw new InvalidArgumentException (argName, Util.THIS.getString ("PROP_invalid_content_char", target));
  1049. }
  1050. }
  1051. /**
  1052. */
  1053. public boolean isValidProcessingInstructionTarget (String target) {
  1054. try {
  1055. checkProcessingInstructionTarget (target);
  1056. } catch (InvalidArgumentException exc) {
  1057. return false;
  1058. }
  1059. return true;
  1060. }
  1061. /**
  1062. */
  1063. public void checkEntityDeclPublicId (String publicId) throws InvalidArgumentException {
  1064. if ( publicId == null ) {
  1065. return;
  1066. }
  1067. checkPublicId (Util.THIS.getString ("PROP_EntityDeclPublicId"), publicId);
  1068. }
  1069. /**
  1070. */
  1071. public boolean isValidEntityDeclPublicId (String publicId) {
  1072. try {
  1073. checkEntityDeclPublicId (publicId);
  1074. } catch (InvalidArgumentException exc) {
  1075. return false;
  1076. }
  1077. return true;
  1078. }
  1079. /**
  1080. */
  1081. public void checkAttlistDeclAttributeDefaultValue (String defaultValue) throws InvalidArgumentException {
  1082. if ( defaultValue == null ) {
  1083. return;
  1084. }
  1085. boolean apostrofFound = false;
  1086. boolean quoteFound = false;
  1087. for (int i = 0, len = defaultValue.length (); i < len; i++) {
  1088. char c = defaultValue.charAt (i);
  1089. if (c == '\'')
  1090. if (quoteFound)
  1091. throw new InvalidArgumentException (Util.THIS.getString ("PROP_AttlistDeclAttributeDefaultValue"), Util.THIS.getString ("EXC_invalid_attribute_default_value", defaultValue));
  1092. else
  1093. apostrofFound = true;
  1094. if (c == '"')
  1095. if (apostrofFound)
  1096. throw new InvalidArgumentException (Util.THIS.getString ("PROP_AttlistDeclAttributeDefaultValue"), Util.THIS.getString ("EXC_invalid_attribute_default_value", defaultValue));
  1097. else
  1098. quoteFound = true;
  1099. // todo
  1100. // if (c == '%' || c == '&')
  1101. // throw new InvalidArgumentException ("AttlistDeclAttributeDefaultValue", Util.THIS.getString ("EXC_invalid_attribute_default_value", defaultValue));
  1102. }
  1103. }
  1104. /**
  1105. */
  1106. public boolean isValidAttlistDeclAttributeDefaultValue (String defaultValue) {
  1107. try {
  1108. checkAttlistDeclAttributeDefaultValue (defaultValue);
  1109. } catch (InvalidArgumentException exc) {
  1110. return false;
  1111. }
  1112. return true;
  1113. }
  1114. /**
  1115. */
  1116. public void checkDocumentFragmentVersion (String version) throws InvalidArgumentException {
  1117. if ( version == null )
  1118. return;
  1119. if (!!! version.equals ("1.0")) { // NOI18N
  1120. String arg = Util.THIS.getString ("PROP_DocumentFragmentVersion");
  1121. String msg = Util.THIS.getString ("PROP_invalid_version_number", version);
  1122. throw new InvalidArgumentException (arg, msg);
  1123. }
  1124. }
  1125. /**
  1126. */
  1127. public boolean isValidDocumentFragmentVersion (String version) {
  1128. try {
  1129. checkDocumentFragmentVersion (version);
  1130. } catch (InvalidArgumentException exc) {
  1131. return false;
  1132. }
  1133. return true;
  1134. }
  1135. /**
  1136. */
  1137. public void checkNotationDeclName (String name) throws InvalidArgumentException {
  1138. checkXMLName (Util.THIS.getString ("PROP_NotationDeclName"), name);
  1139. }
  1140. /**
  1141. */
  1142. public boolean isValidNotationDeclName (String name) {
  1143. try {
  1144. checkNotationDeclName (name);
  1145. } catch (InvalidArgumentException exc) {
  1146. return false;
  1147. }
  1148. return true;
  1149. }
  1150. /**
  1151. */
  1152. public void checkAttributeValue (String value) throws InvalidArgumentException {
  1153. String argName = Util.THIS.getString ("PROP_AttributeValue");
  1154. checkAttributeValue (argName, value);
  1155. }
  1156. /**
  1157. */
  1158. public boolean isValidAttributeValue (String value) {
  1159. try {
  1160. checkAttributeValue (value);
  1161. } catch (InvalidArgumentException exc) {
  1162. return false;
  1163. }
  1164. return true;
  1165. }
  1166. /**
  1167. */
  1168. public void checkParameterEntityReferenceName (String name) throws InvalidArgumentException {
  1169. checkXMLName (Util.THIS.getString ("PROP_ParameterEntityReferenceName"), name);
  1170. }
  1171. /**
  1172. */
  1173. public boolean isValidParameterEntityReferenceName (String name) {
  1174. try {
  1175. checkParameterEntityReferenceName (name);
  1176. } catch (InvalidArgumentException exc) {
  1177. return false;
  1178. }
  1179. return true;
  1180. }
  1181. /**
  1182. */
  1183. public void checkDocumentFragmentEncoding (String encoding) throws InvalidArgumentException {
  1184. if ( encoding == null )
  1185. return;
  1186. checkEncoding (Util.THIS.getString ("PROP_DocumentFragmentEncoding"), encoding);
  1187. }
  1188. /**
  1189. */
  1190. public boolean isValidDocumentFragmentEncoding (String encoding) {
  1191. try {
  1192. checkDocumentFragmentEncoding (encoding);
  1193. } catch (InvalidArgumentException exc) {
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. /**
  1199. */
  1200. public void checkTextData (String data) throws InvalidArgumentException {
  1201. String argName = Util.THIS.getString ("PROP_TextData");
  1202. checkCharacterData (argName, data);
  1203. checkEmptyString (argName, data, false);
  1204. int index = data.indexOf ('<');
  1205. if ( index != -1 ) {
  1206. throw new InvalidArgumentException (data, Util.THIS.getString ("PROP_invalid_text_data"));
  1207. }
  1208. index = data.indexOf ('&');
  1209. if ( index != -1 ) {
  1210. throw new InvalidArgumentException (data, Util.THIS.getString ("PROP_invalid_text_data"));
  1211. }
  1212. index = data.indexOf ("]]>");
  1213. if ( index != -1 ) {
  1214. throw new InvalidArgumentException (data, Util.THIS.getString ("PROP_invalid_text_data"));
  1215. }
  1216. }
  1217. /**
  1218. */
  1219. public boolean isValidTextData (String data) {
  1220. try {
  1221. checkTextData (data);
  1222. } catch (InvalidArgumentException exc) {
  1223. return false;
  1224. }
  1225. return true;
  1226. }
  1227. /**
  1228. */
  1229. public void checkDocumentTypePublicId (String publicId) throws InvalidArgumentException {
  1230. if ( publicId == null ) {
  1231. return;
  1232. }
  1233. checkPublicId (Util.THIS.getString ("PROP_DocumentTypePublicId"), publicId);
  1234. }
  1235. /**
  1236. */
  1237. public boolean isValidDocumentTypePublicId (String publicId) {
  1238. try {
  1239. checkDocumentTypePublicId (publicId);
  1240. } catch (InvalidArgumentException exc) {