com.google.gxp.base..svn.text-base.GxpAttrBundle.svn-base Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of google-gxp Show documentation
Show all versions of google-gxp Show documentation
Google XML Pages (GXP) is a templating system used to generate XML/SGML markup (most often HTML).
The newest version!
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gxp.base;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
/**
* An attribute bundle. Used by the GXP compiler for bundling up a bunch
* of attributes into a single item. Currently supports regular and boolean
* attributes.
*/
public class GxpAttrBundle {
private final ImmutableMap attrs;
private final ImmutableSet booleanAttrs;
GxpAttrBundle(Map attrs, Set booleanAttrs) {
this.attrs = ImmutableMap.copyOf(attrs);
this.booleanAttrs = ImmutableSet.copyOf(booleanAttrs);
}
public void write(Appendable out, GxpContext gxpContext) throws IOException {
for (Map.Entry attr : attrs.entrySet()) {
out.append(' ');
out.append(attr.getKey());
out.append("=\"");
attr.getValue().write(out, gxpContext);
out.append('"');
}
for (String booleanAttr : booleanAttrs) {
out.append(' ');
out.append(booleanAttr);
if (gxpContext.isForcingXmlSyntax()) {
out.append("=\"");
out.append(booleanAttr);
out.append('"');
}
}
}
private Map getAttrs() {
return attrs;
}
private Set getBooleanAttrs() {
return booleanAttrs;
}
/**
* An attribute bundle builder.
*/
public static class Builder {
private final ImmutableSet includeAttrs;
private final Map attrs = Maps.newLinkedHashMap();
private final Set booleanAttrs = Sets.newLinkedHashSet();
// in this case all attributes from added bundles will be accepted
public Builder() {
this.includeAttrs = null;
}
// in this case only specified attributes will be accepted from bundles
public Builder(String... includeAttrs) {
this.includeAttrs = ImmutableSet.copyOf(includeAttrs);
}
public Builder attr(String name, T value, boolean cond) {
return cond ? attr(name, value) : this;
}
public Builder attr(String name, T value) {
attrs.put(name, value);
return this;
}
public Builder attr(String name, boolean include, boolean cond) {
return cond ? attr(name, include) : this;
}
public Builder attr(String name, boolean include) {
if (include) {
booleanAttrs.add(name);
}
return this;
}
public Builder addBundle(GxpAttrBundle bundle) {
if (includeAttrs == null) {
attrs.putAll(bundle.getAttrs());
booleanAttrs.addAll(bundle.getBooleanAttrs());
} else {
for (Map.Entry attr : bundle.getAttrs().entrySet()) {
if (includeAttrs.contains(attr.getKey())) {
attrs.put(attr.getKey(), attr.getValue());
}
}
for (String attr : bundle.getBooleanAttrs()) {
if (includeAttrs.contains(attr)) {
booleanAttrs.add(attr);
}
}
}
return this;
}
public GxpAttrBundle build() {
return new GxpAttrBundle(attrs, booleanAttrs);
}
}
}