1919import java .io .ObjectOutputStream ;
2020import java .io .Serializable ;
2121import java .io .StringReader ;
22- import java .net .URI ;
23- import java .net .URISyntaxException ;
22+ import java .util .ArrayList ;
23+ import java .util .HashMap ;
24+ import java .util .Iterator ;
25+ import java .util .List ;
26+ import java .util .Map ;
2427
2528import org .w3c .dom .DOMException ;
2629import org .w3c .dom .Node ;
27- import org .w3c .dom .css .CSSImportRule ;
2830import org .w3c .dom .css .CSSRule ;
2931import org .w3c .dom .css .CSSRuleList ;
3032import org .w3c .dom .css .CSSStyleSheet ;
3537import com .gargoylesoftware .css .parser .CSSOMParser ;
3638import com .gargoylesoftware .css .parser .InputSource ;
3739import com .gargoylesoftware .css .parser .media .MediaQueryList ;
40+ import com .gargoylesoftware .css .parser .selector .Selector ;
3841import com .gargoylesoftware .css .util .LangUtils ;
3942import com .gargoylesoftware .css .util .ThrowCssExceptionErrorHandler ;
4043
@@ -53,20 +56,12 @@ public class CSSStyleSheetImpl implements CSSStyleSheet, Serializable {
5356 private CSSRule ownerRule_ ;
5457 private boolean readOnly_ ;
5558 private CSSRuleList cssRules_ ;
56- private String baseUri_ ;
59+ private CSSStyleSheetRuleIndex index_ ;
5760
5861 public void setMedia (final MediaList media ) {
5962 media_ = media ;
6063 }
6164
62- private String getBaseUri () {
63- return baseUri_ ;
64- }
65-
66- public void setBaseUri (final String baseUri ) {
67- baseUri_ = baseUri ;
68- }
69-
7065 public CSSStyleSheetImpl () {
7166 super ();
7267 }
@@ -307,7 +302,6 @@ public boolean equals(final Object obj) {
307302 @ Override
308303 public int hashCode () {
309304 int hash = LangUtils .HASH_SEED ;
310- hash = LangUtils .hashCode (hash , baseUri_ );
311305 hash = LangUtils .hashCode (hash , cssRules_ );
312306 hash = LangUtils .hashCode (hash , disabled_ );
313307 hash = LangUtils .hashCode (hash , href_ );
@@ -319,7 +313,6 @@ public int hashCode() {
319313 }
320314
321315 private void writeObject (final ObjectOutputStream out ) throws IOException {
322- out .writeObject (baseUri_ );
323316 out .writeObject (cssRules_ );
324317 out .writeBoolean (disabled_ );
325318 out .writeObject (href_ );
@@ -329,7 +322,6 @@ private void writeObject(final ObjectOutputStream out) throws IOException {
329322 }
330323
331324 private void readObject (final ObjectInputStream in ) throws IOException , ClassNotFoundException {
332- baseUri_ = (String ) in .readObject ();
333325 cssRules_ = (CSSRuleList ) in .readObject ();
334326 if (cssRules_ != null ) {
335327 for (int i = 0 ; i < cssRules_ .getLength (); i ++) {
@@ -346,41 +338,156 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot
346338 title_ = (String ) in .readObject ();
347339 }
348340
349- /**
350- * Imports referenced CSSStyleSheets.
351- *
352- * @param recursive <code>true</code> if the import should be done
353- * recursively, <code>false</code> otherwise
354- */
355- public void importImports (final boolean recursive ) throws DOMException {
356- for (int i = 0 ; i < getCssRules ().getLength (); i ++) {
357- final CSSRule cssRule = getCssRules ().item (i );
358- if (cssRule .getType () == CSSRule .IMPORT_RULE ) {
359- final CSSImportRule cssImportRule = (CSSImportRule ) cssRule ;
360- try {
361- final URI importURI = new URI (getBaseUri ()).resolve (cssImportRule .getHref ());
362- final CSSStyleSheetImpl importedCSS = (CSSStyleSheetImpl )
363- new CSSOMParser ().parseStyleSheet (
364- new InputSource (importURI .toString ()), importURI .toString ());
365- if (recursive ) {
366- importedCSS .importImports (recursive );
367- }
368- final MediaList mediaList = cssImportRule .getMedia ();
369- if (mediaList .getLength () == 0 ) {
370- mediaList .appendMedium ("all" );
371- }
372- final CSSMediaRuleImpl cssMediaRule = new CSSMediaRuleImpl (this , null , mediaList );
373- cssMediaRule .setRuleList ((CSSRuleListImpl ) importedCSS .getCssRules ());
374- deleteRule (i );
375- ((CSSRuleListImpl ) getCssRules ()).insert (cssMediaRule , i );
341+ public CSSStyleSheetRuleIndex getRuleIndex () {
342+ return index_ ;
343+ }
344+
345+ public void setRuleIndex (final CSSStyleSheetRuleIndex index ) {
346+ index_ = index ;
347+ }
348+
349+ public void resetRuleIndex () {
350+ index_ = null ;
351+ }
352+
353+ public static final class SelectorEntry {
354+ private Selector selector_ ;
355+ private CSSStyleRuleImpl rule_ ;
356+
357+ SelectorEntry (final Selector selector , final CSSStyleRuleImpl rule ) {
358+ selector_ = selector ;
359+ rule_ = rule ;
360+ }
361+
362+ public Selector getSelector () {
363+ return selector_ ;
364+ }
365+
366+ public CSSStyleRuleImpl getRule () {
367+ return rule_ ;
368+ }
369+ }
370+
371+ public static class CSSStyleSheetRuleIndex {
372+
373+ private static final MediaList DEFAULT_MEDIA_LIST = new MediaListImpl (null );
374+
375+ private final List <CSSStyleSheetRuleIndex > children_ = new ArrayList <>();
376+
377+ private MediaList mediaList_ = DEFAULT_MEDIA_LIST ;
378+ private final Map <String , List <SelectorEntry >> elementSelectors_ = new HashMap <>();
379+ private final List <SelectorEntry > otherSelectors_ = new ArrayList <>();
380+
381+ public void addElementSelector (final String name , final Selector s , final CSSStyleRuleImpl styleRule ) {
382+ List <SelectorEntry > entries = elementSelectors_ .get (name );
383+ if (entries == null ) {
384+ entries = new ArrayList <SelectorEntry >();
385+ elementSelectors_ .put (name , entries );
386+ }
387+ final SelectorEntry selectorEntry = new SelectorEntry (s , styleRule );
388+ entries .add (selectorEntry );
389+ }
390+
391+ public void addOtherSelector (final Selector s , final CSSStyleRuleImpl styleRule ) {
392+ final SelectorEntry selectorEntry = new SelectorEntry (s , styleRule );
393+ otherSelectors_ .add (selectorEntry );
394+ }
395+
396+ public CSSStyleSheetRuleIndex addMedia (final MediaList mediaList ) {
397+ final CSSStyleSheetRuleIndex index = new CSSStyleSheetRuleIndex ();
398+ index .mediaList_ = mediaList ;
399+
400+ children_ .add (index );
401+ return index ;
402+ }
403+
404+ public MediaList getMediaList () {
405+ return mediaList_ ;
406+ }
407+
408+ public List <CSSStyleSheetRuleIndex > getChildren () {
409+ return children_ ;
410+ }
411+
412+ public Iterator <SelectorEntry > getSelectorEntriesIteratorFor (final String elementName ) {
413+ return new SelectorEntriesIterator (this , elementName );
414+ }
415+ }
416+
417+ static final class SelectorEntriesIterator implements Iterator <SelectorEntry > {
418+ private Iterator <SelectorEntry > anyElementSelectors_ ;
419+ private Iterator <SelectorEntry > elementSelectors_ ;
420+ private Iterator <SelectorEntry > otherSelectors_ ;
421+
422+ SelectorEntriesIterator (final CSSStyleSheetRuleIndex index , final String elementName ) {
423+ List <SelectorEntry > sel = index .elementSelectors_ .get ("*" );
424+ if (sel != null && !sel .isEmpty ()) {
425+ anyElementSelectors_ = sel .iterator ();
426+ }
427+ else {
428+ anyElementSelectors_ = null ;
429+ }
430+
431+ sel = index .elementSelectors_ .get (elementName );
432+ if (sel != null && !sel .isEmpty ()) {
433+ elementSelectors_ = sel .iterator ();
434+ }
435+ else {
436+ elementSelectors_ = null ;
437+ }
438+
439+ if (index .otherSelectors_ != null && !index .otherSelectors_ .isEmpty ()) {
440+ otherSelectors_ = index .otherSelectors_ .iterator ();
441+ }
442+ else {
443+ otherSelectors_ = null ;
444+ }
445+ }
446+
447+ @ Override
448+ public SelectorEntry next () {
449+ if (anyElementSelectors_ != null ) {
450+ if (anyElementSelectors_ .hasNext ()) {
451+ return anyElementSelectors_ .next ();
376452 }
377- catch (final URISyntaxException e ) {
378- throw new DOMException (DOMException .SYNTAX_ERR , e .getLocalizedMessage ());
453+ anyElementSelectors_ = null ;
454+ }
455+ if (elementSelectors_ != null ) {
456+ if (elementSelectors_ .hasNext ()) {
457+ return elementSelectors_ .next ();
379458 }
380- catch (final IOException e ) {
381- // TODO handle exception
459+ elementSelectors_ = null ;
460+ }
461+ if (otherSelectors_ != null ) {
462+ if (otherSelectors_ .hasNext ()) {
463+ return otherSelectors_ .next ();
382464 }
465+ otherSelectors_ = null ;
383466 }
467+ return null ;
468+ }
469+
470+ @ Override
471+ public boolean hasNext () {
472+ if (anyElementSelectors_ != null ) {
473+ if (anyElementSelectors_ .hasNext ()) {
474+ return true ;
475+ }
476+ anyElementSelectors_ = null ;
477+ }
478+ if (elementSelectors_ != null ) {
479+ if (elementSelectors_ .hasNext ()) {
480+ return true ;
481+ }
482+ elementSelectors_ = null ;
483+ }
484+ if (otherSelectors_ != null ) {
485+ if (otherSelectors_ .hasNext ()) {
486+ return true ;
487+ }
488+ otherSelectors_ = null ;
489+ }
490+ return false ;
384491 }
385492 }
386493}
0 commit comments